IpAddress.rul 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // 文 件 名:IpAddress.rul
  2. // 脚本功能:获取计算机主机名和IP地址
  3. // 编写日期:2015-01-08
  4. // 使用方法:
  5. // 1. 在 setup.rul 前面加入 #include "IpAddress.rul"
  6. // 2. 调用函数 fGetIpAddress(szHost, szIPAdress)
  7. prototype NUMBER WSOCK32.WSAStartup( NUMBER, POINTER);
  8. prototype NUMBER WSOCK32.gethostbyname(BYREF STRING);
  9. prototype NUMBER WSOCK32.gethostname(BYREF STRING, NUMBER);
  10. prototype NUMBER KERNEL32.RtlMoveMemory (POINTER, NUMBER, NUMBER);
  11. prototype NUMBER WSOCK32.WSACleanup();
  12. prototype fGetIpAddress(BYREF STRING, BYREF STRING);
  13. typedef WSADATA
  14. begin
  15. INT wversion;
  16. INT wHighVersion;
  17. STRING szDescription[255];
  18. STRING szSystemStatus[128];
  19. INT iMaxSockets;
  20. INT iMaxUdpDg;
  21. INT lpszVendorInfo;
  22. end;
  23. typedef HOSTENT
  24. begin
  25. NUMBER hName; // As Long
  26. NUMBER hAliases; // As Long
  27. SHORT hAddrType; //As Integer
  28. SHORT hLength; // As Integer
  29. NUMBER hAddrList; // As Long
  30. end;
  31. typedef IPADDRESS
  32. begin
  33. CHAR b1; //First
  34. CHAR b2; //Second
  35. CHAR b3; //Third
  36. CHAR b4; //Fourth
  37. end;
  38. function fGetIpAddress(szHost, szIPAdress)
  39. WSADATA StructWSAData;
  40. WSADATA POINTER pStructWSAData;
  41. HOSTENT StructHostEnt;
  42. HOSTENT POINTER pStructHostEnt;
  43. NUMBER nResult;
  44. NUMBER pHostAddress;
  45. POINTER pHostIPAddress;
  46. NUMBER HostIPAddress;
  47. IPADDRESS IpAddress;
  48. IPADDRESS POINTER pIpAddress;
  49. STRING sb1, sb2, sb3, sb4;
  50. NUMBER nStartUp;
  51. BOOL bReturn;
  52. begin
  53. // 加载dll库;
  54. if UseDLL(WINSYSDIR^"WSOCK32.DLL") < 0 then
  55. return FALSE;
  56. endif;
  57. pStructWSAData = &StructWSAData;
  58. pStructHostEnt = &StructHostEnt;
  59. // 初始化网络环境;
  60. nStartUp = WSAStartup(0x101, pStructWSAData);
  61. if nStartUp != 0 then
  62. return FALSE;
  63. endif;
  64. // 获取计算机名;
  65. nResult = gethostname(szHost, 256);
  66. if ( nResult != 0 ) then
  67. return FALSE;
  68. endif;
  69. // 根据计算机名获取ip;
  70. pHostAddress = gethostbyname(szHost);
  71. if pHostAddress != 0 then
  72. RtlMoveMemory (pStructHostEnt, pHostAddress, 16);
  73. pHostIPAddress = &HostIPAddress;
  74. RtlMoveMemory (pHostIPAddress, StructHostEnt.hAddrList, 4);
  75. pIpAddress = &IpAddress;
  76. RtlMoveMemory (pIpAddress, HostIPAddress, 4);
  77. NumToStr(sb1, pIpAddress->b1);
  78. NumToStr(sb2, pIpAddress->b2);
  79. NumToStr(sb3, pIpAddress->b3);
  80. NumToStr(sb4, pIpAddress->b4);
  81. szIPAdress = sb1 + "." + sb2 + "." + sb3 + "." + sb4 ;
  82. bReturn = TRUE;
  83. else
  84. bReturn = FALSE;
  85. endif;
  86. if nStartUp = 0 then
  87. WSACleanup();
  88. endif;
  89. UnUseDLL("WSOCK32.DLL");
  90. return bReturn;
  91. end;