怎样在C++程序中编写代码使机器重启,急!!!

wintersea 2003-07-17 10:07:23
怎样在C++程序中编写代码使机器重启。
...全文
146 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Siney 2003-07-17
  • 打赏
  • 举报
回复
MSDN里面给出了最新的例子。要提升权限的:
BOOL MySystemShutdown()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;

// Get a token for this process.

if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return( FALSE );

// Get the LUID for the shutdown privilege.

LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
&tkp.Privileges[0].Luid);

tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get the shutdown privilege for this process.

AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);

if (GetLastError() != ERROR_SUCCESS)
return FALSE;

// Shut down the system and force all applications to close.
//
//ExitWindowsEx(EWX_FORCE|EWX_REBOOT,0)是重启。
//ExitWindowsEx(EWX_FORCE|EWX_LOGOFF,0);是注销
// EWX_FORCE参数是指强行关闭所有程序,不会弹出任何对话框。
if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE | EWX_POWEROFF, 0))
return FALSE;

return TRUE;
}
domustdo 2003-07-17
  • 打赏
  • 举报
回复
Nowcan的代码:

#include <windows.h>
#include <stdio.h>
#include <conio.h>

//这个函数用于获得必要的权限。
BOOL EnablePrivilege(LPCTSTR lpSystemName,LPCTSTR lpName)
{
HANDLE hToken;
BOOL fOk=FALSE;
if(OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken))
{
TOKEN_PRIVILEGES tp;
tp.PrivilegeCount=1;
if(!LookupPrivilegeValue(lpSystemName,lpName,&tp.Privileges[0].Luid))
printf("Can't lookup privilege value.\n");
tp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
if(!AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(tp),NULL,NULL))
printf("Can't adjust privilege value.\n");
fOk=(GetLastError()==ERROR_SUCCESS);
CloseHandle(hToken);
}
return fOk;
}

int main()
{
char msg[256],cname[256];
int timeout;
bool ret;
printf("1----Shut down local computer.\n2----Reboot local computer.\n");
printf("3----Shut down remote computer.\n4----Reboot remote computer.\n\n");

switch(getch())
{
case '1':
printf("Input the message(Max. 255 chars):");
scanf("%s",msg);
printf("Input the timeout value:");
scanf("%d",&timeout);
EnablePrivilege(NULL,SE_SHUTDOWN_NAME);
ret=InitiateSystemShutdown(NULL,msg,timeout,TRUE,FALSE);
//五个参数分别是:1-计算机名,
// 2-提示消息,
// 3-从函数执行到关机的时间(单位是秒),
// 4-是否强制关闭应用程序,
// 5-关机还是重启
break;
case '2':
printf("Input the message(Max. 255 chars):");
scanf("%s",msg);
printf("Input the timeout value:");
scanf("%d",&timeout);
EnablePrivilege(NULL,SE_SHUTDOWN_NAME);
ret=InitiateSystemShutdown(NULL,msg,timeout,TRUE,TRUE);
break;
case '3':
printf("Input the remote computer name:");
scanf("%s",cname);
printf("Input the message(Max. 255 chars):");
scanf("%s",msg);
printf("Input the timeout value:");
scanf("%d",&timeout);
EnablePrivilege(cname,SE_REMOTE_SHUTDOWN_NAME);
ret=InitiateSystemShutdown(cname,msg,timeout,TRUE,FALSE);
break;
case '4':
printf("Input the remote computer name:");
scanf("%s",cname);
printf("Input the message(Max. 255 chars):");
scanf("%s",msg);
printf("Input the timeout value:");
scanf("%d",&timeout);
EnablePrivilege(cname,SE_REMOTE_SHUTDOWN_NAME);
ret=InitiateSystemShutdown(cname,msg,timeout,TRUE,TRUE);
break;
default:
break;
}
if(ret)
printf("\nSuccess!\n");
else
printf("\nFail.\n");
return ret;
}

xuv2002 2003-07-17
  • 打赏
  • 举报
回复
void ReBoot() //重新启动计算机
{
ExitWindowsEx(EWX_FORCE,0);
return;
}

假如是win2k
还必须确保有关机权限
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
return false;
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES)NULL, 0);
if (GetLastError() != ERROR_SUCCESS)
return false;
warton 2003-07-17
  • 打赏
  • 举报
回复
he ExitWindows function logs the current user off.

BOOL ExitWindows(

DWORD dwReserved, // reserved
UINT uReserved // reserved
);


Parameters

dwReserved

Reserved; must be zero.

uReserved

Reserved; must be zero.



Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

Windows NT: The ExitWindows function asks applications if they want to terminate by sending the WM_QUERYENDSESSION message to the main window of all running applications.
Windows 95: The ExitWindows function sends the WM_QUERYENDSESSION message to all applications except the one that called ExitWindows.
An application agrees to terminate by returning TRUE when it receives this message (or by allowing the DefWindowProc function to process the message). If any application returns FALSE when it receives the WM_QUERYENDSESSION message, the shutdown is canceled.

After Windows processes the results of the WM_QUERYENDSESSION message, it sends the WM_ENDSESSION message with the wParam parameter set to TRUE if the system is shutting down and to FALSE if it is not.
Windows does not allow new applications to start up during the shutdown process.

See Also

DefWindowProc, ExitWindowsEx

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