如何应用VS2010中来创建Windows服务程序
答案:1 悬赏:80 手机版
解决时间 2021-11-18 04:38
- 提问者网友:绫月
- 2021-11-17 14:38
如何应用VS2010中来创建Windows服务程序
最佳答案
- 五星知识达人网友:不甚了了
- 2021-11-17 15:51
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
#include
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 ,
LPTSTR , 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).
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
#include
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 ,
LPTSTR , 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).
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