How to set DHCP IP address?  Download
This Delphi function changes the configuration of a network adapter to have its IP address obtained via DHCP. 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 enables DHCP on a specified network adapter adapters 
// 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.
// Warning: once the remote host changes its IP address, the connection 
//          to that host is lost and this function timeouts with error. 
function EnableDHCP(Destination, UserName, Password, Adapter: string): string;
var
  vConfigs: TWmiQuery;
  vConnection: TWmiConnection;
  vMethod: TWmiMethod;
begin
  vConfigs     := TWmiQuery.Create(nil);
  vConnection  := TWmiConnection.Create(nil);
  vMethod      := TWmiMethod.Create(nil);
  try
    if Destination <> '' then
    begin
      vConnection.MachineName := Destination;
      vConnection.Credentials.UserName := UserName;
      vConnection.Credentials.Password := Password;
    end;

    // 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;
    vMethod.WmiMethodName := 'EnableDHCP';

    if not vConfigs.Locate('Caption', Adapter, []) then
    begin
      Result := 'Network adapter not found: ' + Adapter;
      Exit;
    end;

    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;