各位大虾,vc里经常使用的关闭计算机的函数有哪些,哪个比较好用,谢谢了先。

解决方案 »

  1.   

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

  2.   

    ExitWindowsEx 9X 直接关机.但是在NT板需要得到关机权限
      

  3.   

    关闭计算机  此功能是通过shell32.dll中一个索引号为60的API函数调用,显示"关闭Windows"对话窗口实现的。  具体方法为IDC_SHUTDOWNCOMPUTER按钮添加BN_CLICKED消息处理函数:  void CControlDlg::OnShutdowncomputer()   {  HINSTANCE hInst=LoadLibrary("shell32.dll");//装入shell32.dll  SHUTDOWNDLG ShutDownDialog;//指向shell32库中显示关机对话框函数的指针  if(hInst!=NULL)  {  //获得函数的地址并调用之  ShutDownDialog=(SHUTDOWNDLG)GetProcAddress(hInst,(LPSTR)60);//  (*ShutDownDialog)(0);  }  }  函数中的SHUTDOWNDLG定义如下:  typedef int (CALLBACK *SHUTDOWNDLG)(int);//显示关机对话框函数的指针
      

  4.   

    这段代适用于win98/me/2000/winxp
             HANDLE hToken; 
    TOKEN_PRIVILEGES tkp; 
    // Get a token for this process. 
    if ( !OpenProcessToken( GetCurrentProcess(), 
    TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken) ) 
    return FALSE; 
    // 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) 
    return FALSE; 
    // Shut down the system and force all applications to close. 
    if ( !ExitWindowsEx( EWX_POWEROFF | EWX_FORCE, 0) ) 
    return FALSE; return TRUE;
      

  5.   

    System Shutdown Functions
    The following functions are used with system shutdown. Function Description 
    AbortSystemShutdown Stops a system shutdown started by using the InitiateSystemShutdown function. 
    ExitWindows Logs off the current user. 
    ExitWindowsEx Either logs off the current user, shuts down the system, or shuts down and restarts the system. 
    InitiateSystemShutdown Initiates a shutdown and optional restart of the specified computer. 
    InitiateSystemShutdownEx Initiates a shutdown and optional restart of the specified computer. 
    LockWorkStation Locks the workstation's display, protecting it from unauthorized use. 
      

  6.   

    win98下:BOOL ExitWindowsEx(
      UINT uFlags,       // shutdown operation
      DWORD dwReserved   // reserved
    );win2000下: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("切换系统级权限失败!");
    }     fResult =InitiateSystemShutdown( 
                 NULL,                                  // 要关的计算机用户名
                 "关机时间已到,WINDOWS将在上面的时间内关机,请做好保存工作!",  // 显示的消息
                 10,                                    // 关机所需的时间
                 TRUE,                                 // ask user to close apps 
                 FALSE);                               //设为TRUE为重起,设为FALSE为关机
        if(!fResult) 

                 MessageBox("初始化系统关机失败!"); 
    }      tkp.Privileges[0].Attributes = 0; 
            AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);      if (GetLastError() != ERROR_SUCCESS) 
    {
                 MessageBox("AdjustTokenPrivileges disable failed."); 
    }      ExitWindowsEx(EWX_SHUTDOWN,0);