將基於MFC的對話框應用程序修改爲服務程序---------

將基於MFC的對話框應用程序修改爲服務程序



轉載自:http://bbs.csdn.net/topics/380084096


Windows(NT/2000)下有很多服務程序(它們在系統登錄前運行),它們一般都沒有界面,我們可以在服務管理器(運行services.msc)中啓動和關閉它們。下面我試着修改一個有界面的MFC對話框程序,使它成爲一個服務程序。網上提到了一種方法就是,從建立一個COM服務程序入手,然後將一個MFC項目改造成服務程序,最後讓這一程序在啓動時可以顯示圖形界面。這種方法的優點就是,程序嚮導已經幫我們寫好了服務程序的主要代碼,我們的任務就是把它們移植到MFC程序中(這個方法很不錯!)。我的方法和這種方法思想基本一致,但也不完全一樣。我是直接將有些寫在CUI服務程序中的代碼移植過來。主要思想就是把主服務函數等定義爲全局函數,這樣在主對話框類中就可以訪問它們了。


此程序需要注意的地方: 
一次只能安裝一個服務,如果已安裝過一個服務,先將其卸載再安裝其他服務,設置其他應用程序時,在SCM啓動後,因沒有相應啓動請求會被kill掉。本來以爲任何程序都可以被設置爲服務程序,後來實驗發現,一般的應用程序被設置爲服務程序後,由於它不能夠與SCM進行通信,所以SCM無法將其啓動。錯誤提示如下:(下面wcdj是我的服務名字)

本地計算機無法啓動wcdj服務
錯誤1053:服務沒有及時響應啓動或控制請求



這個服務程序的主要流程如下:

 

     SERVICE_TABLE_ENTRY DispatchTable[]={{"Service1",ServiceMain},{NULL,NULL}};  
     if (!StartServiceCtrlDispatcher(DispatchTable))
     {  
           AfxMessageBox("當不是用SCM啓動程序的時候,程序執行下面的代碼");
           ...
           //顯示我們服務程序的對話框
           CTestDlg dlg;
           //m_pMainWnd = &dlg;
           int nResponse = dlg.DoModal();
           if (nResponse == IDOK)
          {
                   // TODO: Place code here to handle when the dialog is
                   //  dismissed with OK
           }
           else if (nResponse == IDCANCEL)
          {
                   // TODO: Place code here to handle when the dialog is
                   //  dismissed with Cancel
           }
     }

否則程序會執行回調函數:

    void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
    { 
          AfxMessageBox("當用SCM啓動程序的時候,程序執行下面的代碼");

          //初始化
          m_ServiceStatus.dwServiceType        = SERVICE_WIN32; 
          m_ServiceStatus.dwCurrentState       = SERVICE_START_PENDING; 
          ...    
 
         while(1)
        {  
                //Sleep(3000); 
                //Place Your Code for processing here....

                //顯示我們服務程序的對話框(當SCM啓動服務程序的時候(系統重啓時或手動在SCM中啓動時),也讓它顯示主對話框界面)
               

                CTestDlg dlg;
               //m_pMainWnd = &dlg;
                int nResponse = dlg.DoModal();
                if (nResponse == IDOK)
               {
                         // TODO: Place code here to handle when the dialog is
                         //  dismissed with OK
                }
                else if (nResponse == IDCANCEL)
                {
                         // TODO: Place code here to handle when the dialog is
                         //  dismissed with Cancel
                }
                ...
        }

}


具體的細節看下面的步驟,主要步驟如下:


(1)首先生成一個基於對話框的應用程序框架,假設我的工程名稱爲test。

 

(2)在test.cpp中添加幾個全局變量和幾個全局函數。

//設置兩個全局變量 
SERVICE_STATUS m_ServiceStatus;
SERVICE_STATUS_HANDLE m_ServiceStatusHandle;

//添加幾個全局函數
//////////////////////////////////////////////////////////////////////////
//函數聲明

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
BOOL InstallService(CString &strPath);
BOOL DeleteService();

//////////////////////////////////////////////////////////////////////////
//函數定義

