1. 程式人生 > >動態修改IP方法

動態修改IP方法

//呼叫DhcpNotifyConfigChange函式動態修改IP
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)
{
 BOOL   bResult = FALSE;
 HINSTANCE  hDhcpDll;
 DHCPNOTIFYPROC pDhcpNotifyProc;
 WCHAR wcAdapterName[256];
 
 MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);
 if((hDhcpDll = LoadLibrary("dhcpcsvc")) == NULL)
  return FALSE;
 if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)
  if((pDhcpNotifyProc)(NULL,
                                     wcAdapterName,
                                     TRUE,
                                     nIndex,
                                     inet_addr(pIPAddress),
                                     inet_addr(pNetMask), 0) == ERROR_SUCCESS)
   bResult = TRUE;
 FreeLibrary(hDhcpDll);
 return bResult;
}