1. 程式人生 > >vs2010 ATL建立windows服務程式

vs2010 ATL建立windows服務程式

1,new project -> 選擇 ATL Project,  設定工程名,如:PureSsl

2,在嚮導的“Application Setting”頁,“Application Type”項選擇: Service(EXE)

3,  更改主檔案PureSsl.cpp,如下:

// PureSsl.cpp : Implementation of WinMain


#include "stdafx.h"
#include "resource.h"
#include "PureSsl_i.h"

#include <atlcomcli.h>
#include <stdio.h>

class CPureSslModule : public ATL::CAtlServiceModuleT< CPureSslModule, IDS_SERVICENAME >
{
public :
 DECLARE_LIBID(LIBID_PureSslLib)
 DECLARE_REGISTRY_APPID_RESOURCEID(IDR_PURESSL, "{A0D1CDBB-EE9F-4110-9710-498FEB5194F5}")
  HRESULT InitializeSecurity() throw()
 {
  // TODO : Call CoInitializeSecurity and provide the appropriate security settings for your service
  // Suggested - PKT Level Authentication,
  // Impersonation Level of RPC_C_IMP_LEVEL_IDENTIFY
  // and an appropiate Non NULL Security Descriptor.

  return S_OK;
 }

    HRESULT RegisterAppId(bool bService = false) throw ();
    HRESULT PreMessageLoop(int nShowCmd) throw();
    HRESULT PostMessageLoop() throw();
    void OnStop() throw();
    void OnPause() throw();
    void OnContinue() throw();
};

HRESULT CPureSslModule::RegisterAppId(bool bService ) throw ()
{
    HRESULT hr = S_OK;
    BOOL res = __super ::RegisterAppId(bService);
    if (bService)
    {
        if (IsInstalled())
        {
            SC_HANDLE hSCM = ::OpenSCManagerW(NULL, NULL, SERVICE_CHANGE_CONFIG);
            SC_HANDLE hService = NULL;
            if (hSCM == NULL)
            {
                hr = ATL::

AtlHresultFromLastError();
            }
            else
            {
                hService = ::OpenService(hSCM, m_szServiceName, SERVICE_CHANGE_CONFIG);
                if (hService != NULL)
                {
                    ::ChangeServiceConfig(hService, SERVICE_NO_CHANGE,
                        SERVICE_AUTO_START,// 修改服務為自動啟動
                        NULL, NULL, NULL, NULL, NULL, NULL, NULL,
                        m_szServiceName); // 通過修改資源IDS_SERVICENAME 修改服務的顯示名字

                    SERVICE_DESCRIPTION Description;
                    TCHAR szDescription[1024];
                    ZeroMemory(szDescription, 1024);
                    ZeroMemory(&Description, sizeof (SERVICE_DESCRIPTION));
                    lstrcpy(szDescription, _T("測試服務描述資訊" ));
                    Description.lpDescription = szDescription;
                    ::ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &Description);
                    ::CloseServiceHandle(hService);
                }
                else
                {
                    hr = ATL::AtlHresultFromLastError();
                }
                ::CloseServiceHandle(hSCM);
            }
        }
    }
    return hr;
}

HRESULT CPureSslModule::PreMessageLoop(int nShowCmd) throw()
{
    // 讓暫停繼續按鈕可以使用
    m_status.dwControlsAccepted = m_status.dwControlsAccepted | SERVICE_ACCEPT_PAUSE_CONTINUE;

    HRESULT hr = __super::PreMessageLoop(nShowCmd);
    // 微軟Bug
    if (hr == S_FALSE)
        hr = S_OK;

    // 這裡新增自己的初始化程式碼...

    if (SUCCEEDED(hr))
    {
        // 這個狀態一定要修改,否則會出現1053錯誤,
        // 這個錯誤我花了很多時間才搞定
        SetServiceStatus(SERVICE_RUNNING);
    }

    return hr;
}

HRESULT CPureSslModule::PostMessageLoop() throw()
{
    HRESULT hr = __super ::PostMessageLoop();

    if (FAILED(hr))
        return hr;

    // 這裡新增自己的清除程式碼

    return hr;
}

void CPureSslModule::OnStop() throw()
{
    __super::OnStop();
    SetServiceStatus(SERVICE_STOPPED);
}

void CPureSslModule::OnPause() throw()
{
    __super::OnPause();
    SetServiceStatus(SERVICE_PAUSED);
}

void CPureSslModule::OnContinue() throw()
{
    __super::OnContinue();
    SetServiceStatus(SERVICE_RUNNING);
}


CPureSslModule _AtlModule;

//
extern "C" int WINAPI _tWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/,
        LPTSTR /*lpCmdLine*/, int nShowCmd)
{
 return _AtlModule.WinMain(nShowCmd);
}

4, 編譯並設定服務

當ATL COM物件生成作為服務,它才會註冊為本地伺服器,並且,在控制面板不會出現在服務列表。這是因為,除錯服務作為本地伺服器上作為服務更為方便。若要安裝它作為服務,請執行以下命令提示:

YourEXE.exe /Service

若要解除安裝該檔案,請執行以下操作:

YourEXE.exe /UnregServer

開啟cmd視窗,進入生成的PureSsl.exe所在的目錄執行:

>  PureSsl.exe /service

> net start PureSsl

即可將服務設定到服務管理器,並啟動它。

要刪除服務:

> sc delete PureSsl

5, 注意

未經第4步直接執行會報以下錯誤:

The program '[24636] PureSsl.exe: Native' has exited with code 2 (0x2).

 若要直接在vs中除錯執行應當執行:

YourEXE.exe /UnregServer

否則會報

First-chance exception at 0x75c49673 in CloudS.exe: 0x00000005: 拒絕訪問。.
The thread 'Win32 Thread' (0x314) has exited with code 1063 (0x427).