请帮忙把这段程序翻译成 delphi 语言 ( 积分: 200 )

  • 主题发起人 主题发起人 黑猫白猫
  • 开始时间 开始时间

黑猫白猫

Unregistered / Unconfirmed
GUEST, unregistred user!
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)<br>{<br> BOOL bResult = FALSE;<br> HINSTANCE hDhcpDll;<br> DHCPNOTIFYPROC pDhcpNotifyProc;<br> WCHAR wcAdapterName[256];<br> <br> MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);<br><br> if((hDhcpDll = LoadLibrary(&quot;dhcpcsvc&quot;)) == NULL)<br> return FALSE;<br><br> if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, &quot;DhcpNotifyConfigChange&quot;)) != NULL)<br> if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0) == ERROR_SUCCESS)<br> bResult = TRUE;<br><br> FreeLibrary(hDhcpDll);<br> return bResult;<br>}
 
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)<br>{<br> BOOL bResult = FALSE;<br> HINSTANCE hDhcpDll;<br> DHCPNOTIFYPROC pDhcpNotifyProc;<br> WCHAR wcAdapterName[256];<br> <br> MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);<br><br> if((hDhcpDll = LoadLibrary(&quot;dhcpcsvc&quot;)) == NULL)<br> return FALSE;<br><br> if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, &quot;DhcpNotifyConfigChange&quot;)) != NULL)<br> if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0) == ERROR_SUCCESS)<br> bResult = TRUE;<br><br> FreeLibrary(hDhcpDll);<br> return bResult;<br>}
 
{<br>type<br> &nbsp;DHCPNOTIFYPROC = function(p1: Pointer; p2: PWideChar; p3: BOOL; p4, p5, p6, p7: Integer): Integer;<br>}<br>function NotifyIPChange(lpszAdapterName: PChar; nIndex: Integer;<br> &nbsp;pIPAddress, pNetMask: PChar): BOOL;<br>var<br> &nbsp;hDhcpDll: THandle;<br> &nbsp;pDhcpNotifyProc: DHCPNOTIFYPROC;<br> &nbsp;wcAdapterName: array[0..255] of WideChar;<br>begin<br> &nbsp;Result := False;<br> &nbsp;MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName, 256);<br> &nbsp;hDhcpDll := LoadLibrary('dhcpcsvc');<br> &nbsp;if (hDhcpDll = 0) then Exit;<br><br> &nbsp;@pDhcpNotifyProc := GetProcAddress(hDhcpDll, 'DhcpNotifyConfigChange');<br> &nbsp;if Assigned(pDhcpNotifyProc) then<br> &nbsp; &nbsp;if (pDhcpNotifyProc(nil, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0) = ERROR_SUCCESS) then<br> &nbsp; &nbsp; &nbsp;Result := TRUE;<br><br> &nbsp;FreeLibrary(hDhcpDll);<br>end;
 
还有这个:<br>BOOL DhcpNotifyConfigChange(<br> &nbsp; &nbsp;LPWSTR lpwszServerName, // 本地机器为NULL<br> &nbsp; &nbsp;LPWSTR lpwszAdapterName, // 适配器名称<br> &nbsp; &nbsp;BOOL bNewIpAddress, // TRUE表示更改IP<br> &nbsp; &nbsp;DWORD dwIpIndex, // 指明第几个IP地址,如果只有该接口只有一个IP地址则为0<br> &nbsp; &nbsp;DWORD dwIpAddress, // IP地址<br> &nbsp; &nbsp;DWORD dwSubNetMask, // 子网掩码<br> &nbsp; &nbsp;int nDhcpAction ); // 对DHCP的操作 0:不修改, 1:启用 DHCP,2:禁用 DHCP
 
function DhcpNotifyConfigChange(<br> &nbsp; &nbsp;lpwszServerName: PWideChar; // 本地机器为NULL<br> &nbsp; &nbsp;lpwszAdapterName: PWideChar; // 适配器名称<br> &nbsp; &nbsp;bNewIpAddress: BOOL; // TRUE表示更改IP<br> &nbsp; &nbsp;dwIpIndex: DWORD; // 指明第几个IP地址,如果只有该接口只有一个IP地址则为0<br> &nbsp; &nbsp;dwIpAddress: DWORD; // IP地址<br> &nbsp; &nbsp;dwSubNetMask: DWORD; // 子网掩码<br> &nbsp; &nbsp;nDhcpAction: Integer ): BOOL;
 
http://www.vckbase.com/document/viewdoc/?id=851<br><br>我改成这样:<br>type<br> &nbsp;DHCPNOTIFYPROC = function(p1:array of widechar; p2: array of widechar; p3: BOOL; p4, p5, p6:dword; p7: Integer): Integer;<br><br>var<br> &nbsp;onehandle: THandle;<br> &nbsp; &nbsp;pDhcpNotifyProc: DHCPNOTIFYPROC;<br> &nbsp; &nbsp;wcAdaptername :array [0..255] of widechar;<br> &nbsp; &nbsp;wcServerName :array[0..255] of widechar;<br> &nbsp; &nbsp;i : integer;<br>begin<br>.....<br> &nbsp; multibytetowidechar(CP_ACP,0,pchar(tempstr),-1,wcAdapterName,256);//tempstr是网卡名称,这个保证得到的值是正确的。<br> &nbsp; multibytetowidechar(CP_ACP,0,pchar(''),-1,wcServerName,256);//机器名为空<br><br> &nbsp; onehandle := loadlibrary('dhcpcsvc');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if onehandle &lt;&gt; 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@pDhcpNotifyProc := GetProcAddress(onehandle, 'DhcpNotifyConfigChange');<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if @pDhcpNotifyProc &lt;&gt; NIL then<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Assigned(pDhcpNotifyProc) then<br> &nbsp; &nbsp;i := pDhcpNotifyProc(wcServerName,wcAdapterName,true,0,inet_addr(pchar(edit1.text)),inet_addr(pchar(edit2.Text)),0);//edit1.text 是网卡的IP地址 edit2.text 是子网掩码<br> &nbsp; &nbsp; &nbsp;showmessage(inttostr(i));<br><br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;finally<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;freelibrary(onehandle);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>end.<br><br>结果看到的 i 为 87 而不是设想中的 0。<br>是不是我的程序写错了?
 
后退
顶部