01.
BOOL
RegSetIP(
LPCTSTR
lpszAdapterName,
LPCTSTR
pIPAddress,
LPCTSTR
pNetMask,
LPCTSTR
pNetGate)
02.
{
03.
HKEY
hKey;
04.
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\
05.
Tcpip\\Parameters\\Interfaces\\";
06.
strKeyName += lpszAdapterName;
07.
if
(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
08.
strKeyName.c_str(),
09.
0,
10.
KEY_WRITE,
11.
&hKey) != ERROR_SUCCESS)
12.
return
FALSE;
13.
14.
char
mszIPAddress[100];
15.
char
mszNetMask[100];
16.
char
mszNetGate[100];
17.
18.
strncpy
(mszIPAddress, pIPAddress, 98);
19.
strncpy
(mszNetMask, pNetMask, 98);
20.
strncpy
(mszNetGate, pNetGate, 98);
21.
22.
int
nIP, nMask, nGate;
23.
24.
nIP =
strlen
(mszIPAddress);
25.
nMask =
strlen
(mszNetMask);
26.
nGate =
strlen
(mszNetGate);
27.
28.
*(mszIPAddress + nIP + 1) = 0x00;
// REG_MULTI_SZ数据需要在后面再加个0
29.
nIP += 2;
30.
31.
*(mszNetMask + nMask + 1) = 0x00;
32.
nMask += 2;
33.
34.
*(mszNetGate + nGate + 1) = 0x00;
35.
nGate += 2;
36.
37.
RegSetValueEx(hKey,
"IPAddress"
, 0, REG_MULTI_SZ, (unsigned
char
*)mszIPAddress, nIP);
38.
RegSetValueEx(hKey,
"SubnetMask"
, 0, REG_MULTI_SZ, (unsigned
char
*)mszNetMask, nMask);
39.
RegSetValueEx(hKey,
"DefaultGateway"
, 0, REG_MULTI_SZ, (unsigned
char
*)mszNetGate, nGate);
40.
41.
RegCloseKey(hKey);
42.
43.
return
TRUE;
44.
}