Forum > Networking and Web Programming

(Windows only) Network interfaces

(1/1)

bobby100:
Hi @ all,

as promised in another topic, here is my unit for reading and setting the network interfaces (IP, Subnetmask etc.). It is a collection of my work and of the knowledge of other people posted all over the internet (Stack Overflow etc.). I am sorry that I didn't take notice where I got what from, so the authors of some pieces of the code will unfortunately remain uncredited.

Usage

Find the network interfaces and feed the names to a Combobox (cbAdapters):

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---GetLocalAdaptersWMI;    if SizeOf(Interfaces) > 0 then    begin      for i := Low(Interfaces) to High(Interfaces) do        cbAdapters.Items.Add(Interfaces[i].Description);    end;  
Check if there is a connection (LAN cable connected, Wi-Fi connected). If not (IP-Adress is empty) - read the last info as saved to registry:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---IsAdapterConnectedWMI(cbAdapters.Items[cbAdapters.ItemIndex],      cbAdapters.ItemIndex);    if trim(Interfaces[cbAdapters.ItemIndex].IP_Address) = '' then      GetLocalAdapterInfoREG(cbAdapters.Items[cbAdapters.ItemIndex],        cbAdapters.ItemIndex); If an adapter is not connected at the moment, you can't get the IP settings through WMI interface, but you can read it from the registry (corresponding function is untRegistry). You should now have the last IP-Address in interfaces[].IP_Address

Use the info we got from Interfaces (cbAdapters is our ComboBox filled with adapter names, lbInfo are Labels and cbInfo9 is a CheckBox):

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  i: integer;begin  i := cbAdapters.ItemIndex;  lbInfo5.Caption := Interfaces[i].MAC;  lbInfo6.Caption := Interfaces[i].IP_Address;  lbInfo7.Caption := Interfaces[i].Subnet_Mask;  lbInfo8.Caption := Interfaces[i].Gateway_Address;  if Interfaces[i].DHCP_Enabled = 1 then    cbInfo9.Checked := True  else    cbInfo9.Checked := False;   if Logging then  begin    Log(GetNDISAdapterTypeString(Interfaces[i].PhysicalMediumType));    Log(GetNetConnectionStatusString(Interfaces[i].ConnectionStatus));  end;end;
Send your settings to an adapter:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  i: integer;  IP, Subnet, Gateway: WideString;  test: TIPv4Rec;begin  try    CoInitialize(nil);    try      if cbDHCP.Checked then //CheckBox for selecting DHCP or static address      begin        SetDynamicIpAddress(cbAdapters.Text); //all the functions are using network interface name (description) for the access        if LastError <> '' then          Log(LastError);        ReleaseDHCP(cbAdapters.Text);        if LastError <> '' then          Log(LastError);        RenewDHCP(cbAdapters.Text);        if LastError <> '' then          Log(LastError);      end      else      begin        IP := TrimSpace(meIPAddr.Text); //MaskedEdits containing our settings        Subnet := TrimSpace(meSubnet.Text);        Gateway := TrimSpace(meGateway.Text);        if (Gateway = '0.0.0.0') or not StrToIPV4(Gateway, test) then          Gateway := '';        SetStaticIpAddress(cbAdapters.Text, IP, Subnet, Gateway);        if LastError <> '' then          Log(LastError);      end;    finally      CoUninitialize;    end;  except    on E: EOleException do      Log(Format('EOleException %s %x', [E.Message, E.ErrorCode]));    on E: Exception do      Log(E.ClassName + ':' + E.Message);  end;end;
I know it isn't so easy to read and use my code because it is ripped from a GUI program, but I hope someone can make a use of it.

If anyone makes a use of it, please leave a note here to feed my ego :)

Remy Lebeau:

--- Quote from: bobby100 on January 09, 2022, 10:33:55 pm ---Find the network interfaces and feed the names to a Combobox (cbAdapters):

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---GetLocalAdaptersWMI;
--- End quote ---

I wonder why you are using WMI for this, instead of using more native system APIs, like GetAdapters(Info|Addresses)?

bobby100:
Hi Remy,

it is easy for you to say (I know that you have the knowledge), but try to get any info if you are beginning from zero. Google for "change ip address programmatically" or similar, and all you will get is about C#, powershell, WMI...
I did found some examples built around native API, but not all I need. For some functions, I needed to use WMI because of the available info. At the end, I've decided to do all over the WMI, and not with a combination of native/WMI.
There was also some info on the net, which APIs will be deprecated in the future versions of Windows, and MS' advice was to use WMI because they will keep the frontend API as is for some time, thus giving me hope that my app will keep working with future versions of Windows for at least some time.
I can't recall all the details, but I can recall that I’ve made some decisions according to such kind of info.

Navigation

[0] Message Index

Go to full version