📄 main.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.
*/
#pragma warning(disable: 4201) // nameless struct/union
#include <windows.h>
#include <tchar.h>
#include <iostream.h>
#include <crtdbg.h>
#pragma warning(default: 4201)
#include "NWConnectionSettings.h"
static NWConnectionSettings* pSet = NULL;
using namespace JetByteTools;
extern "C" void DisplayCurrentSettings()
{
if (pSet == NULL)
{
_ASSERT(NULL);
return;
}
cerr << "Computer Name: " << pSet->GetComputerName() << "\n";
cerr << "Network Host Name : " << pSet->GetHostName() << "\n";
DWORD dwDHCPService = pSet->GetDHCPServiceStartValue();
cerr << "DHCP Service Value : " << dwDHCPService << " ";
bool bDisplayStaticIPInfo = true;
switch (dwDHCPService)
{
case 0:
{
cerr << "(BOOT)\n";
break;
}
case 1:
{
cerr << "(SYSTEM)\n";
break;
}
case 2:
{
cerr << "(AUTOMATIC)\n";
cerr << "Static IP information is logically INVALID\n";
bDisplayStaticIPInfo = false;
break;
}
case 3:
{
cerr << "(MANUAL)\n";
break;
}
case 4:
{
cerr << "(DISABLED)\n";
break;
}
default:
{
cerr << "(??? HOLA, UNEXPECTED !!!)\n";
break;
}
}
cerr << "Network Card : " << pSet->GetName_NC1() << "\n";
DWORD dwDHCP = pSet->GetEnableDHCP_NC1();
cerr << "Enable DHCP Value for Network Card : " << dwDHCP << " ";
switch (dwDHCP)
{
case 0:
{
cerr << "(BOOT)\n";
break;
}
case 1:
{
cerr << "(SYSTEM)\n";
break;
}
case 2:
{
cerr << "(AUTOMATIC)\n";
break;
}
case 3:
{
cerr << "(MANUAL)\n";
break;
}
case 4:
{
cerr << "(DISABLED)\n";
break;
}
default:
{
cerr << "(??? HOLA, UNEXPECTED !!!)\n";
break;
}
}
if (bDisplayStaticIPInfo == true)
{
cerr << "static TCP/IP Address for Network Card : " << pSet->GetTcpIpAddress_NC1() << "\n";
cerr << "static TCP/IP SubnetMask for Network Card : " << pSet->GetTcpIpSubnetMask_NC1() << "\n";
cerr << "static TCP/IP Default Gateway for Network Card : " << pSet->GetDefaultGateway_NC1() << "\n";
}
return;
}
extern "C" bool SetDHCP()
{
cerr << "Preparing to Set DHCP...\n";
cerr << "----------------\n";
if (pSet == NULL)
{
_ASSERT(NULL);
return false;
}
bool bAnswer = false;
char strConfirm[64];
::ZeroMemory(strConfirm, 64);
while ( strcmp(strConfirm, "y") != 0 && strcmp(strConfirm, "n") != 0)
{
::ZeroMemory(strConfirm, 64);
cerr << "Are You sure you want to Enable DHCP ? (y/n) : ";
cin >> strConfirm;
}
if (strcmp(strConfirm, "y") == 0)
{
if (pSet->SetDHCPServiceStartValue(2)
&& pSet->SetEnableDHCP_NC1(1)
&& pSet->SetTcpIpAddress_NC1("0.0.0.0")
&& pSet->SetTcpIpSubnetMask_NC1("0.0.0.0"))
{
cerr << "DHCP was set successfully...\n";
bAnswer = true;
}
else
{
cerr << "DHCP was NOT set correctly!!\n";
}
}
else
{
cerr << "IP configuration is NOT changed!\n";
}
return bAnswer;
}
extern "C" bool SetStaticIP()
{
cerr << "Preparing to Set Static IP...\n";
cerr << "----------------\n";
if (pSet == NULL)
{
_ASSERT(NULL);
return false;
}
char strIPAddress[64];
::ZeroMemory(strIPAddress, 64);
char strSubnetMask[64];
::ZeroMemory(strSubnetMask, 64);
char strConfirm[64];
::ZeroMemory(strConfirm, 64);
cerr << "Please Enter the IP Address (w.x.y.z): ";
cin >> strIPAddress;
cerr << "Please Enter the SubnetMask (w.x.y.z): ";
cin >> strSubnetMask;
while ( strcmp(strConfirm, "y") != 0 && strcmp(strConfirm, "n") != 0)
{
::ZeroMemory(strConfirm, 64);
cerr << "IP Address " << strIPAddress << ", SubnetMask " << strSubnetMask << ", is this correct ? (y/n) : ";
cin >> strConfirm;
}
if (strcmp(strConfirm, "y") == 0)
{
bool bAnswer = false;
if (pSet->SetDHCPServiceStartValue(1)
&& pSet->SetEnableDHCP_NC1(4)
&& pSet->SetTcpIpAddress_NC1(strIPAddress)
&& pSet->SetTcpIpSubnetMask_NC1(strSubnetMask))
{
cerr << "Static IP was set successfully...\n";
bAnswer = true;
}
else
{
cerr << "Static IP was NOT set correctly!!\n";
}
return bAnswer;
}
else
{
cerr << "IP configuration is NOT changed!\n";
}
return false;
}
extern "C" bool SetTheComputerName()
{
cerr << "Preparing to Set Computer Name...\n";
cerr << "----------------\n";
if (pSet == NULL)
{
_ASSERT(NULL);
return false;
}
bool bAnswer = false;
cerr << "Enter the computer name\n";
cerr << "(for simplicity hostname == computer name)\n";
cerr << "If you want to keep the current name, ";
cerr << "do not type a name but type '-1'.\n";
cerr << "New Computer AND Hostname : ";
char strComputername[64];
::ZeroMemory(strComputername, 64);
cin >> strComputername;
if (strcmp(strComputername, "-1") != 0)
{
char strConfirm[64];
::ZeroMemory(strConfirm, 64);
while ( strcmp(strConfirm, "y") != 0 && strcmp(strConfirm, "n") != 0)
{
::ZeroMemory(strConfirm, 64);
cerr << "Computer Name " << strComputername << ", is this correct ? (y/n) : ";
cin >> strConfirm;
}
if (strcmp(strConfirm, "y") == 0)
{
if (pSet->SetComputerName(strComputername)
&& pSet->SetHostName(strComputername))
{
bAnswer = true;
cerr << "ComputerName was set successfully!!\n";
}
else
{
cerr << "ComputerName was NOT set correctly!!\n";
}
}
else
{
cerr << "Computername is NOT changed!\n";
}
}
else
{
cerr << "Computername is NOT changed!\n";
}
return bAnswer;
}
extern "C" bool RebootPC()
{
cerr << "Preparing to Reboot the PC...\n";
cerr << "----------------\n";
if (pSet == NULL)
{
_ASSERT(NULL);
return false;
}
bool bAnswer = false;
char strConfirm[64];
::ZeroMemory(strConfirm, 64);
while ( strcmp(strConfirm, "y") != 0 && strcmp(strConfirm, "n") != 0)
{
::ZeroMemory(strConfirm, 64);
cerr << "A reboot is required after a registry change!\n";
cerr << "Do you want to reboot the target PC ? (y/n) : ";
cin >> strConfirm;
}
if (strcmp(strConfirm, "y") == 0)
{
char* compname = pSet->GetConnectedComputerName();
BOOL BAnswer = FALSE;
if (compname == NULL)
{
BAnswer = ::ExitWindows(0, 0);
}
else
{
BAnswer = ::InitiateSystemShutdown(compname, // address of name of computer to shut down
NULL, // address of message to display in dialog box
0, // time to display dialog box
TRUE, // force applications with unsaved changes flag
TRUE // reboot flag
);
}
if (BAnswer)
{
bAnswer = true;
cerr << "Computer will reboot!!\n";
}
else
{
cerr << "Computer will NOT reboot!!\n";
}
}
else
{
cerr << "Computer is NOT scheduled to reboot!\n";
}
return bAnswer;
}
extern "C" void DoMenu()
{
int choice = -1;
while (choice == -1)
{
cerr << "----------------\n";
cerr << "Please specify an option...\n";
cerr << "0. Quit Program\n";
cerr << "1. Use DHCP\n";
cerr << "2. Use a static IP Address\n";
cerr << "3. Change the Computer and Network Name\n";
cerr << "----------------\n";
cerr << "Enter Option: ";
cin >> choice;
switch (choice)
{
case 0:
{
return;
}
case 1:
{
if (SetDHCP())
{
SetTheComputerName();
}
RebootPC();
break;
}
case 2:
{
if (SetStaticIP())
{
SetTheComputerName();
}
RebootPC();
break;
}
case 3:
{
if (SetTheComputerName())
{
RebootPC();
}
break;
}
default:
{
choice = -1;
break;
}
}
}
}
int main(int argc, char *argv[ ])
{
cerr << "DHCP Utility 1.0\n";
cerr << "----------------\n";
cerr << "made by GBO \n(gert.boddaert@advalvas.be), 8 may 2000\n";
cerr << "----------------\n";
cerr << "*** USE AT YOUR OWN RISK ***\n";
cerr << "----------------\n";
cerr << "If no command line parameter is supplied, \nthe settings of the local computer are retrieved.\n";
cerr << "To retrieve and change settings of a remote Network computer, \nSpecify the network computer name as parameter.\n";
cerr << "Administrator Privileges are required for the program \nto function properly.\n";
cerr << "----------------\n";
char* computername = NULL;
if (argc == 0)
{
_ASSERT(NULL); // huh?
exit(-1);
}
else if (argc == 1)
{
cerr << "Local computer settings...\n";
cerr << "----------------\n";
}
else if (argc == 2)
{
computername = argv[1];
cerr << "Remote computer settings...: " << computername << "\n";
cerr << "----------------\n";
}
else
{
cerr << "Please specify maximum 1 parameter (the remote network computer name)!\n";
cerr << "----------------\n";
exit(-1);
}
NWConnectionSettings theNWCSettings;
pSet = &theNWCSettings;
bool bAnswer = theNWCSettings.Connect(computername);
if (bAnswer)
{
DisplayCurrentSettings();
DoMenu();
}
else
{
cerr << "Serious Error occurred!\n";
cerr << "----------------\n";
}
theNWCSettings.Close();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -