gpt4 book ai didi

c++ - 窗口服务 SIGSEGV

转载 作者:行者123 更新时间:2023-11-28 07:13:03 25 4
gpt4 key购买 nike

我的 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;
}

SCM 消息处理程序:

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:“Service.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

我在 MinGW、Win7 32 位专业版上使用 Code::Blocks。

你有什么想法吗?

最佳答案

您的所有服务处理程序都缺少 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;
}
}

关于c++ - 窗口服务 SIGSEGV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20680729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com