Bruno Buesser
asked on
How to get programmatically the LAN servers IP address my application is started from
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
Can somebody help with some code. Delphi is preferred but any other programming language is also appreciated.
Thanks, Bruno
That will depend - are you starting the app via an UNC path or from a mapped drive?
ASKER
That depends on the customer who installs the application. So I think I should have a solution for both UNC and mapped drive.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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;
Thanks for sharing!