编程高手们:请问在VF中要获得本机IP地址的代码是什么?高手知道请指点,本人万分感谢!!!!
由于本人做毕业设计,有关网络管理的软件.遇到难题所以请编程高手们指点指点我.请问在VF中要获得本机IP地址的代码是什么?高手知道请指点,本人万分感谢!!!!...
由于本人做毕业设计,有关网络管理的软件.遇到难题所以请编程高手们指点指点我.请问在VF中要获得本机IP地址的代码是什么?高手知道请指点,本人万分感谢!!!!
展开
1个回答
展开全部
方法一:
ox=CREATEOBJECT("mswinsock.winsock")
?ox.LocalIP
RELEASE OX
方法二:
public IPSocket
crlf=chr(13)+chr(10)
* 显示本地 ip 地址
IPSocket = CreateObject("MSWinsock.Winsock")
if type('IPSocket')='O'
IPAddress = IPSocket.LocalIP
localhostname = IPSocket.localhostname
remotehost = IPSocket.remotehost
remotehostip = IPSocket.remotehostip
MessageBox ("本地 IP = " + IPAddress+crlf+"本地 host = "+localhostname;
+crlf+"Remotehost = "+remotehost+crlf+"Remotehostip = "+remotehostip)
else
MessageBox ("Winsock 未安装!")
endif
方法三:
**获取IP的完全版
DO decl
*| typedef struct _MIB_IPADDRTABLE {
*| DWORD dwNumEntries; // number of entries in the table
*| MIB_IPADDRROW table[ANY_SIZE]; // array of IP address entries
*| } MIB_IPADDRTABLE, *PMIB_IPADDRTABLE; total bytes = 4 + undef
*| typedef struct _MIB_IPADDRROW {
*| DWORD dwAddr; // IP address 0:4
*| DWORD dwIndex; // interface index 4:4
*| DWORD dwMask; // subnet mask 8:4
*| DWORD dwBCastAddr; // broadcast address 12:4
*| DWORD dwReasmSize; // re-assembly size 16:4
*| unsigned short unused1; // not currently used 20:2
*| unsigned short unused2; // not currently used 22:2
*| } MIB_IPADDRROW, *PMIB_IPADDRROW; total bytes = 24
#DEFINE MIB_IPADDRROW_SIZE 24
PRIVATE lWinsockEnabled
lWinsockEnabled = InitWinsock()
LOCAL lcBuffer, lnBufsize, lnEntries, lnIndex, lcEntryBuf
* allocate a space large enough to keep the data
lnBufsize = 8192
lcBuffer = Repli(Chr(0), lnBufsize)
= GetIpAddrTable (@lcBuffer, @lnBufsize, 1)
lnEntries = buf2dword(SUBSTR(lcBuffer, 1,4))
* resulting cursor
CREATE CURSOR cs (ipaddr C(15), iindex N(3), smask C(15),;
bcast C(15), rsize N(12), hostname C(50))
FOR lnIndex = 1 TO lnEntries
lcEntryBuf = SUBSTR(lcBuffer, 5+(lnIndex-1)*MIB_IPADDRROW_SIZE,;
MIB_IPADDRROW_SIZE)
= AddEntry (lcEntryBuf)
ENDFOR
IF lWinsockEnabled
= WSACleanup()
ENDIF
GO TOP
BROW NORMAL NOWAIT
* end of main
PROCEDURE AddEntry (lcEntryBuf)
LOCAL lcIPAddrBuf, lcIPAddr, lcHostName, lnIndex,;
lcMask, lcBCast, lnRSize
* IP address and the host name
lcIPAddrBuf = SUBSTR(lcEntryBuf, 1,4)
lcIPAddr = inet_ntoa(buf2dword(lcIPAddrBuf))
lcHostName = GetHostName(lcIPAddrBuf)
* the index of the interface associated with this IP address
lnIndex = buf2dword(SUBSTR(lcEntryBuf, 5,4))
* the subnet mask for the IP address
lcMask = inet_ntoa(buf2dword(SUBSTR(lcEntryBuf, 9,4)))
* the broadcast address
lcBCast = inet_ntoa(buf2dword(SUBSTR(lcEntryBuf, 13,4)))
* the maximum re-assembly size for received datagrams
lnRSize = buf2dword(SUBSTR(lcEntryBuf, 17,4))
INSERT INTO cs valueS (lcIPAddr, lnIndex, lcMask, lcBCast,;
lnRSize, lcHostName)
RETURN
FUNCTION GetHostName (lcAddrBuf)
#DEFINE AF_INET 2 && internetwork: UDP, TCP, etc.
#DEFINE HOSTENT_SIZE 16
LOCAL lcResult, lnHOSTENTptr, lcHOSTENT
lcResult = ""
IF lWinsockEnabled
lnHOSTENTptr = gethostbyaddr(lcAddrBuf, 4, AF_INET)
IF lnHOSTENTptr <> 0
lcHOSTENT = GetMemBuf (lnHOSTENTptr, HOSTENT_SIZE)
lcResult = GetMemStr(buf2dword(SUBSTR(lcHOSTENT, 1,4)))
ENDIF
ENDIF
RETURN lcResult
FUNCTION InitWinsock()
* Initializing the Winsock service for the application
#DEFINE WSADATA_SIZE 398
#DEFINE WS_VERSION 514 && 0x0202
LOCAL lcWSADATAln, lnInitResult
lcWSADATA = Repli(Chr(0), WSADATA_SIZE)
lnInitResult = WSAStartup (WS_VERSION, @lcWSADATA)
RETURN (lnInitResult = 0)
FUNCTION GetMemBuf (lnAddr, lnBufsize)
* returning data from a memory block
LOCAL lcBuffer
lcBuffer = Repli(Chr(0), lnBufsize)
= Heap2Str (@lcBuffer, lnAddr, lnBufsize)
RETURN lcBuffer
FUNCTION GetMemStr (lnAddr)
* returning data from a memory block
* before the first occurence of a zero byte
LOCAL lcBuffer
lcBuffer = GetMemBuf (lnAddr, 250)
RETURN SUBSTR(lcBuffer, 1, AT(Chr(0),lcBuffer)-1)
FUNCTION buf2dword (lcBuffer)
RETURN Asc(SUBSTR(lcBuffer, 1,1)) + ;
Asc(SUBSTR(lcBuffer, 2,1)) * 256 +;
Asc(SUBSTR(lcBuffer, 3,1)) * 65536 +;
Asc(SUBSTR(lcBuffer, 4,1)) * 16777216
PROCEDURE decl
DECLARE STRING inet_ntoa IN ws2_32 INTEGER in_addr
DECLARE INTEGER WSACleanup IN ws2_32
DECLARE INTEGER WSAStartup IN ws2_32 INTEGER wVerRq, STRING @lpWSAData
DECLARE INTEGER gethostbyaddr IN ws2_32;
STRING addr, INTEGER len, INTEGER type
DECLARE RtlMoveMemory IN kernel32 As Heap2Str;
STRING @Dest, INTEGER Src, INTEGER nLength
DECLARE INTEGER GetIpAddrTable IN iphlpapi;
STRING @pIpAddrTable, INTEGER @pdwSize, INTEGER bOrder
方法四:
LOCAL oWMI AS OBJECT,oLocal AS OBJECT,oHARDWARE AS OBJECT,object1 AS OBJECT,lcCPUID,LcMAC,lcHDID,lcSerial
oWMI=CREATEOBJECT("WbemScripting.SWbemLocator")
oLocal=oWMI.ConnectServer(".", "root\cimv2")
oHARDWARE=oLocal.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled=1")
FOR EACH object1 IN oHARDWARE
LcMAC=object1.Properties_('MACAddress').VALUE
LcIP=object1.Properties_('IPAddress').VALUE
EXIT
ENDFOR
?'网卡MAC地址:',LcMAC
?'IP地址:',LcIP
ox=CREATEOBJECT("mswinsock.winsock")
?ox.LocalIP
RELEASE OX
方法二:
public IPSocket
crlf=chr(13)+chr(10)
* 显示本地 ip 地址
IPSocket = CreateObject("MSWinsock.Winsock")
if type('IPSocket')='O'
IPAddress = IPSocket.LocalIP
localhostname = IPSocket.localhostname
remotehost = IPSocket.remotehost
remotehostip = IPSocket.remotehostip
MessageBox ("本地 IP = " + IPAddress+crlf+"本地 host = "+localhostname;
+crlf+"Remotehost = "+remotehost+crlf+"Remotehostip = "+remotehostip)
else
MessageBox ("Winsock 未安装!")
endif
方法三:
**获取IP的完全版
DO decl
*| typedef struct _MIB_IPADDRTABLE {
*| DWORD dwNumEntries; // number of entries in the table
*| MIB_IPADDRROW table[ANY_SIZE]; // array of IP address entries
*| } MIB_IPADDRTABLE, *PMIB_IPADDRTABLE; total bytes = 4 + undef
*| typedef struct _MIB_IPADDRROW {
*| DWORD dwAddr; // IP address 0:4
*| DWORD dwIndex; // interface index 4:4
*| DWORD dwMask; // subnet mask 8:4
*| DWORD dwBCastAddr; // broadcast address 12:4
*| DWORD dwReasmSize; // re-assembly size 16:4
*| unsigned short unused1; // not currently used 20:2
*| unsigned short unused2; // not currently used 22:2
*| } MIB_IPADDRROW, *PMIB_IPADDRROW; total bytes = 24
#DEFINE MIB_IPADDRROW_SIZE 24
PRIVATE lWinsockEnabled
lWinsockEnabled = InitWinsock()
LOCAL lcBuffer, lnBufsize, lnEntries, lnIndex, lcEntryBuf
* allocate a space large enough to keep the data
lnBufsize = 8192
lcBuffer = Repli(Chr(0), lnBufsize)
= GetIpAddrTable (@lcBuffer, @lnBufsize, 1)
lnEntries = buf2dword(SUBSTR(lcBuffer, 1,4))
* resulting cursor
CREATE CURSOR cs (ipaddr C(15), iindex N(3), smask C(15),;
bcast C(15), rsize N(12), hostname C(50))
FOR lnIndex = 1 TO lnEntries
lcEntryBuf = SUBSTR(lcBuffer, 5+(lnIndex-1)*MIB_IPADDRROW_SIZE,;
MIB_IPADDRROW_SIZE)
= AddEntry (lcEntryBuf)
ENDFOR
IF lWinsockEnabled
= WSACleanup()
ENDIF
GO TOP
BROW NORMAL NOWAIT
* end of main
PROCEDURE AddEntry (lcEntryBuf)
LOCAL lcIPAddrBuf, lcIPAddr, lcHostName, lnIndex,;
lcMask, lcBCast, lnRSize
* IP address and the host name
lcIPAddrBuf = SUBSTR(lcEntryBuf, 1,4)
lcIPAddr = inet_ntoa(buf2dword(lcIPAddrBuf))
lcHostName = GetHostName(lcIPAddrBuf)
* the index of the interface associated with this IP address
lnIndex = buf2dword(SUBSTR(lcEntryBuf, 5,4))
* the subnet mask for the IP address
lcMask = inet_ntoa(buf2dword(SUBSTR(lcEntryBuf, 9,4)))
* the broadcast address
lcBCast = inet_ntoa(buf2dword(SUBSTR(lcEntryBuf, 13,4)))
* the maximum re-assembly size for received datagrams
lnRSize = buf2dword(SUBSTR(lcEntryBuf, 17,4))
INSERT INTO cs valueS (lcIPAddr, lnIndex, lcMask, lcBCast,;
lnRSize, lcHostName)
RETURN
FUNCTION GetHostName (lcAddrBuf)
#DEFINE AF_INET 2 && internetwork: UDP, TCP, etc.
#DEFINE HOSTENT_SIZE 16
LOCAL lcResult, lnHOSTENTptr, lcHOSTENT
lcResult = ""
IF lWinsockEnabled
lnHOSTENTptr = gethostbyaddr(lcAddrBuf, 4, AF_INET)
IF lnHOSTENTptr <> 0
lcHOSTENT = GetMemBuf (lnHOSTENTptr, HOSTENT_SIZE)
lcResult = GetMemStr(buf2dword(SUBSTR(lcHOSTENT, 1,4)))
ENDIF
ENDIF
RETURN lcResult
FUNCTION InitWinsock()
* Initializing the Winsock service for the application
#DEFINE WSADATA_SIZE 398
#DEFINE WS_VERSION 514 && 0x0202
LOCAL lcWSADATAln, lnInitResult
lcWSADATA = Repli(Chr(0), WSADATA_SIZE)
lnInitResult = WSAStartup (WS_VERSION, @lcWSADATA)
RETURN (lnInitResult = 0)
FUNCTION GetMemBuf (lnAddr, lnBufsize)
* returning data from a memory block
LOCAL lcBuffer
lcBuffer = Repli(Chr(0), lnBufsize)
= Heap2Str (@lcBuffer, lnAddr, lnBufsize)
RETURN lcBuffer
FUNCTION GetMemStr (lnAddr)
* returning data from a memory block
* before the first occurence of a zero byte
LOCAL lcBuffer
lcBuffer = GetMemBuf (lnAddr, 250)
RETURN SUBSTR(lcBuffer, 1, AT(Chr(0),lcBuffer)-1)
FUNCTION buf2dword (lcBuffer)
RETURN Asc(SUBSTR(lcBuffer, 1,1)) + ;
Asc(SUBSTR(lcBuffer, 2,1)) * 256 +;
Asc(SUBSTR(lcBuffer, 3,1)) * 65536 +;
Asc(SUBSTR(lcBuffer, 4,1)) * 16777216
PROCEDURE decl
DECLARE STRING inet_ntoa IN ws2_32 INTEGER in_addr
DECLARE INTEGER WSACleanup IN ws2_32
DECLARE INTEGER WSAStartup IN ws2_32 INTEGER wVerRq, STRING @lpWSAData
DECLARE INTEGER gethostbyaddr IN ws2_32;
STRING addr, INTEGER len, INTEGER type
DECLARE RtlMoveMemory IN kernel32 As Heap2Str;
STRING @Dest, INTEGER Src, INTEGER nLength
DECLARE INTEGER GetIpAddrTable IN iphlpapi;
STRING @pIpAddrTable, INTEGER @pdwSize, INTEGER bOrder
方法四:
LOCAL oWMI AS OBJECT,oLocal AS OBJECT,oHARDWARE AS OBJECT,object1 AS OBJECT,lcCPUID,LcMAC,lcHDID,lcSerial
oWMI=CREATEOBJECT("WbemScripting.SWbemLocator")
oLocal=oWMI.ConnectServer(".", "root\cimv2")
oHARDWARE=oLocal.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled=1")
FOR EACH object1 IN oHARDWARE
LcMAC=object1.Properties_('MACAddress').VALUE
LcIP=object1.Properties_('IPAddress').VALUE
EXIT
ENDFOR
?'网卡MAC地址:',LcMAC
?'IP地址:',LcIP
参考资料: http://tech.cixiong.com/t/200601/11/0935709.html
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询