Files
DBService/ComposeService/ServiceBody.cpp
T
2026-07-09 11:27:59 +08:00

114 lines
3.1 KiB
C++

/*******************************************************************************
*
* 模 块 名:DBService 文件名:ServiceBody.c
* 相关文件:DBService.h
* 提交日期:2012.2.1 作 者:Alex
* 版 本:0.1
* 模块描述:
* 本模块提供了用户定义的服务启动和停止程序
* ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv);
* ServiceStop();
* 修改记录:
* 日 期 版本 修改人 修改内容
*
*
*******************************************************************************/
#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <tchar.h>
#include <assert.h>
#include <string.h>
#include "DBService.h"
#include "ServiceInit.h"
// this event is signalled when the service should end
static HANDLE hServerStopEvent = NULL;
DWORD g_unThreadId = 0;
TCHAR strErr[256];
void ServiceStart (DWORD dwArgc, LPTSTR *lpszArgv)
{
if (!ReportStatusToSCMgr(
SERVICE_START_PENDING, // service state
NO_ERROR, // exit code
3000)) // wait hint
{
_tprintf(TEXT("ServiceStart failed - %s\n"), GetLastErrorText(strErr,256));
return;
}
char *parm = 0;
if(dwArgc>=2)
parm = lpszArgv[1];
HANDLE thread = (HANDLE)CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)DBMain, NULL, 0, &g_unThreadId );
if( !thread ){
_tprintf(TEXT("CreateThread failed - %s\n"), GetLastErrorText(strErr,256));
goto cleanup;
}
hServerStopEvent = CreateEvent(
NULL, // no security attributes
TRUE, // manual reset event
FALSE, // not-signalled
SZSTOPEVENTNAME); // named
if ( hServerStopEvent == NULL){
_tprintf(TEXT("CreateEvent(ServerStopEvent) failed - %s\n"), GetLastErrorText(strErr,256));
goto cleanup;
}
if (!ReportStatusToSCMgr(
SERVICE_RUNNING, // service state
NO_ERROR, // exit code
0)) // wait hint
{
_tprintf(TEXT("ReportStatusToSCMgr failed - %s\n"), GetLastErrorText(strErr,256));
goto cleanup;
}
WaitForSingleObject(hServerStopEvent, INFINITE);
// _tprintf(TEXT("hServerStopEvent signaled!\n"));
cleanup:
if( g_unThreadId!=0 ){
PostThreadMessage( g_unThreadId, WM_CLOSE, 0, 0L );
}
if( g_hEvServiceThread ){
if(WAIT_OBJECT_0!=WaitForSingleObject(g_hEvServiceThread,15000))
{
TerminateThread(thread, 1);
_tprintf(TEXT("TerminateThread %s\n"), GetLastErrorText(strErr,256));
}
else{
// _tprintf(TEXT("g_hEvMcuThread signaled!\n"));
}
CloseHandle( g_hEvServiceThread );
}
if( thread ){
if (WAIT_OBJECT_0 != WaitForSingleObject(thread,1000)){
TerminateThread(thread, 2);
}
CloseHandle( thread );
_tprintf(TEXT("CloseHandle() %s\n"), GetLastErrorText(strErr,256));
}
if(hServerStopEvent){
CloseHandle(hServerStopEvent);
}
}
VOID ServiceStop()
{
if ( hServerStopEvent )
SetEvent(hServerStopEvent);
}
#endif