Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Get local ip
Message
From
16/03/2004 11:13:34
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
16/03/2004 10:35:36
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
00885258
Message ID:
00886704
Views:
15
>Try with this...... WithOut COM/OCX
>
>Regards.-
>
>-------------------------------------------------------------------------------------
>SET PROCEDURE TO GetIP.prg
>oo = CREATEOBJECT("GetIP")
>a = oo.GetLocalHostName()
>b = oo.GetHostIP( a )
>? "Local Host:", a
>? "Local IP:", b
>
>DEFINE CLASS GetIP AS CUSTOM
>
> NAME = "GetIP"
>
> PROCEDURE Init
> DECLARE INTEGER WSAStartup IN ws2_32 INTEGER wVerRq, STRING @lpWSAData
> DECLARE INTEGER WSACleanup IN ws2_32
> DECLARE INTEGER gethostbyname IN ws2_32 STRING hostname
>
> DECLARE INTEGER gethostname IN ws2_32;
> STRING @name, INTEGER namelen
>
> DECLARE RtlMoveMemory IN kernel32 As Heap2Str;
> STRING @Dest, INTEGER Src, INTEGER nLength
> ENDPROC
>
> PROCEDURE GetLocalHostName
> *|-- returns the standard host name for the local machine
> #DEFINE SOCKET_ERROR -1
> LOCAL lcBuffer, lnResult
> lcBuffer = SPACE(250)
> lnResult = gethostname (@lcBuffer, Len(lcBuffer))
> RETURN Iif(lnResult=0, SUBSTR(lcBuffer, 1,AT(Chr(0),lcBuffer)-1), "")
> ENDPROC
>
> FUNCTION GetHostIP (lcHostname)
> #DEFINE HOSTENT_SIZE 16
> LOCAL lcHOSTENTptr, lcHOSTENT, lnAddrlistPtr
>
> *|-- address for the HOSTENT structure
> lcHOSTENTptr = gethostbyname(lcHostname)
>
> IF lcHOSTENTptr <> 0
> lcHOSTENT = This.GetMemBuf( lcHOSTENTptr, HOSTENT_SIZE )
> *|-- a pointer to a null-terminated list of addresses
> lnAddrlistPtr = This.buf2dword(SUBSTR(lcHOSTENT, 13,4))
> RETURN This.GetIPfromHOSTENT(lnAddrlistPtr)
> ENDIF
> RETURN ""
> ENDPROC
>
>
> PROCEDURE GetIPfromHOSTENT (lnAddrlistPtr)
> *|-- retrieving IP address from the HOSTENT structure
> LOCAL lnDataAddress, lcResult
> lnDataAddress = This.buf2dword( This.GetMemBuf( lnAddrlistPtr, 4 ) )
> RETURN IIF( lnDataAddress <> 0, This.GetIPAddress( This.GetMemBuf( lnDataAddress, 4 ) ), "" )
> ENDPROC
>
> FUNCTION GetIPAddress (lcAddrBuf)
> *|-- converts 4-characters string buffer
> *|-- to the IP address string representation
> LOCAL lcResult, ii
> lcResult = ""
> FOR ii=1 TO 4
> lcResult = lcResult +;
> LTRIM(STR(Asc(SUBSTR(lcAddrBuf, ii,1)))) +;
> Iif(ii=4, "",".")
> ENDFOR
> RETURN lcResult
> ENDPROC
>
> PROCEDURE buf2dword (lcBuffer)
> RETURN Asc(SUBSTR(lcBuffer, 1,1)) + ;
> Asc(SUBSTR(lcBuffer, 2,1)) * 256 +;
> Asc(SUBSTR(lcBuffer, 3,1)) * 65536 +;
> Asc(SUBSTR(lcBuffer, 4,1)) * 16777216
> ENDPROC
>
> PROCEDURE GetMemBuf (lnAddr, lnBufsize)
> LOCAL lcBuffer
> lcBuffer = REPLICATE(Chr(0), lnBufsize)
> Heap2Str( @lcBuffer, lnAddr, lnBufsize )
> RETURN lcBuffer
> ENDPROC
>
>
>ENDDEFINE

Nice code Roberto :)
I think WSAStartup is missing. I edited part of it as :
  Procedure GetLocalHostName
  *|-- returns the standard host name for the local machine
  #Define SOCKET_ERROR -1
  Local lcBuffer, lnResult, lpWSAData
********************************
  lpWSAData = Space(512)
  WSAStartup(0x0101,@lpWSAData)
********************************
  lcBuffer = Space(250)
  lnResult = gethostname (@lcBuffer, Len(lcBuffer))
  Return Iif(lnResult=0, Substr(lcBuffer, 1,At(Chr(0),lcBuffer)-1), "")
Endproc
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform