Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: FoxPro FAQ

API Functions

API Structures & Pointers - Get Host & IP Example by craigsboyd
Posted: 8 Jul 03 (Edited 18 Feb 05)

Slighthaze = NULL

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

Do DeclareAPIs

Local lcBuffer, lnResult, lcHostname, lcMessage, lcHostentStruct
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)
    If Empty(lcMessage)
        lcMessage = "Sorry, unable to retrieve Local Host and IP"
    Messagebox(lcMessage,64,"Local Host & IP")
    Messagebox("Error returned: " + Transform(lnResult))

    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

Function IPPortion(nPointer)
    Local lnAddress, lcResult
    lnAddress = buf2dword(MemoryBuffer(nPointer, 4))
    Return Iif(lnAddress <> 0, JustIP(MemoryBuffer(lnAddress, 4)), "")

Function JustIP(cBuffer)
    Local lcResult, lnCounter
    lcResult = ""
    For lnCounter=1 To 4
        lcResult = lcResult + Ltrim(Str(Asc(Substr(cBuffer, lnCounter)))) + Iif(lnCounter=4, "",".")
    Return lcResult

Function buf2word (cBuffer)
    Return Asc(Substr(cBuffer, 1,1)) + Asc(Substr(cBuffer, 2,1)) * 256

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)

Function MemoryBuffer(nAddress, nBuffersize)
    Local lcBuffer
    lcBuffer = SPACE(nBuffersize)
    = Heap2Str (@lcBuffer, nAddress, nBuffersize)
    Return lcBuffer

Back to Microsoft: FoxPro FAQ Index
Back to Microsoft: FoxPro Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close