Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set my IP address programmatically?

Tags:

delphi

How can I set my IP address programmatically?

My application is checking it, using code from DelphiTricks (although I am not sure if the code from About.com might not be better)

I want to be able to set the address programmatically for testing purposes. And (I don't know if this is important), I want to be able to do so even I am not attached to any network (on a laptop, on a train).

like image 819
Mawg says reinstate Monica Avatar asked Sep 25 '12 00:09

Mawg says reinstate Monica


People also ask

Can you make your IP address anything you want?

Your public IP address is usually set by your internet service provider (ISP), and you can't choose it yourself. However, you can "coax" it to change in any of several different ways: Change your network or location: Your public IP address will change based on where and how you connect to the internet.

How do I manually choose an IP address?

Right Click Local Area Connection and select Properties. Then double click Internet Protocol Version 4 (TCP/IPv4). Select Use the Following IP address: and type in the IP address, Subnet mask and Default gateway. Click OK to apply the settings.


1 Answers

To change the ip address of your network adapter you can use the EnableStatic method of the Win32_NetworkAdapterConfiguration WMI class or the AddIPAddress WinApi method.

Try this sample which uses the WMI.

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils,
  ActiveX,
  Variants,
  ComObj;

procedure  SetStaticIpAddress(const NetworkCard, IPAddress, Mask :string);
const
  WbemUser    ='';
  WbemPassword='';
  WbemComputer='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator   : OLEVariant;
  FWMIService     : OLEVariant;
  FWbemObjectSet  : OLEVariant;
  FWbemObject     : OLEVariant;
  FOutParams      : OLEVariant;
  vIpAddress      : 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('ReturnValue  %s',[FOutParams]));
  end
  else
  Writeln('Network card not found');
end;


begin
 try
    CoInitialize(nil);
    try
      SetStaticIpAddress('Network card name','192.168.1.1','255.255.255.0');
    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.
like image 149
RRUZ Avatar answered Oct 14 '22 03:10

RRUZ