vs2010 ATL创建windows服务程序
1,new project -> 選擇 ATL Project,? 設(shè)置工程名,如:PureSsl
2,在向?qū)У摹癆pplication 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,// 修改服務(wù)為自動啟動
??????????????????????? NULL, NULL, NULL, NULL, NULL, NULL, NULL,
??????????????????????? m_szServiceName); // 通過修改資源IDS_SERVICENAME 修改服務(wù)的顯示名字
??????????????????? SERVICE_DESCRIPTION Description;
??????????????????? TCHAR szDescription[1024];
??????????????????? ZeroMemory(szDescription, 1024);
??????????????????? ZeroMemory(&Description, sizeof (SERVICE_DESCRIPTION));
??????????????????? lstrcpy(szDescription, _T("測試服務(wù)描述信息" ));
??????????????????? 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()
{
??? // 讓暫停繼續(xù)按鈕可以使用
??? 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))
??? {
??????? // 這個狀態(tài)一定要修改,否則會出現(xiàn)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, 編譯并設(shè)置服務(wù)
當ATL COM對象生成作為服務(wù),它才會注冊為本地服務(wù)器,并且,在控制面板不會出現(xiàn)在服務(wù)列表。這是因為,調(diào)試服務(wù)作為本地服務(wù)器上作為服務(wù)更為方便。若要安裝它作為服務(wù),請運行以下命令提示:
YourEXE.exe /Service
若要卸載該文件,請運行以下操作:
YourEXE.exe /UnregServer
打開cmd窗口,進入生成的PureSsl.exe所在的目錄執(zhí)行:
>? PureSsl.exe /service
> net start PureSsl
即可將服務(wù)設(shè)置到服務(wù)管理器,并啟動它。
?
要刪除服務(wù):
> sc delete PureSsl
5, 注意
未經(jīng)第4步直接運行會報以下錯誤:
The program '[24636] PureSsl.exe: Native' has exited with code 2 (0x2).
?若要直接在vs中調(diào)試運行應(yīng)當執(zhí)行:
YourEXE.exe /UnregServer
否則會報
First-chance exception at 0x75c49673 in CloudS.exe: 0x00000005: 拒絕訪問。.
The thread 'Win32 Thread' (0x314) has exited with code 1063 (0x427).
?
總結(jié)
以上是生活随笔為你收集整理的vs2010 ATL创建windows服务程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 名人名言收集
- 下一篇: windows 如何使用4GB(开启3G