VB uses API to obtain the IP address of the Local Machine

Source: Internet
Author: User
Tags socket error
Option explicitpublic const min_sockets_reqd as long = 1 Public const between as long = & h101public const between as long = Hangzhou \ & h100 and & HFF & Public const ws_version_minor as long = between and & HFF & public const socket_error as long =-1 Public const error_success as long = 0 public const Limit = 257 public const Limit = 129 public const max_wsadelimit = 256 public const Limit = 128 public type wsadatawversion as integerwhighversion as integerszdescription (0 to max_wsadeworkflow) as byteszsystemstatus (0 to max_wsasysstatus) as alias as longend typetype alias as integerszdescription as string * alias as stringend typepublic type hostenthname as longhaliases as longhaddrtype as integerhlen as integerhaddrlist longend typedeclare function wsastartupinfo lib "wsock32" alias "wsastartup" (byval wversionrequested as integer, lpwsadata as wsadatainfo) as longdeclare function wsacleanup lib "wsock32" () as longdeclare function using lib "wsock32" () as longdeclare function wsastartup lib "wsock32" (byval defined as long, lpwsadata as wsadata) as longdeclare function gethostname lib "wsock32" (byval szhost as string, byval dwhostlen as long) as longdeclare function gethostbyname lib "wsock32" (byval szhost as string) as longdeclare sub copymemoryip lib "Kernel32" alias "rtlmovememory" (hpvdest as any, byval hpvsource as long, byval cbcopy as long) 'Get the local IP address public function getipaddress () as stringdim shostname as string * 256dim lphost as longdim host as hostentdim dwipaddr as longdim tmpipaddr () as bytedim I as integerdim sipaddr as stringif not socketsinitialize () thengetipaddress = "" Exit functionend ifif gethostname (shostname, 256) = socket_error thengetipaddress = "msgbox" Windows Sockets error "& STR $ (wsagetlasterror () &" has occurred. unable to successfully get host name. "socketscleanupexit functionend ifshostname = trim $ (shostname) lphost = gethostbyname (shostname) If lphost = 0 thengetipaddress =" "msgbox" Windows Sockets are not responding. "&" unable to successfully get host name. "socketscleanupexit functionend ifcopymemoryip host, lphost, Len (host) copymemoryip dwipaddr, host. haddrlist, 4 redim tmpipaddr (1 to host. hlen) copymemoryip tmpipaddr (1), dwipaddr, host. hlenfor I = 1 to host. hlensipaddr = sipaddr & tmpipaddr (I )&". "nextgetipaddress = mid $ (sipaddr, 1, Len (sipaddr)-1) socketscleanupend functionpublic function getiphostname () as stringdim shostname as string * 256if not socketsinitialize () authorization = "" Exit functionend ifif gethostname (shostname, 256) = socket_error thengetiphostname = "" msgbox "Windows Sockets error" & STR $ (wsagetlasterror () & "has occurred. unable to successfully get host name. "socketscleanupexit functionend ifgetiphostname = left $ (shostname, instr (shostname, CHR (0)-1) socketscleanupend functionpublic function hibyte (byval wparam as integer) hibyte = wparam \ & h100 and & HFF & End functionpublic function lobyte (byval wparam as integer) lobyte = wparam and & HFF & End functionpublic sub socketscleanup () If wsacleanup () <> error_success thenmsgbox "socket error occurred in cleanup. "End ifend subpublic function evaluate () as booleandim wsad as wsadatadim slobyte as stringdim shibyte as stringif wsastartup (random, wsad) <> define thenmsgbox" the 32-bit windows socket is not responding. "socketsinitialize = falseexit functionend ifif wsad. wmaxsockets <min_sockets_reqd thenmsgbox "this application requires a minimum of" & CSTR (min_sockets_reqd) & "supported sockets. "socketsinitialize = falseexit functionend ifif lobyte (wsad. wversion) <ws_version_major or (lobyte (wsad. wversion) = ws_version_major and hibyte (wsad. wversion) <ws_version_minor) thenshibyte = CSTR (hibyte (wsad. wversion) slobyte = CSTR (lobyte (wsad. wversion) msgbox "Sockets version" & slobyte &". "& shibyte &" is not supported by 32-bit Windows Sockets. "socketsinitialize = falseexit functionend ifsocketsinitialize = trueend functionsub main () msgbox getipaddress," ip address "endend sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.