这个程序是这样的:它能够检测到正在运行的程序有哪些,然后按照用户选定的某个应用程序结束运行时关掉计算机。比如金山影霸可以设定播放完后自动关机,但是powerdvd等就没这个功能,所以,我想自己做一个,以便我用什么播放器都可以实现自动关机,或者某个我打开的程序运行完毕后也可以自动关机。请高手指点一下啊!

解决方案 »

  1.   

    用findwindow查找你设定的程序的主窗口
    直到找不到了就exitwindows...或者用HOOK来侦测你设定的程序的消息一侦测到退出了就exitwindows...
      

  2.   

    ExitWindows && ExitWindowsEx
      

  3.   

    /********************************************************************
    函数:OnTimer(UINT nIDEvent)
    参数:UINT nIDEvent--详情参见MSDN
    功能:用于在程序中显示系统时间并监控系统时间是否到了关机时间,是的化
          关机.可用于windows2000/nt 或 windows9x
    历史纪录:ANDY-26/4/2002  15/5/2002最后修改 
    ********************************************************************/
    void CShutDownDlg::OnTimer(UINT nIDEvent) 
    {
    // TODO: Add your message handler code here and/or call default
        SYSTEMTIME  st;
    char time[30];
    ::GetLocalTime(&st); 
        ::wsprintf(time," %d-%d-%d ",st.wYear,st.wMonth,st.wDay);
        ::GetTimeFormat(NULL,TIME_FORCE24HOURFORMAT,&st,"HH':'mm':'ss",time,50); 
        GetDlgItem(IDC_STATIC_TIME)->SetWindowText(time); 
        
    /////////////////////////
    if(m_isbegin&&(m_hour==st.wHour)&&(m_minute==st.wMinute))//当关机标志为开并且当自动关机时间到了//的时候关机。 

    HANDLE hToken;
            TOKEN_PRIVILEGES tkp;
    /*The TOKEN_PRIVILEGES structure contains information about a set of privileges for an access token.
    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);
    }  CDialog::OnTimer(nIDEvent);
    }