How do I programmatically change my computer's network settings?

I use my laptop in two network environments (work and home) and they have different network settings (IP address, subnet mask, default gateway), so I have to change the network settings every time I return home and when I I come to the office .. is there any code at all that can change the settings so that I only need to run the program and not change the settings manually?

I googled a bit and found this one , but I can't find the name of my network card, and it seems that the code only changes the IP address and subnet mask but it doesn't change the default gateway. Right?

oh btw it would be great if you guys can use delphi programming while answering my question (especially delphi 7)

+3


source to share


1 answer


As you pointed out, the code posted on this question does not change the default gateway, in order to do this you must execute SetGateways

.

Try this modified version of the source code that allows you to configure the IP address, mask, and default gateway for your 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 name of a network device you can use the list of network connections

enter image description here

+7


source







All Articles