Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

Get IP addr.

I need some code to get the IP addr. from a node in my network (not the node where the program runs)
0
lblack
Asked:
lblack
1 Solution
 
BlackManCommented:
Here's a snip from one of my units:
------------------------- snip --------------------
unit LBSNet;

interface

Uses
  Winsock, Messages,
  Windows, WinSpool, Classes, SysUtils;

Type
  TIPStruct            = Record
                      B1,
                      B2,
                      B3,
                      B4      : Byte;
                    End; (* TIPStruct *)

Procedure GetIPAddr(NodeName : String; Var IP : tIPStruct);
Function GetIPAddrAsString(NodeName : String) : String;
Function GetNodeName : String;
Function GetUserName : String;
Function IPMatch(IP1, IP2 : String) : Boolean;

implementation


Procedure InitWinsock;
Var
  VerReqd      : Word;
  WSAData      : TWSAData;
  WSErr      : Word;
Begin
  VerReqd := $0101;
  WSErr := WSAStartup(VerReqd, WSAData);
End; (* Procedure InitWinsock *)

Procedure GetIPAddr(NodeName : String; Var IP : tIPStruct);
Type
  TAddrList      = Array[0..10] Of ^TInAddr;
Var
  HostEnt      : PHostEnt;
  AddrList      : ^TAddrList;
Begin
  FillChar(IP, SizeOf(IP), 0);
  HostEnt := GetHostByName(PChar(NodeName));
  If HostEnt <> nil Then
  Begin
    AddrList := Pointer(HostEnt.H_Addr_List);

    With AddrList[0]^.S_un_b, IP Do
    Begin
      B1 := Byte(S_B1);
      B2 := Byte(S_B2);
      B3 := Byte(S_B3);
      B4 := Byte(S_B4);
    End; (* With *)
  End; (* If HostEnt <> nil *)
End; (* Procedure GetIPAddr *)

Function GetIPAddrAsString(NodeName : String) : String;
Var
  IP      : tIPStruct;
Begin
  GetIPAddr(NodeName, IP);
  With IP Do
    Result := IntToStr(B1) + '.' + IntToStr(B2) + '.' + IntToStr(B3) +
            '.' + IntToStr(B4);
End; (* Function GetIPAddrAsString *)

Function GetNodeName : String;
Var
  NName      : PChar;
  NNLen      : DWord;
Begin
  NName := StrAlloc(129);
  NNLen := 128;
  GetComputerName(NName, NNLen);
  Result := NName;
  StrDispose(NName);
end;

Function GetUserName : String;
Var
  NName      : PChar;
  NNLen      : DWord;
Begin
  NName := StrAlloc(129);
  NNLen := 128;
  Windows.GetUserName(NName, NNLen);
  Result := NName;
  StrDispose(NName);
end;


Begin
  InitWinSock;
end.

0
 
lblackAuthor Commented:
Quick answer, thanx
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now