dapidmini
dapidmini

Reputation: 1625

how to change my computer's network settings programmatically?

I use my laptop in two network environment (at work and at home) and they have different network settings (ip address, subnet mask, default gateway) so I have to change my network settings everytime I get home and when I arrive at the office.. is there anyway to write a code that can change the settings so that I only have to run a program instead of changing the settings manually?

I've googled a bit and found this but I can't find my network card name and it seems that the code only change the ip address and subnet mask but it doesn't change the default gateway.. am I correct?

oh btw, It'll be great if you guys can use delphi programming in answering my question (especially delphi 7)

Upvotes: 3

Views: 4288

Answers (1)

RRUZ
RRUZ

Reputation: 136391

As you pointed the code posted on this question doesn't change the default gateway, in order to do that you must execute the SetGateways method as well.

Try this modified version of the original code which allow you setup the ip, mask and default gateway for a network adapter.

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils,
  ActiveX,
  Variants,
  ComObj;

procedure  SetStaticIpAddress(const NetworkCard, IPAddress, Mask, GateWay :string);
const
  WbemUser    ='';
  WbemPassword='';
  WbemComputer='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator   : OLEVariant;
  FWMIService     : OLEVariant;
  FWbemObjectSet  : OLEVariant;
  FWbemObject     : OLEVariant;
  FOutParams      : OLEVariant;
  vIpAddress      : OLEVariant;
  vGateWays       : OLEVariant;
  vMask           : OLEVariant;
  oEnum           : IEnumvariant;
  iValue          : LongWord;
begin
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);

  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM Win32_NetworkAdapterConfiguration Where Description="%s"',[NetworkCard]),'WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  if oEnum.Next(1, FWbemObject, iValue) = 0 then
  begin
    vIpAddress   := VarArrayCreate([0, 0], varVariant);
    vIpAddress[0]:= IPAddress;
    vMask   := VarArrayCreate([0, 0], varVariant);
    vMask[0]:=  Mask;
    FOutParams:=FWbemObject.EnableStatic(vIpAddress, vMask);
    // 0 - Successful completion, no reboot required
    // 1 - Successful completion, reboot required
    Writeln(Format('EnableStatic ReturnValue  %s',[FOutParams]));

    vGateWays   := VarArrayCreate([0, 0], varVariant);
    vGateWays[0]:= GateWay;

    FOutParams:=FWbemObject.SetGateways(vGateWays);
    // 0 - Successful completion, no reboot required
    // 1 - Successful completion, reboot required
    Writeln(Format('SetGateways ReturnValue  %s',[FOutParams]));
  end
  else
  Writeln('Network card not found');
end;


begin
 try
    CoInitialize(nil);
    try
      SetStaticIpAddress('network device','192.168.1.1','255.255.255.0','192.168.1.2');
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.

Note.. to get the network device name you can use the network connections list

enter image description here

Upvotes: 7

Related Questions