我想用API设计一个简单的关机程序,有文本框,输入时间(小时/分),然后一个确定按钮,到设定的时候,电脑就自动关机。
我想请问大家关机的METHOD或函数是什么?对这个程序在编程上有什么宝贵的设想请大家提出来,小弟不胜感激,
感谢大家关照我这个菜鸟小弟

解决方案 »

  1.   

    void CRebootDlg::OnOK() 
    {
    // TODO: Add extra validation here
    HANDLE hToken;
        TOKEN_PRIVILEGES tkp;
    DWORD dwVersion;

    dwVersion = GetVersion(); //获得Windows NT或Win32的版本号 switch(IsHow)
    {
    case 0://注销
    ExitWindowsEx(EWX_LOGOFF,0);
    break; case 1://重新启动
    if (dwVersion < 0x80000000)  
    {// Windows NT系列
                OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
                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);            ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0);
    }
    else
    {//Windows 95系列
                ExitWindowsEx(EWX_FORCE | EWX_REBOOT,0);
    }
    break; case 2://关闭计算机
        if (dwVersion < 0x80000000) 
    {// Windows NT
        OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
                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);            ExitWindowsEx(EWX_SHUTDOWN |EWX_FORCE, 0);
    }
    else
    {
                ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN,0);
    }
    break;
        }
    }
      

  2.   

    Shutting DownYou can use the ExitWindowsEx function to shut down the system. Shutting down flushes file buffers to disk and brings the system to a condition in which it is safe to turn off the computer.
    The following example enables the SE_SHUTDOWN_NAME privilege and then shuts down the system.BOOL MySystemShutdown()
    {
       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); 
     
       if (GetLastError() != ERROR_SUCCESS) 
          return FALSE; 
     
       // Shut down the system and force all applications to close. 
     
       if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0)) 
          return FALSE;    return TRUE;
    }
    For more information about setting security privileges, see Privileges.
      

  3.   

    System Shutdown FunctionsThe 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. 
      

  4.   

    以上是模拟关机对话框
    定时用SETTIMER定期检查是否到指定时间就行
      

  5.   

    rundll32 user.exe,exitwindows搞得那么复杂干什么?