请问,想按下一个按钮,使机子自动重启或关机,
是什么函数?谢谢

解决方案 »

  1.   

    he ExitWindowsEx function either logs off, shuts down, or shuts down and restarts the system. BOOL ExitWindowsEx(    UINT uFlags, // shutdown operation
        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.
      

  2.   

    “系统ShutDown”属于Windows系统的一种基本服务。功能上有“关闭系统”,“注销用户”,“锁定工作站”3中操作。SDK中提供了几个函数,来对此服务进行调用。“关闭系统”功能使计算机可以被安全的关闭。所有在文件系统里缓冲的内容都被强制写入磁盘。然后,显示相应的对话框,提示用户计算机将被关闭或者已经准备好被关闭。可选的情况一般是计算机在关闭后重起,而不是直接切断电源。如果一个进程调用“注销”功能函数,则该进程所在的安全环境范围内的所有进程都被终止,使当前的用户退出系统。一个登陆对话框被显示,期待新用户的登陆。“锁定工作站”功能使你可以在离开计算机的时候,保护计算机屏幕不被未授权的用户看到。要解除锁定,必须用管理员或着授权用户的帐号和密码重新登陆。
    如何关闭系统: 程序可以用两种方式关闭本地或远程计算机
    直接关闭系统
    关闭系统并重启
    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函数即可锁定工作站。系统会显示一个锁定对话框,告诉用户此工作站正在使用并且已经被锁定,可以被执行锁定的用户或管理员解锁,解锁的方式是按下
      

  3.   

    您在CSDN上搜一下就可以找到。
      

  4.   

    win32check(exitwindows(0,0);
    将调用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 权限