Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
USER INTERFACES >>  RETRIEVING THE LOCAL IP ADDRESS

  Boudewijn Lutgerink
  Where is Boudewijn Lutgerink?
 Hoonaardstraat, Driel
 Netherlands
 Boudewijn Lutgerink



Every now and then you might need the local IP address of the machine one application is running on.
I just recently had this situation anyway.
Looking around a bit I found a simple way to do this.
First of all, create an instance of the winsock.ocx as in
osck = CreateObject("MSWinsock.winsock.1")

Then read the LOCALIP property of that object.
osck.localIP

if it returns 127.0.0.1 you can bet you are NOT connected to a network, otherwise it will return the localIP address which can be used for any purpose.

Based on a reply by Bernard Bout after I posted this FAQ I have to say that the winsock control has serious memory leaks which could lead to hanging your machine.
Bernard posted the following code that also retrieves the local IP address for you, without use of winsock controls. Kudos to Bernard for sharing this with us.

Here is the code:
PROCEDURE GetIP
LPARAMETER pcItem
LOCAL lcItem
lcItem = iif( vartype(pcItem)='C', upper(pcItem), 'NONE' )
#DEFINE ERROR_SUCCESS           0
#DEFINE ERROR_NOT_SUPPORTED     50
#DEFINE ERROR_INVALID_PARAMETER 87
#DEFINE ERROR_BUFFER_OVERFLOW   111
#DEFINE ERROR_NO_DATA           232

#DEFINE MAX_ADAPTER_NAME_LENGTH        256
#DEFINE MAX_ADAPTER_DESCRIPTION_LENGTH 128
#DEFINE MAX_ADAPTER_ADDRESS_LENGTH     8

DECLARE INTEGER GetAdaptersInfo IN iphlpapi;
    STRING @pAdapterInfo, LONG @pOutBufLen

LOCAL lcBuffer, lnBufsize
lnBufsize = 0
lcBuffer = ""

* this call usually returns the ERROR_BUFFER_OVERFLOW
* with lnBufsize set to the required amount of memory
GetAdaptersInfo(@lcBuffer, @lnBufsize)

lcBuffer = Replicate(Chr(0), lnBufsize)
IF GetAdaptersInfo(@lcBuffer, @lnBufsize) <> ERROR_SUCCESS
  * still something is wrong
  RETURN ""
ENDIF

*|typedef struct _IP_ADAPTER_INFO {
*|  struct _IP_ADAPTER_INFO* Next;         0:4
*|  DWORD ComboIndex;                      4:4
*|  char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];          8:260
*|  char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]; 268:132
*|  UINT AddressLength;                       400:4
*|  BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH]; 404:8
*|  DWORD Index;                              412:4
*|  UINT Type;                                416:4
*|  UINT DhcpEnabled;                         420:2
*|  PIP_ADDR_STRING CurrentIpAddress;         ?? 4 byte pointer ??
*|  IP_ADDR_STRING IpAddressList;             433:15
*|  IP_ADDR_STRING GatewayList;               
*|  IP_ADDR_STRING DhcpServer;                
*|  BOOL HaveWins;
*|  IP_ADDR_STRING PrimaryWinsServer;
*|  IP_ADDR_STRING SecondaryWinsServer;
*|  time_t LeaseObtained;
*|  time_t LeaseExpires;
*|} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;

* wgcs: Experimentally found indices:
*   NetMask       449:15
*   Gateway       473:15
*   dhcp          513:15
*   Wins Server   557:15
*   

do case
  case lcItem='NONE'
    RETURN STRTRAN(SUBSTR(lcBuffer, 433,15), Chr(0),"")
  case lcItem='IP'
    RETURN STRTRAN(SUBSTR(lcBuffer, 433,15), Chr(0),"")
  OTHERWISE
  	RETURN ""
endcase

FEEDBACK

ali Hosein Zadeh @ 6/6/2007 1:05:07 PM
i have a winsock object in my form that i connected to a server.
when i connect to server and want to get local ip it just return 127.0.0.1
but if i don't connect to server but i 'm on the net it give me the right local ip.
am i use another winsock like you write?

Boudewijn Lutgerink @ 6/6/2007 4:52:25 PM
probably. I do not know what winsock object you use. As you can see by the code I use the simple standard that MS ships.
What you can do is take a look at the OLEClass property of your object. that should guive you a clue what you are using.

rose cruz @ 10/3/2007 3:10:41 AM
Its really a useful program...

haris walet @ 12/6/2007 4:35:40 AM
how to find others ip..?

Solomon David @ 3/12/2015 9:01:55 AM
Used this recently, works wonderfully. Thanks a million !!!

Sanjay Gandhi @ 3/1/2017 6:16:05 AM
Thanks for Sharing...Very Useful code...



Your Name: 
Your Feedback: 

Spam Protection:
Enter the code shown: