请教如何在应用程序中关闭或重新启动计算机?

X_FILE 2000-03-26 01:49:00
加精
...全文
490 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
depuw 2001-06-15
  • 打赏
  • 举报
回复
关于在应用程序中关闭或启计算机,用ExitWindowEx可以在Win9x和NT,Win2000下实现!
下面告诉具体方法!
在Win9x下,可以直接用API ExitWindowEx函数就可以了,但对于NT,WIN2000就不行了,你得在View菜单中选择ClassWizard,然后在Message Maps中的Object IDS和Messages项中分别选择你的应用程序的窗口类,WM_CREATE.增加函数,内容如下:(我自己试了,运行良好!)
int CTimeSystemDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here
OSVERSIONINFO verinfo;
verinfo.dwOSVersionInfoSize=sizeof(OSVERSIONINFO);
if(!::GetVersionEx(&verinfo))
{
AfxMessageBox("Get version Error!");
return -1;
}
if(verinfo.dwPlatformId==VER_PLATFORM_WIN32_NT)
{
HANDLE htoken;
DWORD retv;
TOKEN_PRIVILEGES tkp;
if(!::OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&htoken))
{
AfxMessageBox("OpenProcessToken Error!");
return -1;
}
tkp.PrivilegeCount=1;
if(!LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&(tkp.Privileges[0].Luid)))
{
AfxMessageBox("Lookup Privilege Value Error");
return -1;
}
tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
if(!AdjustTokenPrivileges(htoken,false,&tkp,sizeof(tkp),&tkp,&retv))
{
AfxMessageBox("");
return -1;
}
}
return 0;
}
csdn2000 2000-04-01
  • 打赏
  • 举报
回复
在程序中发布命令"rundll32.exe user.exe,exitwindows"。BTW,你可将
"rundll32.exe user.exe,exitwindows" 命令作为关机的快捷方式
DragonCheng 2000-04-01
  • 打赏
  • 举报
回复
也可以用 InitiateSystemShutdown(NULL,NULL,0,TRUE,TRUE);
lu0 2000-03-26
  • 打赏
  • 举报
回复
NT下需要SE_SHUTDOWN_NAME特权.需要先打开该特权才行.只有授权的用户才可以
SHUTDOWN.
dqj 2000-03-26
  • 打赏
  • 举报
回复
.
coala 2000-03-26
  • 打赏
  • 举报
回复
哎,轮不到我了!
Firing_Sky 2000-03-26
  • 打赏
  • 举报
回复
NT 用 ExitWindows
MMX 2000-03-26
  • 打赏
  • 举报
回复
那么多话,说用ExitWindowsEx(1,1)实现系统重启不就行了?不过此函数只能用于Win9X系统,NT无效。(2000我不知道,应该也不行吧)
tide 2000-03-26
  • 打赏
  • 举报
回复


ExitWindowsEx
下面是从msdn 上copy 下来的。在应用程序中关闭或重新启动计算机,这个函数已经绰绰有余了,但重新启动windows 还有许多工作要做。

The ExitWindowsEx function either logs off the current user, shuts down the system, or shuts down and restarts the system. It sends the WM_QUERYENDSESSION message to all applications to determine if they can be terminated.

BOOL ExitWindowsEx(
UINT uFlags, // shutdown operation
DWORD dwReserved // reserved
);

Parameters
uFlags
Specifies the type of shutdown. This parameter must include one of the following values: Value Meaning
EWX_LOGOFF Shuts down all processes running in the security context of the process that called the ExitWindowsEx function. Then it logs the user off.
EWX_POWEROFF Shuts down the system and turns off the power. The system must support the power-off feature.
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.

EWX_REBOOT Shuts down the system and then restarts the system.
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.

EWX_SHUTDOWN Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped.
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.



This parameter can optionally include the following values: Value Meaning
EWX_FORCE Forces processes to terminate. When this flag is set, the system does not send the WM_QUERYENDSESSION and WM_ENDSESSION messages. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.
EWX_FORCEIFHUNG Windows NT 5.0 and later: Forces processes to terminate if they do not respond to the WM_QUERYENDSESSION or WM_ENDSESSION message. This flag is ignored if EWX_FORCE is used.



dwReserved
Reserved; this parameter is ignored.
Return Values

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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