windows服务程序的问题,StartServiceCtrlDispatcher执行不成功

xiaoli2006 2010-08-05 02:52:56

// ServiceTest.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "stdio.h"
#include "tchar.h"
#include "log.h"
#include "iostream"

//定义全局函数变量
void Init();
BOOL IsInstalled();
BOOL Install();
BOOL Uninstall();
void LogEvent(LPCTSTR pszFormat, ...);
void WINAPI ServiceMain();
void WINAPI ServiceStrl(DWORD dwOpcode);
CLog g_log(CLog::ToFile,LL_ALL, "./ServiceTest.log", true);


TCHAR szServiceName[] = _T("ServiceTest");
BOOL bInstall;
SERVICE_STATUS_HANDLE hServiceStatus;
SERVICE_STATUS status;
DWORD dwThreadID;

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Init();

dwThreadID = ::GetCurrentThreadId();

SERVICE_TABLE_ENTRY st[] =
{
{ szServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain },
{ NULL, NULL }
};

Install();

if (stricmp(lpCmdLine, "/install") == 0)
{
Install();
}
else if (stricmp(lpCmdLine, "/uninstall") == 0)
{
Uninstall();
}
else
{
int Flag = ::StartServiceCtrlDispatcher(st);//此处每次执行Flag都是0,绑定不成功???求原因
if (!Flag)
{
g_log.Print(LL_CONNERR,VNCLOG("Register Service Main Function Error! %d\r\n"), GetLastError());
}
}

return 0;
}
//*********************************************************
//Functiopn: Init
//Description: 初始化
//Calls: main
//Called By:
//Table Accessed:
//Table Updated:
//Input:
//Output:
//Return:
//Others:
//History:
// <author>niying <time>2006-8-10 <version> <desc>
//*********************************************************
void Init()
{
hServiceStatus = NULL;
status.dwServiceType = SERVICE_WIN32_SHARE_PROCESS;
status.dwCurrentState = SERVICE_STOPPED;
status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
status.dwWin32ExitCode = 0;
status.dwServiceSpecificExitCode = 0;
status.dwCheckPoint = 0;
status.dwWaitHint = 0;
}

//*********************************************************
//Functiopn: ServiceMain
//Description: 服务主函数,这在里进行控制对服务控制的注册
//Calls:
//Called By:
//Table Accessed:
//Table Updated:
//Input:
//Output:
//Return:
//Others:
//History:
// <author>niying <time>2006-8-10 <version> <desc>
//*********************************************************
void WINAPI ServiceMain()
{
// Register the control request handler
status.dwCurrentState = SERVICE_START_PENDING;
status.dwControlsAccepted = SERVICE_ACCEPT_STOP;

//注册服务控制
hServiceStatus = RegisterServiceCtrlHandler(szServiceName, ServiceStrl);
if (hServiceStatus == NULL)
{
LogEvent(_T("Handler not installed"));
return;
}
SetServiceStatus(hServiceStatus, &status);

status.dwWin32ExitCode = S_OK;
status.dwCheckPoint = 0;
status.dwWaitHint = 0;
status.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(hServiceStatus, &status);

//模拟服务的运行,10后自动退出。应用时将主要任务放于此即可
int i = 0;
while (i < 20)
{
Sleep(1000);

g_log.Print(LL_CONNERR,VNCLOG("================"));
i++;
}
//

status.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(hServiceStatus, &status);
LogEvent(_T("Service stopped"));
}

