VC2010 编写windows service 服务程序

VC2010 编写windows service 服务程序 - Tsing松 - Tsing松的博客

 
选择alt工程模板,输入名字TestService

VC2010 编写windows service 服务程序 - Tsing松 - Tsing松的博客

 
点ok后,出现下面界面

VC2010 编写windows service 服务程序 - Tsing松 - Tsing松的博客

 
直接点Next,出现下一界面选Service。

VC2010 编写windows service 服务程序 - Tsing松 - Tsing松的博客

 

到此,创建windows service工程完成。在TestService.cpp中增加下面的代码(红色)

// TestService.cpp : Implementation of WinMain


#include "stdafx.h"
#include "resource.h"
#include "TestService_i.h"


#include <stdio.h>

using namespace ATL;

class CTestServiceModule : public ATL::CAtlServiceModuleT< CTestServiceModule, IDS_SERVICENAME >
{
public :
DECLARE_LIBID(LIBID_TestServiceLib)
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_TESTSERVICE, "{25AF9FF6-DEA2-4E6C-8EBC-B3BB81B74226}")
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 CTestServiceModule::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 = 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 = AtlHresultFromLastError();
}
::CloseServiceHandle(hSCM);
}
}
}
return hr;
}

HRESULT CTestServiceModule::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 CTestServiceModule::PostMessageLoop() throw()
{
HRESULT hr = __super ::PostMessageLoop();

if (FAILED(hr))
return hr;

// 这里添加自己的清除代码

return hr;
}

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

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

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

CTestServiceModule _AtlModule;

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

编译,打开cmd窗口,进入生成的TestService.exe目录执行

VC2010 编写windows service 服务程序 - Tsing松 - Tsing松的博客

 
即可启动服务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值