[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1520
  • Last Modified:

how to network connections on delphi

how to enum the network connections (Local Area Connection, etc) from my windows using Delphi 7 code? It has to work on XP/Vista
0
WebWolf1
Asked:
WebWolf1
  • 3
  • 2
1 Solution
 
WebWolf1Author Commented:
it doenst compile on my delphi 7

[Error] Unit1.pas(50): Constant object cannot be passed as var parameter

dwResult := WNetEnumResource(hEnum, cEntries, lpnrDrv, cbBuffer);
0
 
WebWolf1Author Commented:
i now see the comments to compile it on delphi 7 but it doesnt does what I want.
it display all active connections like X:\    \\server\files\... etc

I want to enum local area connections like

Local Area Connection
Local Area Connection 2
Wireless Network Connection
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
JohnjcesCommented:
The best way to do this is to find and download the JEDI IPHelper units, IPHlpAPI.zip which are apparently now part of the JEDI Windows API library. The pas file needed was called IpHlpAPI.pas but is now found as JwIPHlpAPI.pas and associated files.

Later today I will put the original IpHlpAPI.zip file up on my FTP server with other added JEDI units that do some neat stuff and provide the link.

Anyway, in IpFunctions.Pas (a unit found in the demo) there is VVGetAdaptersInfo and this will enumerate all of your network adapters

The code I below is below:

It uses:
IPExport, IPHlpApi, IpTypes, IpFunctions;

My function below is cut from one of my apps, SO... look through it as there may be stuff such as booleans that you don't have/need which will cause an error. This will get you going.

Again, I will post the units on my site later today for download. Can't do it now.

John

uses
IPExport, IPHlpApi, IpTypes, IpFunctions;
 
procedure TFPCClusterMain.GetAdaptersInfo(AddtoMemo : Boolean);
var
  PAdapter, PMem: PipAdapterInfo;
  pPerAdapter: PIpPerAdapterInfo;
  PIPAddr: PIpAddrString;
  OutBufLen, OutBufLen2: ULONG;
  s : string;
  i : integer;
begin
 NicIndex.Clear;
 IPAddresses.Clear;
 if AddToMemo then
  Memo1.Lines.Add('Network Adapter Information');
  VVGetAdaptersInfo(PAdapter, OutBufLen);
  PMem := PAdapter;
  try
    while PAdapter <> nil do
      begin
       if AddToMemo then
        begin
         Memo1.Lines.Add('*********************************');
         Memo1.Lines.Add('Adapter Name: ' + PAdapter.AdapterName);
         Memo1.Lines.Add('Description: ' + PAdapter.Description);
         s := '';
        end;
        for i := 0 to PAdapter.AddressLength do
          s := s + Format('%1d', [PAdapter.Address[i]]);
        NicIndex.Add(IntToStr(PAdapter.Index));
        if AddToMemo then
         begin
          Memo1.Lines.Add('Type:  ' + IntToStr(PAdapter.Type_));
          Memo1.Lines.Add('Index: ' + IntToStr(PAdapter.Index));
         end;
        if PAdapter.DhcpEnabled = 0 then
         begin
          DHCPOn := False;
          if AddToMemo then
           Memo1.Lines.Add('DHCP Not Enabled - Good!')
         end
           else
            begin
             DHCPOn := True;
             if AddToMemo then
              Memo1.Lines.Add('DHCP Enabled - You MUST Disable DHCP');
            end;
        PIPAddr := @PAdapter.IpAddressList;
        repeat
         IPAddresses.Add( PIPAddr.IpAddress.S );
         if AddToMemo then
          begin
           Memo1.Lines.Add('Current IP Address ' + PIPAddr.IpAddress.S);
           Memo1.Lines.Add('Current Subnet Mask ' + PIPAddr.IpMask.S);
          end;
          PIPAddr := PIPAddr.Next;
        until PIPAddr = nil;
        try
          VVGetPerAdapterInfo(PAdapter.Index, pPerAdapter,OutBufLen2);
          if pPerAdapter <> nil then
            try
              PIPAddr := pPerAdapter.CurrentDnsServer;
              while Assigned(PIPAddr) and AddToMemo do
              begin
                Memo1.Lines.Add('Current DNS Server: ' + PIPAddr^.IpAddress.S);
                Memo1.Lines.Add('Current DNS Server Mask: ' + PIPAddr^.IpMask.S);
                PIPAddr := PIPAddr.Next;
              end;
              PIPAddr := @pPerAdapter.DnsServerList;
              while Assigned(PIPAddr) and AddToMemo do
              begin
                Memo1.Lines.Add('DNS Server IP Address: ' + pPerAdapter.DnsServerList.IpAddress.S);
                PIPAddr := PIPAddr.Next;
              end;
            finally
              Freemem(pPerAdapter,OutBufLen2);
            end;
        except
          on E:EIpHlpError do
            ShowMessage(E.Message);
        end;
        PAdapter := PAdapter.Next;
      end;
  finally
    if PAdapter <> nil then
      Freemem(PMem, OutBufLen);
  end;
end;

Open in new window

0
 
WebWolf1Author Commented:
your solution doesn't work. it doesnt return the network connection information, Local Area Connection 1, etc but the devices:


Network Adapter Information
*********************************
Adapter Name: {5DEC8721-BF41-44E2-A2A0-A6C8DF99B401}
Description: ADMtek AN983 based ethernet adapter   <----
Type:  6

etc....


ANYONE ELSE PLEASE?
0
 
JohnjcesCommented:
Well... with this information,

{5DEC8721-BF41-44E2-A2A0-A6C8DF99B401}

search your registry and you will find under

HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{5DEC8721-BF41-44E2-A2A0-A6C8DF99B401}\Connection\Name

the name such Local Area Connection 1 or whatever the Label is for that adapter.

Should be a piece of cake!

John
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now