//*********************************************************
//Functiopn: ServiceStrl
//Description: 服务控制主函数,这里实现对服务的控制,
// 当在服务管理器上停止或其它操作时,将会运行此处代码
//Calls:
//Called By:
//Table Accessed:
//Table Updated:
//Input: dwOpcode:控制服务的状态
//Output:
//Return:
//Others:
//History:
// <author>niying <time>2006-8-10 <version> <desc>
//*********************************************************
void WINAPI ServiceStrl(DWORD dwOpcode)
{
switch (dwOpcode)
{
case SERVICE_CONTROL_STOP:
status.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(hServiceStatus, &status);
PostThreadMessage(dwThreadID, WM_CLOSE, 0, 0);
break;
case SERVICE_CONTROL_PAUSE:
break;
case SERVICE_CONTROL_CONTINUE:
break;
case SERVICE_CONTROL_INTERROGATE:
break;
case SERVICE_CONTROL_SHUTDOWN:
break;
default:
LogEvent(_T("Bad service request"));
}
}
//*********************************************************
//Functiopn: IsInstalled
//Description: 判断服务是否已经被安装
//Calls:
//Called By:
//Table Accessed:
//Table Updated:
//Input:
//Output:
//Return:
//Others:
//History:
// <author>niying <time>2006-8-10 <version> <desc>
//*********************************************************
BOOL IsInstalled()
{
BOOL bResult = FALSE;

//打开服务控制管理器
SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

if (hSCM != NULL)
{
//打开服务
SC_HANDLE hService = ::OpenService(hSCM, szServiceName, SERVICE_QUERY_CONFIG);
if (hService != NULL)
{
bResult = TRUE;
::CloseServiceHandle(hService);
}
::CloseServiceHandle(hSCM);
}
return bResult;
}

//*********************************************************
//Functiopn: Install
//Description: 安装服务函数
//Calls:
//Called By:
//Table Accessed:
//Table Updated:
//Input:
//Output:
//Return:
//Others:
//History:
//<author>niying <time>2006-8-10 <version> <desc>
//*********************************************************
BOOL Install()
{
if (IsInstalled())
return TRUE;

//打开服务控制管理器
SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hSCM == NULL)
{
MessageBox(NULL, _T("Couldn't open service manager"), szServiceName, MB_OK);
return FALSE;
}

// Get the executable file path
TCHAR szFilePath[MAX_PATH];
::GetModuleFileName(NULL, szFilePath, MAX_PATH);

//创建服务
SC_HANDLE hService = ::CreateService(
hSCM, szServiceName, szServiceName,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
szFilePath, NULL, NULL, _T(""), NULL, NULL);

if (hService == NULL)
{
::CloseServiceHandle(hSCM);
MessageBox(NULL, _T("Couldn't create service"), szServiceName, MB_OK);
return FALSE;
}

/*
if(StartService(hService,0,NULL)==0)
{
if(GetLastError()==ERROR_SERVICE_ALREADY_RUNNING)
{
printf( "already Running !\n ");
CloseServiceHandle(hSCM);
CloseServiceHandle(hService);
return TRUE;
}
}

*/

::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);
return TRUE;
}

//*********************************************************
//Functiopn: Uninstall
//Description: 删除服务函数
//Calls:
//Called By:
//Table Accessed:
//Table Updated:
//Input:
//Output:
//Return:
//Others:
//History:
// <author>niying <time>2006-8-10 <version> <desc>
//*********************************************************
BOOL Uninstall()
{
if (!IsInstalled())
return TRUE;

SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

if (hSCM == NULL)
{
MessageBox(NULL, _T("Couldn't open service manager"), szServiceName, MB_OK);
return FALSE;
}

SC_HANDLE hService = ::OpenService(hSCM, szServiceName, SERVICE_STOP | DELETE);

if (hService == NULL)
{
::CloseServiceHandle(hSCM);
MessageBox(NULL, _T("Couldn't open service"), szServiceName, MB_OK);
return FALSE;
}
SERVICE_STATUS status;
::ControlService(hService, SERVICE_CONTROL_STOP, &status);

//删除服务
BOOL bDelete = ::DeleteService(hService);
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);

if (bDelete)
return TRUE;

LogEvent(_T("Service could not be deleted"));
return FALSE;
}

//*********************************************************
//Functiopn: LogEvent
//Description: 记录服务事件
//Calls:
//Called By:
//Table Accessed:
//Table Updated:
//Input:
//Output:
//Return:
//Others:
//History:
// <author>niying <time>2006-8-10 <version> <desc>
//*********************************************************
void LogEvent(LPCTSTR pFormat, ...)
{
TCHAR chMsg[256];
HANDLE hEventSource;
LPTSTR lpszStrings[1];
va_list pArg;

va_start(pArg, pFormat);
_vstprintf(chMsg, pFormat, pArg);
va_end(pArg);

lpszStrings[0] = chMsg;

hEventSource = RegisterEventSource(NULL, szServiceName);
if (hEventSource != NULL)
{
ReportEvent(hEventSource, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (LPCTSTR*) &lpszStrings[0], NULL);
DeregisterEventSource(hEventSource);
}
}