void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{

//     DWORD status; 
//     DWORD specificError; 
 
    AfxMessageBox("當用SCM啓動程序的時候,程序執行下面的代碼");
 
    m_ServiceStatus.dwServiceType        = SERVICE_WIN32; 
    m_ServiceStatus.dwCurrentState       = SERVICE_START_PENDING; 
    m_ServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP; 
    m_ServiceStatus.dwWin32ExitCode      = 0; 
    m_ServiceStatus.dwServiceSpecificExitCode = 0; 
    m_ServiceStatus.dwCheckPoint         = 0; 
    m_ServiceStatus.dwWaitHint           = 0; 
 
    m_ServiceStatusHandle = RegisterServiceCtrlHandler("Service1",ServiceCtrlHandler);  
    if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) 
    { 
        AfxMessageBox("Handler not installed");
  return; 
    }     
 
    m_ServiceStatus.dwCurrentState       = SERVICE_RUNNING; 
    m_ServiceStatus.dwCheckPoint         = 0; 
    m_ServiceStatus.dwWaitHint           = 0;  
    if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus)) 
    {  
    } 
 
 //bRunning=true;
 //while(bRunning)
 while(1)
 {  
  //Sleep(3000); 
  //Place Your Code for processing here....  

  CTestDlg dlg;
  //m_pMainWnd = &dlg;
  int nResponse = dlg.DoModal();
  if (nResponse == IDOK)
  {
   // TODO: Place code here to handle when the dialog is
   //  dismissed with OK
  }
  else if (nResponse == IDCANCEL)
  {
   // TODO: Place code here to handle when the dialog is
   //  dismissed with Cancel
   //AfxMessageBox("cancel");
   //break;//跳出while循環

   m_ServiceStatus.dwCurrentState       = SERVICE_STOPPED; 
   m_ServiceStatus.dwCheckPoint         = 0; 
   m_ServiceStatus.dwWaitHint           = 0;  
   if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus)) 
   {  
   }
   
   exit(0);//後面退出不了,只能強行退出
  }
  
#ifdef _temp_delete//使用下面代碼,只能關閉對話框不能關閉程序
  
  //關閉對話框用,傳遞消息
  MSG msg;
  while (GetMessage(&msg, 0, 0, 0))
  {  
    if (msg.message)
    {
     CString strMsg;
     strMsg.Format("%d",msg.message);
     AfxMessageBox(strMsg);    
    }
   DispatchMessage(&msg);
  }
#endif
 }

    return; 
}

 

void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
    switch(Opcode) 
    { 
 case SERVICE_CONTROL_PAUSE: 
  m_ServiceStatus.dwCurrentState = SERVICE_PAUSED; 
  break; 
  
 case SERVICE_CONTROL_CONTINUE: 
  m_ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
  break; 
  
 case SERVICE_CONTROL_STOP: 
  m_ServiceStatus.dwWin32ExitCode = 0; 
  m_ServiceStatus.dwCurrentState  = SERVICE_STOPPED; 
  m_ServiceStatus.dwCheckPoint    = 0; 
  m_ServiceStatus.dwWaitHint      = 0; 
  
  SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
  
  //bRunning=false;
  
  break;
  
 case SERVICE_CONTROL_INTERROGATE: 
  break; 
    }      
    return; 
}

 

BOOL InstallService(CString &strPath)//無法創建其他應用程序爲服務,因爲它們不能響應啓動請求
{
 
 //char strDir[1024]={0};
 HANDLE schSCManager,schService;
 
//   GetCurrentDirectory(1024,strDir);
//   strcat(strDir,"
 //test.exe "); 
 
 schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);  
 
 if (schSCManager == NULL) 
  return false;
 
    //LPCTSTR lpszBinaryPathName=strDir;
 LPCTSTR lpszBinaryPathName;
 if (strPath=="")
 {
  AfxMessageBox("You must tell me Exepath!");
  return FALSE;
 } 
 else
 {
  lpszBinaryPathName=strPath;
 }

     schService = CreateService(schSCManager,"Service1","wcdj",// service name to display 
        SERVICE_ALL_ACCESS,        // desired access 
        SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS, // service type 
        //SERVICE_DEMAND_START,      // start type 
        SERVICE_AUTO_START,        //系統啓動時自動啓動
        SERVICE_ERROR_NORMAL,      // error control type 
        lpszBinaryPathName,        // service's binary 
        NULL,                      // no load ordering group 
        NULL,                      // no tag identifier 
        NULL,                      // no dependencies 
        NULL,                      // LocalSystem account 
        NULL);                     // no password 
 
    if (schService == NULL) 
        return false;  
 
    CloseServiceHandle(schService); 
 
 return true;
}

 

