停止服务时出现错误997:重叠I/O操作在进行中,附代码
blldw 2005-04-23 12:43:51 代码如下:
/*
* 服务程序
*/
#define BEEPSVC_DEBUG
#include <tchar.h>
#include <windows.h>
#define SERVICE_NAME _T("BeepSvr")
#define DEFAULT_BEEP_DELAY 2000
void RetErrorString(DWORD dwError);
void WINAPI SvcMain(DWORD argc, LPTSTR *argv);
void WINAPI SvcCtrlHandler(DWORD dwControl);
DWORD WINAPI BeepSvcThread(LPVOID lParam);
BOOL InitSvc();
BOOL UpdateServiceStatus(DWORD dwCurrentStatus, DWORD dwWin32ExitCode,
DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint, DWORD dwWaitHint);
// 全局变量
SERVICE_STATUS_HANDLE g_hSvcCtrlHandler = NULL; // 不能关闭该句柄
HANDLE g_hEvent = NULL; // 通知服务结束的事件句柄
HANDLE g_hBeep = NULL; // 工作线程句柄
DWORD dwBeepDelay = 0;
BOOL g_bSvcRunning = FALSE;
BOOL g_bSvcPause = FALSE;
// service entry point
int _tmain(int argc, TCHAR *argv[])
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("进入到服务程序入口点..."));
#endif
SERVICE_TABLE_ENTRY ste[] = {
{SERVICE_NAME, SvcMain},
{NULL, NULL}
};
// 立即调用StartServiceCtrlDispatcher
// 本服务程序只包含一项服务,所以如果有任何初始化代码,应放到SvcMain
if(!StartServiceCtrlDispatcher(ste))
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("调用StartServiceCtrlDispatcher()失败,退出..."));
#endif
return -1;
}
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("退出到服务程序入口点..."));
#endif
return 0;
}
// Service Main Function
// 当SCM启动服务时,它调用SvcMain,当SvcMain函数返回时,服务停止
// 如果需要传递服务参数,需要把服务类型设定为手工启动
void WINAPI SvcMain(DWORD argc, LPTSTR *argv)
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("进入到SvcMain()..."));
#endif
g_hSvcCtrlHandler = RegisterServiceCtrlHandler(SERVICE_NAME, SvcCtrlHandler);
if(g_hSvcCtrlHandler == (SERVICE_STATUS_HANDLE)0)
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("调用RegisterServiceCtrlHanlder()失败..."));
#endif
return;
}
// 启动服务,通知SCM启动服务的进度
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("准备启动服务,SERVICE_START_PENDING..."));
#endif
// 由于服务在正式启动成功前,需要做很多步工作,所以需要设置进度标志给dwCheckPoint
// 每完成一步操作,在下一步操作前将dwCheckPoint加一
// 通知
if(!UpdateServiceStatus(SERVICE_START_PENDING, NO_ERROR, 0, 1, 5000))
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("更新服务状态失败,SERVICE_START_PENDING_1..."));
#endif
goto SvcMainClean;
}
// 创建结束事件--第一步
g_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(g_hEvent == NULL)
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("第一步,创建结束事件失败..."));
#endif
goto SvcMainClean;
}
// 通知
if(!UpdateServiceStatus(SERVICE_START_PENDING, NO_ERROR, 0, 2, 1000))
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("更新服务状态失败,SERVICE_START_PENDING_2..."));
#endif
goto SvcMainClean;
}
// 检查启动参数--第二步
if(argc == 2)
dwBeepDelay = _ttoi(argv[1]);
else
dwBeepDelay = DEFAULT_BEEP_DELAY;
if(dwBeepDelay < 1000)
dwBeepDelay = 1000;
// 通知
if(!UpdateServiceStatus(SERVICE_START_PENDING, NO_ERROR, 0, 3, 5000))
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("更新服务状态失败,SERVICE_START_PENDING_3..."));
#endif
goto SvcMainClean;
}
// 创建线程--第三步
g_hBeep = CreateThread(NULL, 0, BeepSvcThread, NULL, 0, NULL);
if(g_hBeep == NULL)
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("第三步,创建工作线程失败..."));
#endif
goto SvcMainClean;
}
g_bSvcRunning = TRUE;
// 服务启动工作完毕,通知SCM
if(!UpdateServiceStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0))
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("更新服务状态失败,SERVICE_RUNNING..."));
#endif
goto SvcMainClean;
}
// 等待结束事件的到来
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("启动并设置服务状态成功,等待结束事件到来..."));
#endif
WaitForSingleObject(g_hEvent, INFINITE);
WaitForSingleObject(g_hBeep, INFINITE);
// 结束服务
//
// SvcMainClean
//
SvcMainClean:
if(g_hBeep)
CloseHandle(g_hBeep);
if(g_hEvent)
CloseHandle(g_hEvent);
if(g_hSvcCtrlHandler)
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("结束事件到来,退出SvcMain之前发送SERVICE_STOPPED通知..."));
#endif
if(!UpdateServiceStatus(SERVICE_STOPPED, GetLastError(), 0, 0, 0))
{
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("结束事件到来,退出SvcMain之前发送SERVICE_STOPPED通知,失败!..."));
#endif
}
}
#ifdef BEEPSVC_DEBUG
OutputDebugString(_T("结束事件到来,退出SvcMain..."));
#endif
}