Berbagai Cara untuk Mendapatkan IP Address Lokal dengan Visual Foxpro

ip address

Berikut ini adalah berbagai cara untuk mendapatkan alamat IP lokal dengan visual foxpro. Cara pertama menggunakan MSWinsock, menurut saya ini adalah yang paling simple, untuk mencobanya silahkan save as ke prg code berkut:

IPcomp = CREATEOBJECT('MSWinsock.Winsock.1')
MESSAGEBOX("IP Address Computer Anda : "+CHR(13)+IPcomp.LocalIP,64+0,"IP address")

Cara kedua masih mirip dengan cara pertama yaitu menggunakan wsock32, code programnya cukup panjang dibandingkan cara pertama di atas, tapi return hasilnya sangat cepat. untuk mencobanya silahkan save as ke prg code berikut:


#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

Cara ketiga berbeda dengan cara pertama dan kedua, cara berikut menggunakan ping biasa dengan command prompt yang dijalankan lewat visual foxpro. Untuk mencobanya silahkan save as ke prg code berikut:

Lparameters lcPcName
lcPcName = Getenv("COMPUTERNAME")
cTmp = Getenv("TEMP")
! ping -n 1 &lcPcName -4 >&cTmp\ip.txt
cPing = Filetostr(cTmp + '\ip.txt')
cIp = ''
Delete File cTmp + '\ip.txt'
If At('Reply from',cPing) > 0
	cPosisi=At('Reply from',cPing)+11
	cIp = Substr(cPing,cPosisi,15)
	cIp = Left(cIp,At(':',cIp)-1)
	Messagebox('Your IP Addess is : ' + cIp)
Else
	Messagebox('Failed !',16)
Endif
Return cIp

Kalau mau download semua prg nya di sini. Barangkali teman-teman punya cara lain silahkan ditambahkan, sekian.

2 thoughts on “Berbagai Cara untuk Mendapatkan IP Address Lokal dengan Visual Foxpro

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s