Windows服务管理类

头文件
Code

 

CPP实现文件:

 

Code

 

使用示例:

...
#include "MyServiceStatus.h"
class CNTServCtrlDlg : public CDialog
{
...
private:
 CMyServiceStatus m_MyServiceStatus;

}

////////////////////////////////////////////////////////////
CNTServCtrlDlg::CNTServCtrlDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CNTServCtrlDlg::IDD, pParent),m_MyServiceStatus("FileSync")
{
 //{{AFX_DATA_INIT(CNTServCtrlDlg)
  // NOTE: the ClassWizard will add member initialization here
 //}}AFX_DATA_INIT
 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    m_uiTimer = 0;
}

BOOL CNTServCtrlDlg::OnInitDialog()
{
 CDialog::OnInitDialog();
 CenterWindow();
 
 m_MyServiceStatus.Run();//启动查询状态线程
 return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNTServCtrlDlg::OnTimer(UINT nIDEvent)
{
 // Update the status
 ShowState();
}

 

void CNTServCtrlDlg::ShowState()
{  
   
    DWORD dwState = 0xFFFFFFFF;
 
 char buf[64];
 dwState = m_MyServiceStatus.m_dwServiceStatus;
    switch (dwState) {
    case SERVICE_STOPPED:
        strcpy(buf, "Stopped");
        break;
    case SERVICE_START_PENDING:
        strcpy(buf, "Start pending");
        break;
    case SERVICE_STOP_PENDING:
        strcpy(buf, "Stop pending");
        break;
    case SERVICE_RUNNING:
        strcpy(buf, "Running");
        break;
    case SERVICE_CONTINUE_PENDING:
        strcpy(buf, "Continue pending");
        break;
    case SERVICE_PAUSE_PENDING:
        strcpy(buf, "Pause pending");
        break;
    case SERVICE_PAUSED:
        strcpy(buf, "Paused");
        break;
    default:
  if(!m_MyServiceStatus.IsServiceInstalled())
   strcpy(buf,"Service Not Installed");
  else
   strcpy(buf, "<Unknown>");
        break;
    }
    SetDlgItemText(IDC_STATE, buf);
}

void CNTServCtrlDlg::OnDestroy()
{
 m_MyServiceStatus.Stop();//结束CMyServiceStatus中的查询状态线程
 CDialog::OnDestroy();

 if (m_uiTimer != 0) {
     KillTimer(1);
 } 
}

 

posted @ 2009-10-20 11:47  吾非无心  阅读(234)  评论(0编辑  收藏  举报