Slighthaze = [color blue]NULL[/color]
[img http://www.sweetpotatosoftware.com/ttimages/hostandip.gif]
The neatest thing about this example isn't really what it returns (there are less painful ways to get the Local Host and IP), but that it shows you an example of how to handle an API call that returns a pointer to a structure - notice for instance the line "lnResult = gethostbyname(lcHostname)" in the code below. (Cut-N-Paste this code into a PRG file and run it from within VFP.)
Special thanks to FoxWiki - especially wgcs and the late Ed Rauh - for the stuff I learned from a couple of pages out there regarding the use of structures & pointers in VFP, and as always to http://www.news2news.com/vfp/ for their VFP Declare Statements. I couldn't have pieced this thing together without you.
#Define WSADATA_SIZE 398
#Define WS_VERSION 514
#Define HOSTBUFFER_SIZE 256
#Define HOSTENT_STRUCT_SIZE 16
Do DeclareAPIs
Local lcBuffer, lnResult, lcHostname, lcMessage, lcHostentStruct
lcBuffer = SPACE(WSADATA_SIZE)
lnResult = WSAStartup(WS_VERSION, @lcBuffer)
lcMessage = ""
If lnResult = 0
lcBuffer = Replicate(CHR(0),HOSTBUFFER_SIZE)
lnResult = gethostname(@lcBuffer,HOSTBUFFER_SIZE)
If lnResult = 0
lcHostname = STRTRAN(lcBuffer,CHR(0),"")
lcMessage = "HOST: " + lcHostname + Chr(13)
lnResult = gethostbyname(lcHostname)
If lnResult != 0
lcHostentStruct = MemoryBuffer(lnResult, HOSTENT_STRUCT_SIZE)
lnResult = buf2dword(Substr(lcHostentStruct, 13,4))
If lnResult != 0
lcMessage = lcMessage + "LOCAL IP ADDRESS: " + IPPortion(lnResult)
Endif
Endif
Endif
If Empty(lcMessage)
lcMessage = "Sorry, unable to retrieve Local Host and IP"
ENDIF
Messagebox(lcMessage,64,"Local Host & IP")
=WSACleanup()
Else
Messagebox("Error returned: " + Transform(lnResult))
ENDIF
**************************
PROCEDURE DeclareAPIs
**************************
Declare Integer WSAStartup In wsock32 Integer wVerRq, String @lpWSAData
Declare Integer WSACleanup In wsock32
Declare Integer gethostname In wsock32 String @Name, Integer namelen
Declare Integer gethostbyname In wsock32 String HostName
Declare RtlMoveMemory In kernel32 As Heap2Str String @, Integer, Integer
Endproc
**************************
Function IPPortion(nPointer)
**************************
Local lnAddress, lcResult
lnAddress = buf2dword(MemoryBuffer(nPointer, 4))
Return Iif(lnAddress <> 0, JustIP(MemoryBuffer(lnAddress, 4)), "")
Endfunc
**************************
Function JustIP(cBuffer)
**************************
Local lcResult, lnCounter
lcResult = ""
For lnCounter=1 To 4
lcResult = lcResult + Ltrim(Str(Asc(Substr(cBuffer, lnCounter)))) + Iif(lnCounter=4, "",".")
Endfor
Return lcResult
Endfunc
**************************
Function buf2word (cBuffer)
**************************
Return Asc(Substr(cBuffer, 1,1)) + Asc(Substr(cBuffer, 2,1)) * 256
Endfunc
**************************
Function buf2dword(cBuffer)
**************************
Return Asc(Substr(cBuffer, 1,1)) + ;
BitLShift(Asc(Substr(cBuffer, 2,1)), 8) +;
BitLShift(Asc(Substr(cBuffer, 3,1)), 16) +;
BitLShift(Asc(Substr(cBuffer, 4,1)), 24)
Endfunc
**************************
Function MemoryBuffer(nAddress, nBuffersize)
**************************
Local lcBuffer
lcBuffer = SPACE(nBuffersize)
= Heap2Str (@lcBuffer, nAddress, nBuffersize)
Return lcBuffer
Endfunc