请问如何在windows应用程序中关闭计算机?
相关的函数是什么?

解决方案 »

  1.   

    ExitWindowsEx(EWX_POWEROFF,EWX_FORCEIFHUNG);
      

  2.   

    这是一段关机代码 2000和98都可以

    HANDLE hToken;
            TOKEN_PRIVILEGES tkp;
    /*The TOKEN_PRIVILEGES structure contains 
    typedef struct _TOKEN_PRIVILEGES { 
                             DWORD PrivilegeCount; 
                             LUID_AND_ATTRIBUTES Privileges[ANYSIZE_ARRAY]; 
                            } TOKEN_PRIVILEGES, *PTOKEN_PRIVILEGES; */
    //        BOOL fResult;
    m_dwVersion=::GetVersion();//获取当前系统版本信息
            if(m_dwVersion< 0x80000000)//windows2000/nt
    {
                //打开与当前进程相关联的存取标识
                if (!::OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken))
                   AfxMessageBox("OpenProcessToken failed");
                 //查出本机系统的当前特权的Luid(对于本程序来说就是得到有关机特权的用户id)
                 ::LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
     /*The LookupPrivilegeValue function retrieves the locally unique identifier (LUID) used on a 
     specified system to locally represent the specified privilege name. */
                 tkp.PrivilegeCount =1;
                 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
                 ::AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
     /*The AdjustTokenPrivileges function enables or disables privileges in the specified access token. 
     Enabling or disabling privileges in an access token requires TOKEN_ADJUST_PRIVILEGES access. 
     参数说明:(详见msdn)
     HANDLE TokenHandle,              // handle to token
                 BOOL DisableAllPrivileges,       // disabling option
                 PTOKEN_PRIVILEGES NewState,      // privilege information
                 DWORD BufferLength,              // size of buffer
                 PTOKEN_PRIVILEGES PreviousState, // original state buffer
                 PDWORD ReturnLength              // required buffer size*/
                 if (::GetLastError()!=ERROR_SUCCESS)//失败
                    AfxMessageBox("AdjustTokenPrivileges disable failed");
             ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0);
     /*调用ExitWindowsEx关闭机器,Windows NT/2000: To shut down or restart the system, the calling
     process must use the AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. */ 

      else//windows98
    ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE,0);//调用ExitWindowsEx关闭机器。 
    PostQuitMessage(0);