当前位置: 代码迷 >> PB >> pb 获取ip地址解决方法
  详细解决方案

pb 获取ip地址解决方法

热度:46   发布时间:2016-04-29 08:03:29.0
pb 获取ip地址
Delphi(Pascal) code
forwardglobal type n_adapters from nonvisualobjectend typeend forwardglobal type n_adapters from nonvisualobjectend typeglobal n_adapters n_adapterstype prototypesFunction ulong GetLastError( &    ) Library "kernel32.dll"Function ulong FormatMessage( &    ulong dwFlags, &    ulong lpSource, &    ulong dwMessageId, &    ulong dwLanguageId, &    Ref string lpBuffer, &    ulong nSize, &    ulong Arguments &    ) Library "kernel32.dll" Alias For "FormatMessageA"Function ulong GetAdaptersInfo ( &    Ref IP_ADAPTER_INFO pAdapterInfo[], &    Ref ulong pOutBufLen &    ) Library "iphlpapi.dll" Alias For "GetAdaptersInfo"Subroutine CopyMemory ( &    Ref blob Destination, &    long Source[], &    long Length &    ) Library  "kernel32.dll" Alias For "RtlMoveMemory"Subroutine CopyMemory ( &    Ref integer Destination, &    blob Source, &    long Length &    ) Library  "kernel32.dll" Alias For "RtlMoveMemory"end prototypesforward prototypespublic function string of_getlasterror ()public function string of_nbr2hex (unsignedlong aul_number, integer ai_digit)public function boolean of_getadaptersinfo (ref string as_macaddress[], ref string as_description[], ref string as_adaptername[], ref string as_ipaddress[])public function string of_getaddr ()end prototypespublic function string of_getlasterror ();// SCRIPT:     n_adapters.of_GetLastError//// PURPOSE:    This function returns the last Windows API error.//// RETURN:     Error message text//// -----------------------------------------Constant ULong FORMAT_MESSAGE_FROM_SYSTEM = 4096Constant ULong LANG_NEUTRAL = 0String ls_buffer, ls_errmsgULong lul_errorlul_error = GetLastError()ls_buffer = Space(255)FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, &        lul_error, LANG_NEUTRAL, ls_buffer, 255, 0)ls_errmsg = "Error# " + String(lul_error) + "~r~n~r~n" + ls_bufferReturn ls_errmsgend functionpublic function string of_nbr2hex (unsignedlong aul_number, integer ai_digit);// SCRIPT:     n_adapters.of_Nbr2Hex//// PURPOSE:    This function converts a number to a hex string.//// ARGUMENTS:  aul_number    - A number to convert//                    ai_digit        - The number of hex digits expected//// RETURN:     Hex string//// -----------------------------------------ULong lul_temp0, lul_temp1Char lc_retIf ai_digit > 0 Then   lul_temp0 = Abs(aul_number / (16 ^ (ai_digit - 1)))   lul_temp1 = lul_temp0 * (16 ^ (ai_digit - 1))   If lul_temp0 > 9 Then      lc_ret = Char(lul_temp0 + 55)   Else      lc_ret = Char(lul_temp0 + 48)   End If   Return lc_ret + of_Nbr2Hex(aul_number - lul_temp1, ai_digit - 1)End IfReturn ""end functionpublic function boolean of_getadaptersinfo (ref string as_macaddress[], ref string as_description[], ref string as_adaptername[], ref string as_ipaddress[]);// SCRIPT:     n_adapters.of_GetAdaptersInfo//// PURPOSE:    This function returns information about network adapters.//// ARGUMENTS:  as_macaddress    - Array of MAC addresses//                    as_description    - Array of Descriptions//                    as_adaptername    - Array of Adapter names//                    as_ipaddress    - Array of IP addresses//// -----------------------------------------Constant ULong ERROR_SUCCESS = 0Constant ULong ERROR_BUFFER_OVERFLOW = 111IP_ADAPTER_INFO lstr_Adapter[]ULong lul_OutBufLenInteger li_idx, li_max, li_macidx, li_macmax, li_byteBlob lblob_address// call function to get buffer sizeIf GetAdaptersInfo(lstr_Adapter, lul_OutBufLen) = ERROR_BUFFER_OVERFLOW Then    // how many adapters?    li_max = lul_OutBufLen / 640    // allocate structure    lstr_Adapter[li_max].NextPtr = 0    // call function to get data    If GetAdaptersInfo(lstr_Adapter, lul_OutBufLen) = ERROR_SUCCESS Then        For li_idx = 1 To li_max            // get adapter information            as_description[li_idx] = String(lstr_Adapter[li_idx].Description)            as_adaptername[li_idx] = String(lstr_Adapter[li_idx].AdapterName)            as_ipaddress[li_idx]   = String(lstr_Adapter[li_idx].IpAddressList.IpAddress)            // convert MAC address from blob data            li_macmax = lstr_Adapter[li_idx].AddressLength            lblob_address = Blob(Space(li_macmax))            CopyMemory(lblob_address, lstr_Adapter[li_idx].Address, li_macmax)            For li_macidx = 1 To li_macmax                CopyMemory(li_byte, BlobMid(lblob_address, li_macidx, 1), 1)                as_macaddress[li_idx] += of_Nbr2Hex(li_byte, 2)                If li_macidx < li_macmax Then                    as_macaddress[li_idx] += "-"                End If            Next        Next        Return True    End IfEnd IfMessageBox("Error in of_GetAdaptersInfo", &                of_GetLastError(), StopSign!)Return Falseend functionpublic function string of_getaddr ();String ls_macaddress[], ls_description[]String ls_adaptername[], ls_ipaddress[]Integer li_cnt, li_max, li_rowboolean lb_oklb_ok=of_GetAdaptersInfo(ls_macaddress, ls_description, &                    ls_adaptername, ls_ipaddress)if lb_ok=false then return ""li_max = UpperBound(ls_macaddress)For li_cnt = 1 To li_max    if right(ls_ipaddress[li_cnt],2)<>".0" then        return ls_ipaddress[li_cnt];    end ifNextreturn ""end functionon n_adapters.createcall super::createTriggerEvent( this, "constructor" )end onon n_adapters.destroyTriggerEvent( this, "destructor" )call super::destroyend on
  相关解决方案