我刚接触VC,想编个定时关机的软件,但水平太低,弄了几个星期都没见影,希望有好心人伸一下援助之手。

解决方案 »

  1.   

    以下代码可以关机: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"); 
      

  2.   

    做一个定时器 然后判断时间 到时间关机
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    //定义变量
    OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken);
    //OpenProcessToken()这个函数的作用是打开一个进程的访问令牌
    //GetCurrentProcess()函数的作用是得到本进程的句柄
    LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
    //LookupPrivilegeValue()的作用是修改进程的权限
    tkp.PrivilegeCount = 1; 
    //赋给本进程特权
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
    //AdjustTokenPrivileges()的作用是通知Windows NT修改本进程的权利 ExitWindowsEx(EWX_POWEROFF|EWX_FORCE,0);