VC Ip、子网掩码、默认网关、首选DNS服务器

#####阅读原文

Ip、子网掩码、默认网关、首选DNS服务器相关信息在注册表之中,改变注册表的值,然后通知所以应用程序即可。

代码如下:
BOOL SetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
CString strKeyName = “SYSTEM//CurrentControlSet//Services//Tcpip//Parameters//Interfaces// “;
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName,
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;

char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];

strncpy(mszIPAddress, pIPAddress, 98);
strncpy(mszNetMask, pNetMask, 98);
strncpy(mszNetGate, pNetGate, 98);

int nIP, nMask, nGate;

nIP = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = strlen(mszNetGate);

*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;

*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;

*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;

RegSetValueEx(hKey, “IPAddress “, 0, REG_MULTI_SZ, (unsigned char)mszIPAddress, nIP);
RegSetValueEx(hKey, “SubnetMask “, 0, REG_MULTI_SZ, (unsigned char
)mszNetMask, nMask);
RegSetValueEx(hKey, “DefaultGateway “, 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);

RegCloseKey(hKey);

//通知IP地址改变
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 TRUE;
}


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 Web@Cool02.com

文章标题:VC Ip、子网掩码、默认网关、首选DNS服务器

文章字数:223

本文作者:零贰

发布时间:2015-12-01, 14:15:54

最后更新:2020-03-26, 16:36:58

原始链接:/html/20151201141554.html

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
零址导航 Gitee