Recent

Author Topic: Looking for all network resources  (Read 10559 times)

jpremor

  • New Member
  • *
  • Posts: 11
Looking for all network resources
« on: February 03, 2010, 05:44:14 pm »
Hi all!!

i'm new on forum but using lazarus with wince and inet for some days!!

i searching how to find all IP on the device. Looking for some code on the net i found this (sorry about the portuguese variables and coments) which uses Winsock:

Code: [Select]
       if WSAStartup($0101, GInitData) <> 0 then FormPrinc.Label11.Caption := 'Nenhum add'
        else begin

GetHostName(TempBuf, SizeOf(TempBuf));
           FormPrinc.Label11.Caption := TempBuf + ' : ';
        // busca ip
         phe :=GetHostByName(TempBuf);
        if phe = nil then begin

       end
         else begin
         i := 0;
       pptr := PaPInAddr(Phe^.h_addr_list);
while pptr^[i] <> nil do begin
        FormPrinc.Label11.Caption := FormPrinc.Label11.Caption + StrPas(inet_ntoa(pptr^[i]^));
        Inc(i);
end;
WSACleanup;
         end;

end; // se conseguiu pegar winsock


thats ok, but it seems to result just on the first network adapter found. Normally smartphones have more than one (Active sync, wireless, 3G)...

anyone knows if there is possible to list all adapters and look for all ips? If there is no such functions ready to do this i'll be pleased to help to develop this. The problem is: I dont know where to start...

i found some code to search for device list for lazarus and delphi, but on wince there is some functions missing.

Code: [Select]
type
 PNetResourceArray = ^TNetResourceArray;
 TNetResourceArray = array[0..100] of TNetResource;

function CreateNetResourceList(ResourceType: DWord;
                             NetResource: PNetResource;
                             out Entries: DWord;
                             out List: PNetResourceArray): Boolean;
var
 EnumHandle: THandle;
 BufSize: DWord;
 Res: DWord;
begin
 Result := False;
 List := Nil;
 Entries := 0;
 if WNetOpenEnum(RESOURCE_GLOBALNET,
                 ResourceType,
                 0,
                 NetResource,
                 EnumHandle) = NO_ERROR then begin
   try
     BufSize := $4000;  // 16 kByte
     GetMem(List, BufSize);
     try
       repeat
         Entries := DWord(-1);
         FillChar(List^, BufSize, 0);
         Res := WNetEnumResource(EnumHandle, Entries, List, BufSize);
         if Res = ERROR_MORE_DATA then
         begin
           ReAllocMem(List, BufSize);
         end;
       until Res <> ERROR_MORE_DATA;
       Result := Res = NO_ERROR;
       if not Result then
       begin
         FreeMem(List);
         List := Nil;
         Entries := 0;
       end;
     except
       FreeMem(List);
       raise;
     end;
   finally
     WNetCloseEnum(EnumHandle);
   end;
 end;
end;

In this case WNetEnumResource (dont know the origin) should do the work of WSAStartup (winsock).

Any ideia??

thanks!

« Last Edit: February 03, 2010, 05:48:11 pm by jpremor »

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2583
Re: Looking for all network resources
« Reply #1 on: February 04, 2010, 12:21:30 pm »
I used to use the method similar to your last one, till I found out that is fails on some windows versions. Then I swithed to the following (WSAstartup/Cleanup is done once globally, so not here)
Code: Pascal  [Select][+][-]
  1. var
  2.   HostName: array[byte] of Char;
  3.   res, work: PAddrInfo;
  4. begin
  5.   if gethostname(Hostname, Length(Hostname)) <> 0
  6.   then begin
  7.     // some error
  8.     Exit;
  9.   end;
  10.  
  11.   if getaddrinfo(Hostname, nil, nil, res) <> 0
  12.   then begin
  13.     // some error
  14.     Exit;
  15.   end;
  16.  
  17.   work := res;
  18.   while work <> nil do
  19.   begin
  20.     DoSomethingWith(PSockAddrIn(work^.ai_addr)^.sin_Addr.S_addr)..
  21.  
  22.     work := work^.ai_next;
  23.   end;
  24.  
  25.   freeaddrinfo(res);
  26. end;
  27.  
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

jpremor

  • New Member
  • *
  • Posts: 11
Re: Looking for all network resources
« Reply #2 on: February 04, 2010, 01:45:30 pm »
Hi marc,

Yesterday i read some information on microsoft site about winsock functions and noticed that this functions are Obsolete, as you said. I'll do this change today, but expecting the same result.

Thanks!
« Last Edit: February 04, 2010, 01:48:36 pm by jpremor »

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2583
Re: Looking for all network resources
« Reply #3 on: February 04, 2010, 05:43:05 pm »
Which functions were obsolete ?
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

jpremor

  • New Member
  • *
  • Posts: 11
Re: Looking for all network resources
« Reply #4 on: February 11, 2010, 06:27:33 pm »

Marc,

sorry i made some confusion here. The obsolete function that i saw does not have anything to do with winsock. Sorry.

Here is the function which works...



Code: [Select]

uses
  winsock


// **********************************
// find local ip
procedure TFormPrinc.BtnUpdateIPClick(Sender: TObject);
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
TempStr : string;
TempBuf : array [0..63] of char;
    phe: PHostEnt;
    pptr: PaPInAddr;
    i : integer;
    IPstr : string;
    GInitData: TWSADATA;
begin

    FormPrinc.LabelIpLocal.Caption := '';

    TempStr := '';
    if WSAStartup($0101, GInitData) <> 0 then FormPrinc.LabelIpLocal.Caption := 'No IP found'
    else begin
      GetHostName(TempBuf, SizeOf(TempBuf));
      // find ip
      phe :=GetHostByName(TempBuf);
      if phe = nil then begin end
      else begin
        i := 0;
        pptr := PaPInAddr(Phe^.h_addr_list);
        IPstr := '';
        while pptr^[i] <> nil do begin
          IPstr := StrPas(inet_ntoa(pptr^[i]^));

          // add IPstr where you need...
          Memo1.Add(IPstr);

          Inc(i);
        end;
        WSACleanup;
      end;

    end; // if it was possible to open winsock
end;



 

TinyPortal © 2005-2018