📄 nwconnectionsettings.cpp
字号:
//
// 8-may-2000 GBO
// gert.boddaert@advalvas.be
// =================================================================
// DISCLAIMER
// =================================================================
/*
This software is provided "as is" and any express or implied
warranties, including, but not limited to, the implied warranties of
merchantibility and fitness for a particular purpose are disclaimed.
In no event shall the author be liable for any
direct, indirect, incidental, special, exemplary, or consequential
damages (including, but not limited to, procurement of substitute
goods or services; loss of use, data, or profits; or business
interruption) however caused and on any theory of liability,
whether in contract, strict liability, or tort (including negligence
or otherwise) arising in any way out of the use of this software,
even if advised of the possibility of such damage.
*/
// NWConnectionSettings.cpp: implementation of the NWConnectionSettings class.
//
//////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <tchar.h>
#include <crtdbg.h>
#include "NWConnectionSettings.h"
// Code based on CodeGuru Article "Change IP Address and HostName of NT machine" - Massimo Negroni
// Code based on MSDN Article Q194407
// Code based on CodeProject Article "Registry API Wrapper" - Len Holgate
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define STRINGBUFSIZE 512
NWConnectionSettings::NWConnectionSettings() : myDhcpServiceKey(NULL),
myNetworkCardKey1(NULL),
myTcpIpNC1Key(NULL),
myTcpIpServiceKey(NULL),
myComputerKey(NULL)
{
p_myServiceName = NULL;
p_myIPAddress = NULL;
p_mySubNetMask = NULL;
p_myComputerName = NULL;
p_myDefaultGateway = NULL;
p_myHostName = NULL;
p_myRemoteComputerName = NULL;
my_dwDhcpServiceStart = 0;
my_dwTCPIP_EnableDhcp = 0;
}
NWConnectionSettings::~NWConnectionSettings()
{
DeleteObjects();
}
void NWConnectionSettings::DeleteObjects()
{
my_dwDhcpServiceStart = 0;
my_dwTCPIP_EnableDhcp = 0;
myDhcpServiceKey = CRegistryKey(NULL); // close key!
myNetworkCardKey1 = CRegistryKey(NULL); // close key!
myTcpIpNC1Key = CRegistryKey(NULL); // close key!
myTcpIpServiceKey = CRegistryKey(NULL); // close key!
if (p_myServiceName != NULL)
{
free(p_myServiceName);
p_myServiceName = NULL;
}
if (p_myIPAddress != NULL)
{
delete [] p_myIPAddress;
p_myIPAddress = NULL;
}
if (p_mySubNetMask != NULL)
{
delete [] p_mySubNetMask;
p_mySubNetMask = NULL;
}
if (p_myDefaultGateway != NULL)
{
delete [] p_myDefaultGateway;
p_myDefaultGateway = NULL;
}
if (p_myHostName != NULL)
{
free(p_myHostName);
p_myHostName = NULL;
}
if (p_myRemoteComputerName != NULL)
{
free(p_myRemoteComputerName);
p_myRemoteComputerName = NULL;
}
if (p_myComputerName != NULL)
{
delete [] p_myComputerName;
p_myComputerName = NULL;
}
}
bool NWConnectionSettings::Connect(const char* strComputerName)
{
DeleteObjects();
if (strComputerName == NULL)
{
p_myComputerName = NULL;
}
else
{
unsigned int length = strlen(strComputerName)+1;
p_myComputerName = new char[length];
::ZeroMemory(p_myComputerName, length);
strcpy(p_myComputerName, strComputerName);
}
bool bAnswer = false;
// Retrieve DHCP Service Start Value
try
{
myDhcpServiceKey = CRegistryKey(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Services\\DHCP"), KEY_ALL_ACCESS, p_myComputerName);
bAnswer = myDhcpServiceKey.QueryValue( _T("Start"), my_dwDhcpServiceStart);
if (!bAnswer)
{
_ASSERT(NULL);
return bAnswer;
}
myNetworkCardKey1 = CRegistryKey(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards\\1"), KEY_ALL_ACCESS, p_myComputerName);
bAnswer = myNetworkCardKey1.QueryValue( _T("ServiceName"), (LPCTSTR*) &p_myServiceName);
if (!bAnswer)
{
_ASSERT(NULL);
p_myServiceName = NULL;
return bAnswer;
}
// retrieve network card Enable DHCP value
char strService[MAX_PATH + 1];
::ZeroMemory(strService, MAX_PATH+1);
strcpy(strService, "SYSTEM\\CurrentControlSet\\Services\\");
strcat(strService, p_myServiceName);
strcat(strService, "\\Parameters\\TCPIP");
myTcpIpNC1Key = CRegistryKey(HKEY_LOCAL_MACHINE, _T(strService), KEY_ALL_ACCESS, p_myComputerName);
bAnswer = myTcpIpNC1Key.QueryValue( _T("EnableDHCP"), my_dwTCPIP_EnableDhcp);
if (!bAnswer)
{
_ASSERT(NULL);
return bAnswer;
}
// retrieve network card tcpip address value
DWORD Type = 0;
p_myIPAddress = new char[STRINGBUFSIZE];
DWORD cbData = STRINGBUFSIZE;
LONG RESULT = ::RegQueryValueEx( myTcpIpNC1Key, // handle of key to query
_T("IPAddress"), // address of name of value to query
NULL, // reserved
&Type, // address of buffer for value type
(BYTE*) p_myIPAddress, // address of data buffer
&cbData // address of data buffer size
);
if (RESULT == ERROR_SUCCESS && Type == REG_MULTI_SZ)
{
// success!
bAnswer = true;
}
else
{
_ASSERT(NULL);
delete [] p_myIPAddress;
p_myIPAddress = NULL;
bAnswer = false;
return bAnswer;
}
// retrieve network card tcpip subnetmask value
Type = 0;
p_mySubNetMask = new char[STRINGBUFSIZE];
cbData = STRINGBUFSIZE;
RESULT = ::RegQueryValueEx( myTcpIpNC1Key, // handle of key to query
_T("SubnetMask"), // address of name of value to query
NULL, // reserved
&Type, // address of buffer for value type
(BYTE*) p_mySubNetMask, // address of data buffer
&cbData // address of data buffer size
);
if (RESULT == ERROR_SUCCESS && Type == REG_MULTI_SZ)
{
// success!
bAnswer = true;
}
else
{
_ASSERT(NULL);
delete [] p_mySubNetMask;
p_mySubNetMask = NULL;
bAnswer = false;
return bAnswer;
}
Type = 0;
p_myDefaultGateway = new char[STRINGBUFSIZE];
cbData = STRINGBUFSIZE;
RESULT = ::RegQueryValueEx( myTcpIpNC1Key, // handle of key to query
_T("DefaultGateway"), // address of name of value to query
NULL, // reserved
&Type, // address of buffer for value type
(BYTE*) p_myDefaultGateway, // address of data buffer
&cbData // address of data buffer size
);
if (RESULT == ERROR_SUCCESS && Type == REG_MULTI_SZ)
{
// success!
bAnswer = true;
}
else
{
_ASSERT(NULL);
delete [] p_myDefaultGateway;
p_myDefaultGateway = NULL;
bAnswer = false;
return bAnswer;
}
myTcpIpServiceKey = CRegistryKey(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Services\\TcpIp\\Parameters"), KEY_ALL_ACCESS, p_myComputerName);
bAnswer = myTcpIpServiceKey.QueryValue( _T("HostName"), (LPCTSTR*) &p_myHostName);
if (!bAnswer)
{
_ASSERT(NULL);
p_myHostName = NULL;
return bAnswer;
}
myComputerKey = CRegistryKey(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName"), KEY_ALL_ACCESS, p_myComputerName);
bAnswer = myComputerKey.QueryValue( _T("ComputerName"), (LPCTSTR*) &p_myRemoteComputerName);
if (!bAnswer)
{
_ASSERT(NULL);
p_myRemoteComputerName = NULL;
return bAnswer;
}
}
catch (CRegistryKey::Exception &e)
{
e.MessageBox();
DeleteObjects();
return bAnswer;
}
}
void NWConnectionSettings::Close()
{
DeleteObjects();
}
char* NWConnectionSettings::GetName_NC1()
{
return p_myServiceName;
}
DWORD NWConnectionSettings::GetDHCPServiceStartValue()
{
return my_dwDhcpServiceStart;
}
bool NWConnectionSettings::SetDHCPServiceStartValue(DWORD dhcp)
{
bool bAnswer = false;
// Retrieve DHCP Service Start Value
try
{
myDhcpServiceKey.SetValue( _T("Start"), dhcp);
my_dwDhcpServiceStart = dhcp;
bAnswer = true;
}
catch (CRegistryKey::Exception &e)
{
e.MessageBox();
}
return bAnswer;
}
DWORD NWConnectionSettings::GetEnableDHCP_NC1()
{
return my_dwTCPIP_EnableDhcp;
}
bool NWConnectionSettings::SetEnableDHCP_NC1(DWORD dhcpvalue)
{
bool bAnswer = false;
if (p_myServiceName == NULL)
{
return bAnswer;
}
try
{
myTcpIpNC1Key.SetValue( _T("EnableDHCP"), dhcpvalue);
my_dwTCPIP_EnableDhcp = dhcpvalue;
bAnswer = true;
}
catch (CRegistryKey::Exception &e)
{
e.MessageBox();
}
return bAnswer;
}
char* NWConnectionSettings::GetTcpIpAddress_NC1(void)
{
return p_myIPAddress;
}
bool NWConnectionSettings::SetTcpIpAddress_NC1(const char* tcpipaddress)
{
bool bAnswer = false;
if (p_myServiceName == NULL || tcpipaddress == NULL)
{
return bAnswer;
}
unsigned int length = strlen(tcpipaddress) + 2; // we need 2 null terminating
char* temp = new char[length];
::ZeroMemory(temp, length);
strcpy(temp, tcpipaddress);
DWORD Type = REG_MULTI_SZ;
LONG RESULT = ::RegSetValueEx( myTcpIpNC1Key, // handle of key to set value for
_T("IPAddress"), // address of value to set
NULL, // reserved
Type, // flag for value type
(CONST BYTE*) temp, // address of value data
length);
if (RESULT == ERROR_SUCCESS )
{
// success!
if (p_myIPAddress != NULL)
{
delete [] p_myIPAddress;
p_myIPAddress = NULL;
}
p_myIPAddress = new char[length+1];
::ZeroMemory(p_myIPAddress, length+1);
strcpy(p_myIPAddress, tcpipaddress);
bAnswer = true;
}
else
{
_ASSERT(NULL);
delete [] p_myIPAddress;
p_myIPAddress = NULL;
bAnswer = false;
}
delete [] temp;
return bAnswer;
}
char* NWConnectionSettings::GetTcpIpSubnetMask_NC1(void)
{
return p_mySubNetMask;
}
bool NWConnectionSettings::SetTcpIpSubnetMask_NC1(const char* subnetmask)
{
bool bAnswer = false;
if (p_myServiceName == NULL || subnetmask == NULL)
{
return bAnswer;
}
unsigned int length = strlen(subnetmask) + 2; // we need 2 null terminating
char* temp = new char[length];
::ZeroMemory(temp, length);
strcpy(temp, subnetmask);
DWORD Type = REG_MULTI_SZ;
LONG RESULT = ::RegSetValueEx( myTcpIpNC1Key, // handle of key to set value for
_T("SubnetMask"), // address of value to set
NULL, // reserved
Type, // flag for value type
(CONST BYTE*) temp, // address of value data
length);
if (RESULT == ERROR_SUCCESS )
{
// success!
if (p_mySubNetMask != NULL)
{
delete [] p_mySubNetMask;
p_mySubNetMask = NULL;
}
p_mySubNetMask = new char[length+1];
::ZeroMemory(p_mySubNetMask, length+1);
strcpy(p_mySubNetMask, subnetmask);
bAnswer = true;
}
else
{
_ASSERT(NULL);
delete [] p_mySubNetMask;
p_mySubNetMask = NULL;
bAnswer = false;
}
delete [] temp;
return bAnswer;
}
char* NWConnectionSettings::GetDefaultGateway_NC1(void)
{
return p_myDefaultGateway;
}
bool NWConnectionSettings::SetDefaultGateway_NC1(const char* defaultgateway)
{
bool bAnswer = false;
if (p_myServiceName == NULL || defaultgateway == NULL)
{
return bAnswer;
}
unsigned int length = strlen(defaultgateway) + 2; // we need 2 null terminating
char* temp = new char[length];
::ZeroMemory(temp, length);
strcpy(temp, defaultgateway);
DWORD Type = REG_MULTI_SZ;
LONG RESULT = ::RegSetValueEx( myTcpIpNC1Key, // handle of key to set value for
_T("DefaultGateway"), // address of value to set
NULL, // reserved
Type, // flag for value type
(CONST BYTE*) temp, // address of value data
length);
if (RESULT == ERROR_SUCCESS )
{
// success!
if (p_myDefaultGateway != NULL)
{
delete [] p_myDefaultGateway;
p_myDefaultGateway = NULL;
}
p_myDefaultGateway = new char[length+1];
::ZeroMemory(p_myDefaultGateway, length+1);
strcpy(p_myDefaultGateway, defaultgateway);
bAnswer = true;
}
else
{
_ASSERT(NULL);
delete [] p_myDefaultGateway;
p_myDefaultGateway = NULL;
bAnswer = false;
}
delete [] temp;
return bAnswer;
}
char* NWConnectionSettings::GetHostName(void)
{
return p_myHostName;
}
bool NWConnectionSettings::SetHostName(const char* hostname)
{
bool bAnswer = false;
if (hostname == NULL)
{
return bAnswer;
}
try
{
myTcpIpServiceKey.SetValue( _T("HostName"), (LPCTSTR) hostname);
bAnswer = true;
}
catch (CRegistryKey::Exception &e)
{
e.MessageBox();
}
if (!bAnswer)
{
_ASSERT(NULL);
free(p_myHostName);
p_myHostName = NULL;
bAnswer = false;
}
else
{
// success!
if (p_myHostName != NULL)
{
free(p_myHostName);
p_myHostName = NULL;
}
unsigned int length = strlen(hostname);
p_myHostName = (char*) malloc(length+1);
::ZeroMemory(p_myHostName, length+1);
strcpy(p_myHostName, hostname);
bAnswer = true;
}
return bAnswer;
}
char* NWConnectionSettings::GetComputerName(void)
{
return p_myRemoteComputerName;
}
bool NWConnectionSettings::SetComputerName(const char* computername)
{
bool bAnswer = false;
if (computername == NULL)
{
return bAnswer;
}
try
{
myComputerKey.SetValue( _T("ComputerName"), (LPCTSTR) computername);
bAnswer = true;
}
catch (CRegistryKey::Exception &e)
{
e.MessageBox();
}
if (!bAnswer)
{
_ASSERT(NULL);
free(p_myRemoteComputerName);
p_myRemoteComputerName = NULL;
bAnswer = false;
}
else
{
// success!
if (p_myRemoteComputerName != NULL)
{
free(p_myRemoteComputerName);
p_myRemoteComputerName = NULL;
}
unsigned int length = strlen(computername);
p_myRemoteComputerName = (char*) malloc(length+1);
::ZeroMemory(p_myRemoteComputerName, length+1);
strcpy(p_myRemoteComputerName, computername);
bAnswer = true;
}
return bAnswer;
}
char* NWConnectionSettings::GetConnectedComputerName()
{
return p_myComputerName;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -