因为某些原因,在计划任务中添加的定时关机,不能关闭电脑。
分析原因可能是因为某些进程没有关闭或者说关闭不了。查阅了一些资料,了解到按Ctrl键关机可以绕过结束进程这个过程。但我实际的环境不能人为参与,所以想用编程解决。
求指教。最好不要模拟键盘那样的方式,当然如果没有更好的方法,也可以。为避免浪费可用分先发50,如果好用,或者提供好的可行思路,必加分。再次感谢您的关注。

解决方案 »

  1.   

    试试用shutdown命令?应该可以强制关机吧
      

  2.   

    搜了一下,你可以使用底层键盘钩子,再配合shutdown命令,应该不会有什么太大难度
      

  3.   

    #1 得分:0 回复于: 2013-02-04 10:55:26 
    试试用shutdown命令?应该可以强制关机吧 
      

  4.   

    直接命令行 shutdown.exe -s
    看是否可以满足条件,可以的话,就可以CreateProcess等来调用
      

  5.   

    使用Windows未公开APIconst int SE_SHUTDOWN_PRIVILEGE = 0x13;  
    typedef int (__stdcall *PFN_RtlAdjustPrivilege)( INT, BOOL, BOOL, INT*);  
    typedef int (__stdcall *PFN_ZwShutdownSystem)(INT);  
    HMODULE hModule = ::LoadLibrary(_T("ntdll.dll"));  
    if( hModule != NULL)  
    { PFN_RtlAdjustPrivilege pfnRtl = (PFN_RtlAdjustPrivilege)GetProcAddress( hModule, "RtlAdjustPrivilege"); PFN_ZwShutdownSystem pfnShutdown = (PFN_ZwShutdownSystem)GetProcAddress( hModule,"ZwShutdownSystem");  
    if( pfnRtl != NULL & pfnShutdown != NULL )  
    {  
    int en = 0;  
    int nRet= pfnRtl( SE_SHUTDOWN_PRIVILEGE, TRUE, TRUE, &en);  
    if( nRet == 0x0C000007C ) nRet = pfnRtl(SE_SHUTDOWN_PRIVILEGE, TRUE, FALSE, &en); //SH_SHUTDOWN = 0; //SH_RESTART = 1; //SH_POWEROFF = 2;   
    const int SH_POWEROFF = 2; nRet = pfnShutdown(POWEROFF);  
    }  
    }
      

  6.   

    请无视上面的代码,请参考以下整理后的代码:
    BOOL MyShutdown()
    {
    const int SE_SHUTDOWN_PRIVILEGE = 0x13;  
    typedef int (__stdcall *PFN_RtlAdjustPrivilege)( INT, BOOL, BOOL, INT*);  
    typedef int (__stdcall *PFN_ZwShutdownSystem)(INT);  
    HMODULE hModule = ::LoadLibrary(_T("ntdll.dll"));  
    if( hModule != NULL)  
    {
    PFN_RtlAdjustPrivilege pfnRtl = (PFN_RtlAdjustPrivilege)GetProcAddress( hModule, "RtlAdjustPrivilege");
    PFN_ZwShutdownSystem pfnShutdown = (PFN_ZwShutdownSystem)GetProcAddress( hModule,"ZwShutdownSystem");  
    if( pfnRtl != NULL && pfnShutdown != NULL )  
    {
    int en = 0;  
    int nRet= pfnRtl( SE_SHUTDOWN_PRIVILEGE, TRUE, TRUE, &en);  
    if( nRet == 0x0C000007C )
    nRet = pfnRtl(SE_SHUTDOWN_PRIVILEGE, TRUE, FALSE, &en);
    //const SH_SHUTDOWN = 0;
    //const SH_RESTART = 1;
    const SH_POWEROFF = 2;
    nRet = pfnShutdown(POWEROFF);
    return nRet; 
    }
    }
    return FALSE;
    }