My Windows 服务在 ServiceMain 函数结束时给出 SIGSEGV。
代码如下:
开始:
int main(int argc, char* argv[])
{
SrvName[16]=0;
SERVICE_TABLE_ENTRY servicetable[]=
{
{strServiceName,(LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL,NULL}
};
BOOL success;
success=StartServiceCtrlDispatcher(servicetable);
assert(success!=0);
return(0);
}
服务开始:
void ServiceMain(DWORD argc, LPTSTR *argv)
{
BOOL success;
nServiceStatusHandle=RegisterServiceCtrlHandlerEx(strServiceName,
(LPHANDLER_FUNCTION_EX)ServiceCtrlHandler, NULL);
assert(nServiceStatusHandle!=0);
success=UpdateServiceStatus(SERVICE_START_PENDING,NO_ERROR,0,1,20000);
assert(success!=0);
killServiceEvent=CreateEvent(0,TRUE,FALSE,0);
assert(killServiceEvent!=NULL);
success=UpdateServiceStatus(SERVICE_START_PENDING,NO_ERROR,0,2,10000);
assert(success!=0);
nServiceCurrentStatus=SERVICE_RUNNING;
success=UpdateServiceStatus(SERVICE_RUNNING,NO_ERROR,0,0,0);
assert(success!=0);
WaitForSingleObject(killServiceEvent,INFINITE);
CloseHandle(killServiceEvent);
UpdateServiceStatus(SERVICE_STOPPED,NO_ERROR,0,0,0);
return;
}///2x SIGSEGV here
状态更新功能:
BOOL UpdateServiceStatus(DWORD dwCurrentState, DWORD dwWin32ExitCode,
DWORD dwServiceSpecificExitCode, DWORD dwCheckPoint,
DWORD dwWaitHint)
{
BOOL success;
SERVICE_STATUS nServiceStatus;
nServiceStatus.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
nServiceStatus.dwCurrentState=dwCurrentState;
if(dwCurrentState==SERVICE_START_PENDING)
{
nServiceStatus.dwControlsAccepted=0;
}
else
{
nServiceStatus.dwControlsAccepted=SERVICE_ACCEPT_STOP
|SERVICE_ACCEPT_SHUTDOWN;
}
if(dwServiceSpecificExitCode==0)
{
nServiceStatus.dwWin32ExitCode=dwWin32ExitCode;
}
else
{
nServiceStatus.dwWin32ExitCode=ERROR_SERVICE_SPECIFIC_ERROR;
}
nServiceStatus.dwServiceSpecificExitCode=dwServiceSpecificExitCode;
nServiceStatus.dwCheckPoint=dwCheckPoint;
nServiceStatus.dwWaitHint=dwWaitHint;
success=SetServiceStatus(nServiceStatusHandle,&nServiceStatus);
return success;
}
单片机消息处理程序:
void ServiceCtrlHandler(DWORD nControlCode,DWORD dwEventType,
LPVOID lpEventData,LPVOID lpContext)
{
switch(nControlCode)
{
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
nServiceCurrentStatus=SERVICE_STOP_PENDING;
UpdateServiceStatus(SERVICE_STOP_PENDING,NO_ERROR,0,1,10000);
SetEvent(killServiceEvent);
return;
default:
break;
}
UpdateServiceStatus(nServiceCurrentStatus,NO_ERROR,0,0,0);
return;
}
所以,我在ServiceMain()的末尾有2个sigsegv:"服务.exe导致从位置 00000000 读取的位置 00000000 发生访问冲突。"寄存 器:
eax=00000000 ebx=00617d60 ecx=75bd76ba edx=00600174 esi=00000001 edi=00000000
eip=00000000 esp=010eff8c ebp=00617d70 iopl=0 nv up ei pl zr na po nc
cs=001b ss=0023 ds=0023 es=0023 fs=003b gs=0000 efl=00010246
AddrPC Params
00000000 00617D60 010EFFD4 77A437EB
7765ED5C 00617D60 70866618 00000000 kernel32.dll!BaseThreadInitThunk
77A437EB 77B47587 00617D60 00000000 ntdll.dll!RtlInitializeExceptionChain
77A437BE 77B47587 00617D60 00000000 ntdll.dll!RtlInitializeExceptionChain
我正在使用 Code::Blocks with MinGW, Win7 32bit pro。
你对此有什么想法吗?
所有服务处理程序都缺少WINAPI
( __stdcall
) 调用约定,并且您的HandlerEx
也缺少返回值。 这些错误导致调用堆栈管理不善。
修复您的声明,并摆脱类型转换。 编译器会发出有关错误声明的错误,但您强制它忽略它们并接受您的错误代码。
SERVICE_TABLE_ENTRY servicetable[]=
{
{strServiceName, &ServiceMain},
{NULL,NULL}
};
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
...
nServiceStatusHandle = RegisterServiceCtrlHandlerEx(..., &ServiceCtrlHandler, ...);
...
}
DWORD WINAPI ServiceCtrlHandler(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData, LPVOID lpContext)
{
switch(dwControl)
{
case SERVICE_CONTROL_SHUTDOWN:
case SERVICE_CONTROL_STOP:
...
return NO_ERROR;
case SERVICE_CONTROL_INTERROGATE:
...
return NO_ERROR;
default:
return ERROR_CALL_NOT_IMPLEMENTED;
}
}