系统关机有 注销、重启、关机、睡眠等选项,不同机器和系统可能不同,如何编程获取有哪些选项?

解决方案 »

  1.   

    注销可以,但重启和关机是分不出来的。至于睡眠,系统应该会给应用程序发POWER之类的消息。
      

  2.   

    BOOL CDlg::OnPower(WPARAM wParam,LPARAM lParam)
    {
    TRACE("LCDDRIVER POWER-----> %x\n",wParam);
    WPARAM ID;
    switch(wParam)
    {
    case PBT_APMQUERYSUSPENDFAILED: //挂起要求被其它程序拒绝
    break;
    case PBT_APMRESUMEAUTOMATIC: //Operation resuming automatically after event. 
    ID=wParam; //操作系统被恢复
    case PBT_APMRESUMESUSPEND: //Operation resuming after suspension. 
    ID=wParam; //系统在唤起后
    if(!m_bUsbConnect){
    m_bUsbConnect=ConnectUsbDevice();
    SetTimer(FLASHTIMER,300000,NULL);
    }
    break;
    case PBT_APMQUERYSUSPEND: //请求允许挂起
    if(m_bUsbConnect)
    {
    m_bUsbConnect = FALSE;
    KillTimer(FLASHTIMER);
    DisconnectUsbDevice();
    TRACE("OnPower------>\n");
    Sleep(500);
    }
    // TRACE("OnPower------>\n");
    break;
    case PBT_APMSUSPEND: //System is suspending operation. 
    ID=wParam; //系统被挂起
    break;
    }
    return(TRUE);
    }
      

  3.   

    BOOL ExitWindowsEx(
      UINT uFlags,
      DWORD dwReason
    );主要就是uFlags
    uFlags 
    [in] Shutdown type. 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. 
    The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Res section.
     
    EWX_REBOOT Shuts down the system and then restarts the system. 
    The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Res 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. 
    The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Res section.Note that specifying this flag will not shut down the power to the system even if it supports the power-off feature. You must specify EWX_POWEROFF to do this.
     
    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.Windows XP: If the computer is locked, and this flag is not specified, the shutdown process will fail.
     
    EWX_FORCEIFHUNG 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.Windows NT and Windows Me/98/95:  This value is not supported. dwReason 
    [in] Reason for initiating the shutdown. This parameter must be one of the system shutdown reason codes.
    If this parameter is zero, the SHTDN_REASON_FLAG_PLANNED reason code will not be set, and therefore the default action is an undefined shutdown that is logged as "No title for this reason could be found". By default, it is also an unplanned shutdown. Depending on how the system is configured, an unplanned shutdown triggers the creation of a file that contains the system state information, which can delay shutdown. Therefore, do not use zero for this parameter.
    Windows 2000, Windows NT, and Windows Me/98/95:  This parameter is ignored.
    Return Values
    If the function succeeds, the return value is nonzero. Because the function executes asynchronously, a nonzero return value indicates that the shutdown has been initiated. It does not indicate whether the shutdown will succeed. It is possible that the system, the user, or another application will abort the shutdown.If the function fails, the return value is zero. To get extended error information, call GetLastError.Res
    The ExitWindowsEx function returns as soon as it has initiated the shutdown process. The shutdown or logoff then proceeds asynchronously. The function is designed to stop all processes in the caller's logon session. Therefore, if you are not the interactive user, the function can succeed without actually shutting down the computer. If you are not the interactive user, use the InitiateSystemShutdown or InitiateSystemShutdownEx function.A non-zero return value does not mean the logoff was or will be successful. The shutdown is an asynchronous process, and it can occur long after the API call has returned, or not at all. Even if the timeout value is zero, the shutdown can still be aborted by applications, services or even the system. The non-zero return value indicates that the validation of the rights and parameters was successful and that the system accepted the shutdown request.When this function is called, the caller must specify whether or not applications with unsaved changes should be forcibly closed. If the caller chooses not to force these applications closed, and an application with unsaved changes is running on the console session, the shutdown will remain in progress until the user logged into the console session aborts the shutdown, saves changes, closes the application, or forces the application to close. During this period, the shutdown may not be aborted except by the console user, and another shutdown may not be initiated.Note that calling this function with the value of the uFlags parameter set to EWX_FORCE avoids this situation. Remember that doing this may result in loss of data.To set a shutdown priority for an application relative to other applications in the system, use the SetProcessShutdownParameters function.During a shutdown or log-off operation, applications that are shut down are allowed a specific amount of time to respond to the shutdown request. If the time expires, the system displays a dialog box that allows the user to forcibly shut down the application, to retry the shutdown, or to cancel the shutdown request. If the EWX_FORCE value is specified, the system always forces applications to close and does not display the dialog box.If the EWX_FORCEIFHUNG value is specified, the system forces hung applications to close and does not display the dialog box.
    Windows Me/98/95:  Because of the design of the shell, calling ExitWindowsEx with EWX_FORCE fails to completely log off the user (the system terminates the applications and displays the Enter Windows Password dialog box, however, the user's desktop remains.) To log off the user forcibly, terminate the Explorer process before calling ExitWindowsEx with EWX_LOGOFF and EWX_FORCE.
    Console processes receive a separate notification message, CTRL_SHUTDOWN_EVENT or CTRL_LOGOFF_EVENT, as the situation warrants. A console process routes these messages to its HandlerRoutine function. ExitWindowsEx sends these notification messages asynchronously; thus, an application cannot assume that the console notification messages have been handled when a call to ExitWindowsEx returns.To shut down or restart the system, the calling process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information, see Running with Special Privileges.
      

  4.   

    在2000下权限不够的话要提高权限,代码如下
    先取得在2000下权限 HANDLE hToken;  
    TOKEN_PRIVILEGES tkp;  

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    MessageBox("OpenProcessToken failed!");

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

    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  

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

    if (GetLastError() != ERROR_SUCCESS)  
    MessageBox("AdjustTokenPrivileges enable failed!");

    if (!ExitWindowsEx(EWX_REBOOT, 0))  
    MessageBox("ExitWindowsEx failed!");--------
    OK,揭贴给分吧