//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
String g_strAppTitle = "WinNT/2000/2003/2k/XP远程关机 ";
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
SetWindowLong(edtTimeOut->Handle, GWL_STYLE,
GetWindowLong(edtTimeOut->Handle, GWL_STYLE) | ES_NUMBER);
Caption = g_strAppTitle;
Application->Title = g_strAppTitle;
}
//---------------------------------------------------------------------------
int __fastcall ShutDownRemoteMachine(LPSTR lpMachineName,
LPSTR lpUsr, LPSTR lpPwd, LPSTR lpMsg, DWORD dwTimeOut)
{
HANDLE hToken;
int nRet = -1;
TOKEN_PRIVILEGES tp, tpNew;
NETRESOURCE nr;
ZeroMemory(&nr, sizeof(nr));
nr.dwType = RESOURCETYPE_ANY;
nr.lpLocalName = "";
nr.lpProvider = "";
nr.lpRemoteName = lpMachineName;
DWORD dwResult = WNetAddConnection2(&nr, lpPwd, lpUsr, 0);
if(dwResult == 0)
{
if(OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
tp.PrivilegeCount = 1;
if(!(LookupPrivilegeValue(lpMachineName,
"SeRemoteShutdownPrivilege", &tp.Privileges[0].Luid)))
{
nRet = -2; // 查找远程关机权限失败
}
else
{
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
DWORD dwRetLen;
if(!(AdjustTokenPrivileges(
hToken, false, &tp, sizeof(tp), &tpNew, &dwRetLen)))
{
nRet = -3; // 调整远程关机权限失败
}
else
{
if(InitiateSystemShutdown(
lpMachineName, lpMsg, dwTimeOut, TRUE, FALSE))
nRet = 0;
else
nRet = -4; // 远程关机执行失败
}
}
CloseHandle(hToken);
}
}
else
nRet = GetLastError(); // 连接到远程主机失败
return nRet;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnShutDownClick(TObject *Sender)
{
try
{
if(edtMachine->Text == "" || cbUsr->Text == "")
{
MessageBox(Handle, "必须输入机器名和用户名!",
g_strAppTitle.c_str(), MB_OK | MB_ICONWARNING);
return;
}
String strMachine = edtMachine->Text;
if(edtMachine->Text[1] != '\\')
strMachine = "\\\\" + strMachine;
DWORD dwTimeOut;
try
{
dwTimeOut = edtTimeOut->Text.ToInt();
}
catch(...)
{
dwTimeOut = 0;
}
int nRet = ShutDownRemoteMachine(
strMachine.c_str(), cbUsr->Text.c_str(), edtPwd->Text.c_str(),
mmoMsg->Text.c_str(), dwTimeOut);
String strMsg;
UINT unIcon = MB_ICONWARNING;
switch(nRet)
{
case 0:
strMsg = String("对远程主机 ") + strMachine + " 关机执行成功!";
unIcon = MB_ICONINFORMATION;
break;
case -2:
strMsg = String("查找远程主机 ") + strMachine + " 关机权限失败!"; break;
case -3:
strMsg = String("调整远程主机 ") + strMachine + " 关机权限失败!"; break;
case -4:
strMsg = String("对远程主机 ") + strMachine + " 执行关机失败!"; break;
default:
strMsg = String("连接到 " + strMachine + " 失败,原因可能是:\r\n")
+ SysErrorMessage(nRet); break;
}
MessageBox(Handle, strMsg.c_str(), g_strAppTitle.c_str(), MB_OK | unIcon);
}
catch(...)
{
ShowMessage("输入错误!");
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnAbortClick(TObject *Sender)
{
String strMachine = edtMachine->Text;
if(edtMachine->Text[1] != '\\')
strMachine = "\\\\" + strMachine;
if(AbortSystemShutdown(strMachine.c_str()))
MessageBox(Handle, String("成功中止远程关机任务!\r\n"
+ strMachine).c_str(), g_strAppTitle.c_str(),
MB_OK | MB_ICONINFORMATION);
else
MessageBox(Handle, String("远程关机任务的中止执行失败!\r\n"
+ strMachine).c_str(), g_strAppTitle.c_str(),
MB_OK | MB_ICONWARNING);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnAboutClick(TObject *Sender)
{
Form2 = new TForm2(this);
Form2->ShowModal();
delete Form2;
/*TForm2 *wnd;
wnd=new TForm2(Application);
//wnd->Left=0;
//wnd->Top=0;
//wnd->Height=366;
//wnd->Width=583;
wnd->Show(); */
}
//---------------------------------------------------------------------------