请问,想按下一个按钮,使机子自动重启或关机,
是什么函数?谢谢
是什么函数?谢谢
解决方案 »
- 新手问题~ 怎么理解“断点”这个词?实在摸不着头脑
- 这个一般是什么问题啊?
- 我对Delphi里面的TChart控件不熟悉,请问怎么控制 TChartSeries呀,比如怎么控制颜色,曲线表的高度等,谢谢,拜托(还可以在加分)
- 这个问题并不容易,不信你们去做一个试试
- 如何调试三层程序的中间层呢?
- delphi 开发服务端 有什么开源的模型吗?
- 制作Delphi6的安装程序哪有下载?
- 这个问题困绕了我几天了,寻求帮助。
- dspack如何设置摄像头视频质量
- delphi菜鸟 求大神指点
- adoquery的state问题
- 为什么try..except..在调试过程中报错,而直接运行项目.exe中却运行正常?
DWORD dwReserved // reserved
);
ParametersuFlagsSpecifies the type of shutdown. This parameter must be some combination of the following values: Value Meaning
EWX_FORCE Forces processes to terminate. When this flag is set, Windows does not send the messages WM_QUERYENDSESSION and WM_ENDSESSION to the applications currently running in the system. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.
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 Res section. Windows 95: Security privileges are not supported or required.
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 Res section. Windows 95: Security privileges are not supported or required.
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 Res section. Windows 95: Security privileges are not supported or required.
dwReservedReserved; this parameter is ignored. Return ValuesIf the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
如何关闭系统: 程序可以用两种方式关闭本地或远程计算机
直接关闭系统
关闭系统并重启
Windows NT/2000 及后续版本: 程序必须拥有SE_SHUTDOWN_NAME权限才能成功调用关闭函数。
ExitWindowsEx函数可以用来关闭系统。如函数成功调用,系统对每个窗口发送WM_QUERYENDSESSION 消息,询问窗口所属的程序是否可以被终止。收到此消息的程序应该进行响应,清除环境释放资源,然后返回TRUE表示自己可以被终止。然而调用ExitWindowEx的时候如果指定了EXW_FORCE,则系统强行终止相关的进程并关闭,这样可能导致数据的丢失。这是一段在NT/2000中调用ExitWindowEx关闭系统的程序(强制关闭所有程序)。在windows95/98/me中直接调用ExitWindowEx即可。
-----------------------------------------------------------------------------------------------------------
HANDLE hToken;
TOKEN_PRIVILEGES tkp; // Get a token for this process. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
Error("OpenProcessToken"); // 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); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) error("AdjustTokenPrivileges"); // Shut down the system and force all applications to close. if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) error("ExitWindowsEx");-----------------------------------------------------------------------------------------------------------Windows NT/2000以及后续版本: InitiateSystemShutdown函数可以指定一段延时,在进行延时计数的时候,在将被关闭的目标计算机上显示一个对话框,提示用户尽快注销。一旦计数结束,系统则立刻被关闭。在此之前,可以调用AbortSystemShutdown函数停止计数,取消相应的关闭操作。InitiateSystemShutdown也可以指定让系统重启。
InitiateSystemShutdown有一个参数LPTSTR lpMachineName,可以指定为网络上的计算机名字,也就是说,可以关闭网络上的他计算机(如果你的用户在该计算机上有足够的权限的话)。以下这个例子调用InitiateSystemShutdown函数关闭用户已经登陆的本地计算机(要关闭远程计算机将InitSystemShutdown第一个参数由NULL改为正确的计算机名字或)。同样的,也需要先获得SE_SHUTDOWN_NAME权限。
---------------------------------------------------------------------------------------------------------HANDLE hToken; // handle to process token TOKEN_PRIVILEGES tkp; // pointer to token structure BOOL fResult; // system shutdown flag // Get the current process token handle so we can get shutdown // privilege. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) ErrorHandler("OpenProcessToken failed."); // Get the LUID for 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 shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0); // Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS) ErrorHandler("AdjustTokenPrivileges enable failed."); // Display the shutdown dialog box and start the time-out countdown. fResult = InitiateSystemShutdown( NULL, // shut down local computer "Click on the main window and press \the Escape key to cancel shutdown.", // message to user 20, // time-out period FALSE, // ask user to close apps TRUE); // reboot after shutdown if (!fResult) {ErrorHandler("InitiateSystemShutdown failed."); }
// Disable shutdown privilege. tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
(PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS) {ErrorHandler("AdjustTokenPrivileges disable failed.");
} ---------------------------------------------------------------------------------------------
而使用AbortSystemShutoown取消InitialSystemShutdown操作的代码如下(记住要在延时结束前执行才能起作用
// Get the current process token handle so we can get shutdown // privilege.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
ErrorHandler("OpenProcessToken failed."); }// Get the LUID for 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 shutdown privilege for this process. AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
// Cannot test the return value of AdjustTokenPrivileges. if (GetLastError() != ERROR_SUCCESS)
{ErrorHandler("AdjustTokenPrivileges enable failed.");
}
// Prevent the system from shutting down.
fResult = AbortSystemShutdown(NULL);
if (!fResult)
{
ErrorHandler("AbortSystemShutdown failed.");
} // Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
if (GetLastError() != ERROR_SUCCESS) {ErrorHandler("AdjustTokenPrivileges disable failed."); }reak;
关于用户权限(Privileges)的详细信息,可以参见MSDN
如何注销当前用户
可以使用ExitWindows或ExitWindowsEx函数注销当前用户。
在默认的情况下,当程序调用ExitWindows或ExitWindowsEx注销的时候,WM_QUERYENDSESSION消息也被发送到系统内的每个窗口。窗口所属的程序响应此消息并且返回TRUE表示可以被关闭,如果任意一个程序返回FALSE,注销操作将被取消。Windows NT/2000以及后续版本: 当一个程序响应WM_QUERYENDSESSION并返回TRUE,那么它就会立刻接受到WM_ENDSESSION消息并马上结束,而不管其他的程序是怎么样回应WM_QUERYENDSESSION消息的。Windows 95/98/Me:只有当系统内所有的程序都对WM_QUERYENDSESSION消息回应TRUE之后,他们才会一起接受到WM_ENDSESSION 消息,然后结束。
如果要强制关闭所有的程序,使用ExitWindowsEx函数,指定EXW_FORCE标志。如果这样做,系统不发送WM_QUERYENDSESSION 消息,而直接终止正在运行的程序。
在注销的时候,系统还向每一个进程发送CTRL_LOGOFF_EVENT 控制码。控制台程序可以注册一个HandlerRoutine例程来处理这个控制码(使用SetConsoleCtrlHandler函数)。(更多的关于控制台的控制码,参见MSDN里“HandlerRoutine”有关章节)
综上所述:只有所有的程序都允许退出,注销操作才能成功。如果某一个程序响应WM_QUERYENDSESSION并返回FALSE ,用户则不能被注销。这样就可以写出防止用户注销或关闭的程序(非强制情况)。//这是注销当前用户的代码ExitWindows(0, 0); //响应WM_QUERYENDSESSION消息,如果在弹出消息框里选择NO,则取消注销。case WM_QUERYENDSESSION: {
int r; r = MessageBox(NULL, "Shut down?","WM_QUERYENDSESSION", MB_YESNO); // Return TRUE to allow shutdown, FALSE to stop. return r == IDYES; break; }
如何锁定工作站 使用LockWorkStation函数即可锁定工作站。系统会显示一个锁定对话框,告诉用户此工作站正在使用并且已经被锁定,可以被执行锁定的用户或管理员解锁,解锁的方式是按下
将调用E x i t Wi n d o w s ( )
注销当前用户,然后以另外一个用户身份登录。
win32check(exitwindowsEX(EXW_REBOOT,0);
退出Wi n d o w s 然后重新启动计算机。
win32check(exitwindowsEX(EXW_shutdown,0);
选项退出并关闭系统,这样用户就可以断开电源。
不论是E x i t Wi n d o w s ( )还是E x i t Wi n d o w s E x ( )函数,如果调用成功就返回Tr u e ,否则就返回F a l s e 。注意如果运行在Windows NT 环境下,使用E x i t Wi n d o w s E x ( )函数并不会关闭系统;这需要特
殊的权限。必须使用Win32 API 函数A d j u s t To k e n P r i v l e g e s ( )授予S E _ S H U T D O W N _ N A M E 权限