{..}
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;
{..}