vfp中关于取本地 IP的问题?
LOCAL ipsocket
ipsocket=CREATEOBJECT("MSWinsock.Winsock")
IF TYPE("ipsocket")="O"
WITH ipsocket &&as MSWinsockLib.Winsock
ipaddress=.LocalIP
localhost=.LocalHostName
ENDWITH
MESSAGEBOX("你的IP地址是:"+ipaddress+CHR(13)+CHR(10)+"你的服务器名是:"+localhost)
ELSE
MESSAGEBOX("系统未装winsock控件")
ENDIF
RELEASE ipsocket
在我本机上使用正常,安装到另外 一台机器上出错,提示:找不到MSWinsock.Winsock类定义。怎么办?多谢各位了。
[解决办法]
以前用的一段代码,复制保存为getip.prg
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 24PRIVATE lWinsockEnabled lWinsockEnabled = InitWinsock()LOCAL lcBuffer, lnBufsize, lnEntries, lnIndex, lcEntryBuf * allocate a space large enough to keep the data lnBufsize = 8192lcBuffer = 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) ENDFORIF lWinsockEnabled = WSACleanup() ENDIF*!* GO TOP *!* BROW NORMAL NOWAIT * end of mainPROCEDURE 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) RETURNFUNCTION 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 lcResultFUNCTION 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 lcBufferFUNCTION 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)) * 16777216PROCEDURE 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