How to set static IP address?
Download
|
|
This Delphi function changes the configuration of a network adapter to
have a static IP address and subnet mask. It works against local and remote computer.
However, there is a negative side effect when working with a remote host:
as soon as the remote host successfully changes its IP address, the connection
to the remote host is lost. Even though this function succeeds,
it timeouts with error. This side effect does not happen when configuring the
local host.
This examples utilizes TWmiQuery, TWmiMethod and TWmiConnection components. The code below creates and destroys the components on the fly, so this function may be used in console- or UI-type applications. The function requires the caption of network adapter, which can be retrieved via this example. |
// this function sets static IP address on a specified network adapter.
// It may be used for local or remote host. When used for local host
// Destination, UserName and Password parameters must be empty.
// For remote computer, parameters must be:
// TargetHost: name or IP address of the remote host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
// Adapter: the caption of the adapter where DHCP is to be enabled.
// ListActiveAdapters function returns this caption.
// See "How to find IP enabled adapter?" topic.
// IPAddress: the new IP address to be set, like '10.8.36.33'
// SubNetMask: the network mask to be set, for example '255.255.255.0'
// The method returns empty string on success and error description otherwise
//
// Warning: once the remote host changes its IP address, the connection
// to that host is lost and this function timeouts with error.
function SetStaticIP(Destination, UserName, Password, Adapter,
IPAddress, SubNetMask: string): string;
var
vConfigs: TWmiQuery;
vConnection: TWmiConnection;
vMethod: TWmiMethod;
begin
vConfigs := TWmiQuery.Create(nil);
vConnection := TWmiConnection.Create(nil);
vMethod := TWmiMethod.Create(nil);
try
vConnection.MachineName := Destination;
vConnection.Credentials.UserName := UserName;
vConnection.Credentials.Password := Password;
// try to connect. It may fail if the destination computer does not have
// WMI (like windows 95, 98, NT without WMI core installed), or
// if the provided credentials are not valid
try
vConnection.Connected := true;
except
Result := 'Cannot connect to destination.';
Exit;
end;
vConfigs.WQL.Text := 'select * from Win32_NetworkAdapterConfiguration ';
vConfigs.Connection := vConnection;
vConfigs.Open;
vMethod.WmiObjectSource := vConfigs;
if not vConfigs.Locate('Caption', Adapter, []) then
begin
Result := 'Network adapter not found: ' + Adapter;
Exit;
end;
// set static IP address
vMethod.WmiMethodName := 'EnableStatic';
vMethod.InParams.ParamByName('IPAddress').AsString := IPAddress;
vMethod.InParams.ParamByName('SubnetMask').AsString := SubNetMask;
if vMethod.Execute <> 0 then
begin
Result := vMethod.LastWmiErrorDescription;
if Result = '' then Result := 'Error ' + IntToStr(vMethod.LastWmiError);
end;
finally
vMethod.Free;
vConfigs.Free;
vConnection.Free;
end;
end;
|