123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- // 文 件 名:IpAddress.rul
- // 脚本功能:获取计算机主机名和IP地址
- // 编写日期:2015-01-08
- // 使用方法:
- // 1. 在 setup.rul 前面加入 #include "IpAddress.rul"
- // 2. 调用函数 fGetIpAddress(szHost, szIPAdress)
- prototype NUMBER WSOCK32.WSAStartup( NUMBER, POINTER);
- prototype NUMBER WSOCK32.gethostbyname(BYREF STRING);
- prototype NUMBER WSOCK32.gethostname(BYREF STRING, NUMBER);
- prototype NUMBER KERNEL32.RtlMoveMemory (POINTER, NUMBER, NUMBER);
- prototype NUMBER WSOCK32.WSACleanup();
- prototype fGetIpAddress(BYREF STRING, BYREF STRING);
- typedef WSADATA
- begin
- INT wversion;
- INT wHighVersion;
- STRING szDescription[255];
- STRING szSystemStatus[128];
- INT iMaxSockets;
- INT iMaxUdpDg;
- INT lpszVendorInfo;
- end;
- typedef HOSTENT
- begin
- NUMBER hName; // As Long
- NUMBER hAliases; // As Long
- SHORT hAddrType; //As Integer
- SHORT hLength; // As Integer
- NUMBER hAddrList; // As Long
- end;
- typedef IPADDRESS
- begin
- CHAR b1; //First
- CHAR b2; //Second
- CHAR b3; //Third
- CHAR b4; //Fourth
- end;
- function fGetIpAddress(szHost, szIPAdress)
- WSADATA StructWSAData;
- WSADATA POINTER pStructWSAData;
- HOSTENT StructHostEnt;
- HOSTENT POINTER pStructHostEnt;
- NUMBER nResult;
- NUMBER pHostAddress;
- POINTER pHostIPAddress;
- NUMBER HostIPAddress;
- IPADDRESS IpAddress;
- IPADDRESS POINTER pIpAddress;
- STRING sb1, sb2, sb3, sb4;
- NUMBER nStartUp;
- BOOL bReturn;
- begin
- // 加载dll库;
- if UseDLL(WINSYSDIR^"WSOCK32.DLL") < 0 then
- return FALSE;
- endif;
-
- pStructWSAData = &StructWSAData;
- pStructHostEnt = &StructHostEnt;
- // 初始化网络环境;
- nStartUp = WSAStartup(0x101, pStructWSAData);
-
- if nStartUp != 0 then
- return FALSE;
- endif;
-
- // 获取计算机名;
- nResult = gethostname(szHost, 256);
- if ( nResult != 0 ) then
- return FALSE;
- endif;
-
- // 根据计算机名获取ip;
- pHostAddress = gethostbyname(szHost);
-
- if pHostAddress != 0 then
- RtlMoveMemory (pStructHostEnt, pHostAddress, 16);
-
- pHostIPAddress = &HostIPAddress;
- RtlMoveMemory (pHostIPAddress, StructHostEnt.hAddrList, 4);
-
- pIpAddress = &IpAddress;
- RtlMoveMemory (pIpAddress, HostIPAddress, 4);
-
- NumToStr(sb1, pIpAddress->b1);
- NumToStr(sb2, pIpAddress->b2);
- NumToStr(sb3, pIpAddress->b3);
- NumToStr(sb4, pIpAddress->b4);
-
- szIPAdress = sb1 + "." + sb2 + "." + sb3 + "." + sb4 ;
-
- bReturn = TRUE;
- else
- bReturn = FALSE;
- endif;
-
- if nStartUp = 0 then
- WSACleanup();
- endif;
-
- UnUseDLL("WSOCK32.DLL");
- return bReturn;
- end;
|