Search code examples
delphidelphi-xe

Delphi - mapped drive ipaddress


How can I find the IP Address of a mapped drive using Delphi. My current system used a mapped drive to connect to the Pervasive database. I am in the process of switching to PostgreSQL and need the IP Address of the mapped drive to connect to the PostgreSQL server.

I Have a mapped a folder (Y:) on a machine named HP. Passing Y: to WNetGetConnection returns the machine name HP but when I use this value in GetHostName it returns the detail of the local machine.

I am using XE7 on Windows10

  LPathStr:=MAX_PATH;
  If WNetGetConnectionA(PAnsiChar('Y:'), PathStr, LPathStr) = 0 then
  begin
    if GetHostName(PathStr,SizeOf(PathStr)) = 0 then
    begin
      varHostEnt := GetHostByName(PathStr);
      varTInAddr.S_addr := u_long(pu_long(varHostEnt^.h_addr_list^)^);
      IpAddr := inet_ntoa(varTInAddr);
      HostName := PathStr;
    end

Solution

  • You can use WNetGetConnection() or WNetGetUniversalName() to resolve a network-mapped drive letter/path to its network UNC path, and then you can use a DNS query (gethostbyname() or better getaddrinfo()) to get the IP address of the server name specified in the UNC path.