之前看了很多相关的帖子,大概明白了基本的执行流程。
第一次运行,编译完成后生成了.exe,直接在命令行窗口,ServiceTest.exe /install",安装此服务,"也就是写入注册表信息,此时服务本身并未启动。
第二次直接运行此程序,现在进入“注册服务控制”阶段,即将服务程序绑定到服务控制程序SCM。按道理,它应该init后,if判断跳到StartServiceCtrlspatcher(st)处执行。
问题:StartServiceCtrlspatcher()但是却执行失败,1063错误。不知道这是为什么?在ServiceMain设置断点,根本就没有进入。
http://topic.csdn.net/u/20100624/16/4391A404-3B66-4377-B605-7768F85E758B.html 参考了此处的文章,也没有解决。
有很多类似的帖子,不用简单复制了,基本上看过了。
...全文
307 点赞 收藏 9
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaoli2006 2010-08-05
现在已经成为一种服务了,只能在控制面板里面启动,没法调试啊,可以提示一下怎么调试吗?
[Quote=引用 6 楼 oyljerry 的回复:]
引用 5 楼 xiaoli2006 的回复:
那就调试一下看看g_log是否调用Print了20次,是否有问题...是否覆盖了
[/Quote]
回复
oyljerry 2010-08-05
[Quote=引用 5 楼 xiaoli2006 的回复:]

TO:wltg2001
那是我之前我调试时加的代码,没改过来。刚刚重新试验了一下,程序可以实现20秒钟后停止了。但是为啥每次我都打印语句都没有作用呢?g_log是我定义的一个Clog对象,它的Print()可以保证没有问题。但是为啥到这里就没有用了?
[/Quote]
那就调试一下看看g_log是否调用Print了20次,是否有问题...是否覆盖了
回复
xiaoli2006 2010-08-05
TO:wltg2001
那是我之前我调试时加的代码,没改过来。刚刚重新试验了一下,程序可以实现20秒钟后停止了。但是为啥每次我都打印语句都没有作用呢?g_log是我定义的一个Clog对象,它的Print()可以保证没有问题。但是为啥到这里就没有用了?
回复
oyljerry 2010-08-05
http://topic.csdn.net/u/20070404/14/876fe49b-6c37-49b5-8501-e389fa1213ff.html
回复
wltg2001 2010-08-05
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Init();

dwThreadID = ::GetCurrentThreadId();

SERVICE_TABLE_ENTRY st[] =
{
{ szServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain },
{ NULL, NULL }
};

Install();//这里怎么又调用Install啊?
if (stricmp(lpCmdLine, "/install") == 0)
{
Install();
}
else if (stricmp(lpCmdLine, "/uninstall") == 0)
{
Uninstall();
}
else
{
int Flag = ::StartServiceCtrlDispatcher(st);//此处每次执行Flag都是0,绑定不成功???求原因
if (!Flag)
{
g_log.Print(LL_CONNERR,VNCLOG("Register Service Main Function Error! %d\r\n"), GetLastError());
}
}

return 0;
}
回复
xiaoli2006 2010-08-05
TO:wltg2001
我尝试过在“控制面板"的服务中直接手动开启此服务,但是我在ServiceMain()方法中,执行20秒后
自动停止,但是并没有停止,一直都在运行。而且我在.exe目录下建了一个"ServiceTest.log"文本,执行g_log.Print语句就会打印一行,但是没有任何反应。不知这是为何?

int i = 0;
while (i < 20)
{
Sleep(1000);

g_log.Print(LL_CONNERR,VNCLOG("================"));
i++;
}

回复
wltg2001 2010-08-05
第二次直接运行此程序,现在进入“注册服务控制”阶段。按道理,它应该init后,if判断跳到StartServiceCtrlspatcher(st)处执行。但是却执行失败,1063错误。不知道这是为什么?在ServiceMain设置断点,根本就没有进入。
=======
服务程序根本就不是直接执行的,你应该是在 控制面板|管理工具中的“服务”中找到你写的服务,选启动来开启它。
回复
发动态
发帖子
VC/MFC
创建于2007-09-28

1.5w+

社区成员

VC/MFC相关问题讨论
申请成为版主
社区公告
暂无公告