How to get IP Address using Foxpro Command
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 = Create Object(\"MSWinsock.winsock.1\")
Â
Then read the LOCAL IP 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 \"\"