注册 登录
编程论坛 VFP论坛

获取本机内、外网IP

sych 发布于 2024-05-28 11:51, 396 次点击
代码都是网上抄来的,不知道有坑没有,请大家指教,同事也请大家分享一下自己掌握的简单易用的方法获取内外网IP的代码
IPSocket=CreateObject("MSWinsock.Winsock")
?"内网IP",IPSocket.LocalIP

test=CREATEOBJECT("WinHttp.WinHttpRequest.5.1")
test.OPEN("get","http://www.,.F.)
test.SEND
?"外网IP",test.responsetext

6 回复
#2
csyx2024-05-28 12:05
谢谢,不过我这用 www. 返回的是 ipv6 地址
#3
liuxingang282024-05-28 13:29
若本机有多个 IP,推荐采用下述方法:

LOCAL ;
    oWMI AS OBJECT,;
    oLocal AS OBJECT,;
    oHARDWARE AS OBJECT,;
    object1 AS OBJECT,;
    cIP,;
    cMAC
oWMI = CREATEOBJECT("WbemScripting.SWbemLocator")
oLocal = oWMI.ConnectServer(".", "root\cimv2")
oHARDWARE = oLocal.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled=1")
FOR EACH object1 IN oHARDWARE
    cIP = object1.Properties_('IPAddress').VALUE
    cMAC = object1.Properties_('MACAddress').VALUE
    EXIT
ENDFOR
? 'IP地址:',cIP
? 'MAC地址:',cMAC
#4
sych2024-05-28 14:44
回复 3楼 liuxingang28
一个程序对外通信,对应的IP是唯一的
#5
sych2024-05-28 14:47
回复 2楼 csyx
通过路由器界面查询的也是IPV6?
这倒是个新情况
#6
nbwww2024-05-28 16:15
公网IP查询可以用这个地址:http://

LOCAL zdrole1
zdrole1 = createObject('Microsoft.XMLHTTP')
zdrole1.open('GET' , 'http://' , .f.)
zdrole1.send()
resu_zjb_url =STREXTRACT(zdrole1.responseText,'<h2>','</h2>')
?resu_zjb_url
#7
sych2024-05-29 09:59
获取程序使用的内网IP地址
PROCEDURE getip
LOCAL dwSize, stTable, nTableCount,    dwLocalAddr, dwOwningPid,szLocalAddr,CurrentProcessId,ii
store "0.0.0.0" TO dwLocalAddr
Declare Integer GetCurrentProcessId In kernel32.Dll
DECLARE LONG GetExtendedTcpTable IN "Iphlpapi.dll" STRING@, LONG@, LONG, LONG, LONG, LONG
DECLARE LONG inet_ntoa IN "Ws2_32" LONG
DECLARE LONG ntohs IN "Ws2_32" LONG
DECLARE INTEGER CloseHandle IN kernel32 INTEGER hObject
DECLARE Long GetModuleFileNameEx IN PsApi  Long hProcess, Long hModule, String @ lpExename, Long dwSize
DECLARE Long OpenProcess IN WIN32API Long dwDesiredAccess, Long bInheritHandle, Long dwProcessId
CurrentProcessId=GetCurrentProcessId()
dwSize=0
GetExtendedTcpTable(NULL, @dwSize, 1, 2, 5, 0) && TCP_TABLE_OWNER_PID_ALL
IF m.dwSize > 0
    stTable = REPLICATE(0h0, m.dwSize) && 分配数据空间
    GetExtendedTcpTable(@stTable, @dwSize, 1, 2, 5, 0)
    nTableCount = CTOBIN(SUBSTR(m.stTable, 1, 4), "4Rs")
    IF m.nTableCount > 0
        stTable = STUFF(m.stTable, 1, 4, "")
        FOR ii = 1 TO m.nTableCount
            szLocalAddr = CTOBIN(SUBSTR(m.stTable, 5, 4), "4R")+2^31 && 本地 IP地址
            szLocalAddr = SYS(2600, inet_ntoa(m.szLocalAddr), 16)
            szLocalAddr = LEFT(szLocalAddr, AT(0h00, m.szLocalAddr)-1)
            dwOwningPid = CTOBIN(SUBSTR(m.stTable, 21, 4), "4R")+2^31 && 关联的进程ID
            IF m.CurrentProcessId=m.dwOwningPid
                dwLocalAddr=m.szLocalAddr
                exit
            endif
            stTable = STUFF(m.stTable, 1, 24, "")
        NEXT
    ENDIF
ENDIF
retu m.dwLocalAddr
1