Einzelnen Beitrag anzeigen

Benutzerbild von x000x
x000x

Registriert seit: 21. Jan 2004
Ort: Bei Hamburg
308 Beiträge
 
Delphi XE2 Professional
 
#6

Re: IP Äandern von Fest zu DHCP und umgekehrt

  Alt 6. Okt 2006, 02:29
Moin,
Delphi-Quellcode:
{..}

uses {..,} ComObj, ActiveX;

{..} 

function ChangeIPAddress(const szConnID, szIPAddress, szSubnetMask, szGateway: String): Boolean;
var
   SWbem,
   SWbemCon,
   SWbemQuery,
   objVar : OleVariant;
   szWQL : String;
   pFetched : LongInt;
   unknown : IUnknown;
   enumUnk : IEnumUnknown;
   szMacAddr : String;
   varIPAddress : Variant;
   varSubnetMask : Variant;
   varGateway : Variant;
   varGatewayMetric : Variant;
begin
   Result := False;
   CoInitialize(nil);
   try
      varIPAddress := VarArrayCreate([0, 0], varOleStr);
      varIPAddress[0] := szIPAddress;

      varSubnetMask := VarArrayCreate([0, 0], varOleStr);
      varSubnetMask[0] := szSubnetMask;

      varGateway := VarArrayCreate([0, 0], varOleStr);
      varGateway[0] := szGateway;

      varGatewayMetric := VarArrayCreate([0, 0], varInteger);
      varGatewayMetric[0] := 1;

      SWbem := CreateOleObject('WbemScripting.SWbemLocator');
      // strServer, strNamespace, strUser, strPassword, strLocale, strAuthority,
      // iSecurityFlags, objwbemNamedValueSet
      SWbemCon := SWbem.ConnectServer('.', 'root\cimv2', '', '', '');
      szWQL := 'Select * From Win32_NetworkAdapter'+
                    ' Where NetConnectionID = ''' + szConnID + '''';
      SWbemQuery := SWbemCon.ExecQuery(szWQL);
      unknown := SWbemQuery._NewEnum;
      unknown.QueryInterface(IEnumVariant, enumUnk);
      if (enumUnk.Next(1, objVar, @pFetched) <> S_OK) or (pFetched = 0) then
         Exit;
      if VarIsEmpty(objVar.MACAddress) then
         Exit;
      szMacAddr := objVar.MACAddress;

      szWQL := 'Select * From Win32_NetworkAdapterConfiguration'+
                    ' Where MACAddress = ''' + szMacAddr + '''';
      SWbemQuery := SWbemCon.ExecQuery(szWQL);
      unknown := SWbemQuery._NewEnum;
      unknown.QueryInterface(IEnumVariant, enumUnk);
      if (enumUnk.Next(1, objVar, @pFetched) <> S_OK) or (pFetched = 0) then
         Exit;

      Result := (objVar.EnableStatic(varIPAddress, varSubnetMask) = S_OK) and
                (objVar.SetGateways(varGateway, varGatewayMetric) = S_OK);
   finally
      VarClear(SWbem);
      VarClear(SWbemCon);
      CoUnInitialize;
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   if ChangeIPAddress('Drahtlose Netzwerkverbindung', '192.168.0.8', '255.255.255.0', '192.168.0.1') then
      ShowMessage('Geändert')
   else
      ShowMessage('Nicht Geändert')
end;

{..}
Mit diesem Code hier und diesem MSDN-LINK solltest du dir eine Funktion für dynamische Adressen
selber erstellen können.
Peter
-= Gruss Peter =-
-= alias x000x =-
  Mit Zitat antworten Zitat