delphi & netbios

jorgitobush
jorgitobush used Ask the Experts™
on
Hi,

I would like to know if there is a way to obtain a machine name (netbios name) given its ip address in delphi. I tried with client and server socket but got nothing for the moment.
                                           Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

Try this:

uses Winsock;

function GetRemoteHostName(IPAddr : string) : string;
var
  wsdata : TWSAData;
  he : PHostEnt;
  ip : TInAddr;            
begin
  WSAStartup(MakeWord(1, 1), wsdata);
  ip.S_addr := inet_addr(PChar(IPAddr));
  he := gethostbyaddr(@ip.S_un_b, 4, AF_INET);        
  if he<>nil then
    Result := string(he.h_name)
  else
    Result := IntToStr(WSAGetLastError);
  WSACleanup();
end;

Regards, Geo

Author

Commented:
I Apreciatte your answer but that function retrieves de host name as ping -a does, but doesn't return the machine name.
For example : ip : 24.232.109.33 has a machine name of 'SERVER' but the host is OL33-109.fibertel.com.ar . In this case, the function returns 'OL33-109.fibertel.com.ar', not 'SERVER'.                

                                          thanks...
check out the NetServerEnum and WNetEnumResource functions
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Hi all...

After looking for a while I finally found the answer...
Here it is:

To retreive netbios information from a remote machine (machine name, mac address, shares, users logged in and other resources) you must send an UDP packet to that machine (with ip address as input) and receive the response packet.
I did it with NMUDP control setting remote host to the target ip, remote port 137 and local port 137.
Well, I think that the result OL33-109.fibertel.com.ar means that your PC has DNS defined which returns that info.

However, it's nice that you've found the answer yourself.

Regards, Geo
jorgitobush:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial