Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get programmatically the LAN servers IP address my application is started from

Posted on 2009-04-07
5
Medium Priority
?
742 Views
Last Modified: 2013-12-03
I want programmatically determine the IP address of the LAN server in a application that is started on a client but whose exe file resides on the server. So I don't want the IP address of the PC the application is running on (client) but is started from (server).
Can somebody help with some code. Delphi is preferred but any other programming language is also appreciated.

Thanks, Bruno

0
Comment
Question by:Bruno Buesser
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 24089493
That will depend - are you starting the app via an UNC path or from a mapped drive?
0
 
LVL 1

Author Comment

by:Bruno Buesser
ID: 24094405
That depends on the customer who installs the application. So I think I should have a solution for both UNC and mapped drive.
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 24097860
For UNC paths, that is quite simple - just extract the server name from that path (i.e. the name between '\\' and the next following backslash) and call 'gethostbyname()' to obtain the IP address. For a mapped drive, things are more difficult. You have to obtain the drive letter first and look that one up from the information returned by 'NetUseEnum()' (see e.g. http://win32.mvps.org/network/nusee.cpp on how to use that) to get the share's UNC path from 'ui2_remote' in 'USE_INFO_2' and proceed as above. Alternatively, you could also do that using 'WNetEnumResource()' (http://win32.mvps.org/network/wnoe_wner.c).
0
 
LVL 1

Author Comment

by:Bruno Buesser
ID: 24226214
That's my Delphi solution:


function GetComputerName: string;
var
  ComputerName: array[0..MAX_COMPUTERNAME_LENGTH+1] of Char;
  Size: DWORD;
begin
  Size:=MAX_COMPUTERNAME_LENGTH+1;
  if Windows.GetComputerNameA(ComputerName, Size) then
     Result:= ComputerName
  else
     Result:= '';
end;
 
function GetHostIP: string;
{ Get IP from Computer my applicatio is started from.
  Returns an empty string in case of errors
 
  ParamStr(0) may have 3 different path types:
  1. Local path:       C:\mydirectory\myapplication.exe
  2. Mapped drive:     O:\<sharename>\mydirectory\myapplication.exe
  3. UNC Network path: \\<servername>\<sharename>\mydirectory\myapplication.exe
}
var
  S: string;
  ServerName: string;
  WSAData: TWSAData;
  HostEnt: PHostEnt;
  Index: Integer;
begin
  ServerName:= '';
 
  S:= ExpandUNCFileName(ParamStr(0));  // Returns also UNC path for mapped drives
  if Pos('\\',S) > 0 then
  begin
    S:= Copy(S,3,Length(S)-3);
    Index:= Pos('\',S);
    if Index > 0 then
      ServerName:= Copy(S,1,Index-1);
  end else
    ServerName:= GetComputerName;
 
 
  if ServerName <> '' then
  begin
    HostEnt:= GetHostByName(PChar(ServerName));
    WSAStartup(2, WSAData);
    try
      with HostEnt^ do
        Result:= Format('%d.%d.%d.%d',[Byte(h_addr^[0]),Byte(h_addr^[1]),Byte(h_addr^[2]),Byte(h_addr^[3])]);
    finally
      WSACleanup;
    end;
  end else
    Result:= '';
end;

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 24226669
Thanks for sharing!
0

Featured Post

The Ideal Solution for Multi-Display Applications

Check out ATEN’s VS1912 12-Port DP Video Wall Media Player at InfoComm 2017. Kerri describes how easy it is to design creative video walls in asymmetric layouts and schedule detailed playlists ahead of time with its advanced scheduling feature.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question