BOOL DeleteService()
{
 HANDLE schSCManager;
 SC_HANDLE hService;
 
 schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
 
 if (schSCManager == NULL) 
  return false; 
 
 hService=OpenService(schSCManager,"Service1",SERVICE_ALL_ACCESS);
 
 if (hService == NULL) 
  return false;
 
 if(DeleteService(hService)==0)
  return false;
 
 if(CloseServiceHandle(hService)==0)
  return false;
 else
  return true;
}

 

(3)修改BOOL CTestApp::InitInstance()中代碼,還是在test.cpp中。

 //在別處顯示對話框
//  CTestDlg dlg;
//  m_pMainWnd = &dlg;
//  int nResponse = dlg.DoModal();
//  if (nResponse == IDOK)
//  {
//   // TODO: Place code here to handle when the dialog is
//   //  dismissed with OK
//  }
//  else if (nResponse == IDCANCEL)
//  {
//   // TODO: Place code here to handle when the dialog is
//   //  dismissed with Cancel
//  }

 

 //啓動服務(兩種方式:雙擊運行和SCM啓動,執行流程如剛開始提到的那樣),對話框在下面顯示
 SERVICE_TABLE_ENTRY DispatchTable[]={{"Service1",ServiceMain},{NULL,NULL}};  
  //StartServiceCtrlDispatcher(DispatchTable); 
 if (!StartServiceCtrlDispatcher(DispatchTable))
        {  
                AfxMessageBox("當不是用SCM啓動程序的時候,程序執行下面的代碼");  
  
  CTestDlg dlg;
  //m_pMainWnd = &dlg;
  int nResponse = dlg.DoModal();
  if (nResponse == IDOK)
  {
   // TODO: Place code here to handle when the dialog is
   //  dismissed with OK
  }
  else if (nResponse == IDCANCEL)
  {
   // TODO: Place code here to handle when the dialog is
   //  dismissed with Cancel
  }
 }

再在上面聲明全局函數:
extern void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);

 

(4)在主對話框上,添加4個新按鈕和1個編輯框,功能分別是:安裝服務、刪除服務、打開服務管理器、顯示啓動項。代碼如下:

void CTestDlg::OnButton1() //安裝服務

 UpdateData(TRUE);//讀取編輯框變量m_strSvrPath的值
 
 if(InstallService(m_strSvrPath))
 {
  //printf("/n/nService Installed Sucessfully/n");
  MessageBox("Service Installed Sucessfully","note",MB_OK|MB_ICONINFORMATION);
 }
 else
 {
  //printf("/n/nError Installing Service/n");
  MessageBox("Error Installing Service","note",MB_OK|MB_ICONWARNING);
 }
}

 

void CTestDlg::OnButton2() //刪除服務
{
 if(DeleteService())
 {
  //printf("/n/nService UnInstalled Sucessfully/n");
  MessageBox("Service UnInstalled Sucessfully","note",MB_OK|MB_ICONINFORMATION);
 }
 else
 {
  //printf("/n/nError UnInstalling Service/n");
  MessageBox("Error UnInstalling Service","note",MB_OK|MB_ICONWARNING);
 }
}

 

void CTestDlg::OnButton3() //打開服務管理器
{
 //打開服務管理器
 ::ShellExecute(NULL,"open","cmd.exe","/c services.msc",NULL,SW_HIDE);
}

 

void CTestDlg::OnButton4() //顯示啓動項
{
 //打開啓動項
 ::ShellExecute(NULL,"open","cmd.exe","/c msconfig",NULL,SW_HIDE); 
}


最後別忘了在開頭聲明全局函數:
extern void WINAPI ServiceCtrlHandler(DWORD Opcode);
extern BOOL InstallService(CString &strPath);
extern BOOL DeleteService();


這個服務程序還不是很完善,應該再添加一些判斷和LogEvent記錄信息,但是主要的服務程序框架應該都包含了。

最後,希望讀到此篇文章的朋友提出自己的意見。 :)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章