Windows service terminates unexpectedly in windows 7
I have written a windows service that catches a UDP message, creates a process as user, sends the message to the process via named pipes, and repeats. Service works great in XP but terminates unexpectedly in windows 7 after one iteration. Event Viewer shows that after I pass my message to the service there is an application error followed by the unexpected termination of my service. Application error says faulting module is ntdll.dll. Debugging my service shows where the code stops.
Here is my work loop.
do
{
DebugLog("Waiting For UDP message");
if(recvfrom(socketId, buffer, buffSize, 0, &clientAddrCast, &size) >= 0)
{
DebugLog("Create Named Pipe");
hPipe = CreateNamedPipe(lpszPipename, // pipe name
PIPE_ACCESS_DUPLEX, // read/write access
PIPE_TYPE_MESSAGE | // message pipe
PIPE_READMODE_MESSAGE, // message read mode
1, // maximum Instaces
buffSize, // output buffer size
buffSize, // input buffer size
5000, // client time-out
&pSecAttrib); // security attributes
if (hPipe == INVALID_HANDLE_VALUE)
ErrorLog("CreateNamedPipe");
DebugLog("Create User Process");
if(!CreateProcessAsUser(hTokenDupe, "C:\\Services\\UDPClient_UserApp.exe", NULL, NULL, NULL, false, dwCreati开发者_如何学GoonFlags, NULL, NULL, &si, &pi))
{
// if client app fails use SendMessage as failsafe
//WTSSendMessage(WTS_CURRENT_SERVER_HANDLE, sessionID, pTitle, sizeof(pTitle),
//buffer, sizeof(buffer), MB_OK, 30, &resp, false);
ErrorLog("CreateProcess");
}
else
{
DebugLog("Writing to User Process");
// Open pipe to client
if(cSuccess = ConnectNamedPipe(hPipe, NULL) == 0)
ErrorLog("ConnectNamedPipe");
else
{
Sleep(2000);
cbToWrite = (lstrlen(buffer)+1)*sizeof(TCHAR);
if(!WriteFile(
hPipe, // pipe handle
buffer, // message
cbToWrite, // message length
&cbWritten, // bytes written
NULL)) // not overlapped
ErrorLog("WriteFile");
}
}
}
DebugLog("Cleanup");
DestroyEnvironmentBlock(&pEnv);
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
ErrorLog("test");
}while(gServiceStatus.dwCurrentState == SERVICE_RUNNING);
My debug logs in XP show that after DebugLog("Cleanup")
it loops back to DebugLog("Waiting For UDP message")
. In windows 7 it stops after DebugLog("Cleanup")
. I am looking into problems my code may create. Any other suggestions would be greatly appreciated.
Thanks, Joseph G.
My guess would be tighter security in Win7 is getting you.
There's missing code here, what's in pSecAttrib
for example? Beef up your error handling so (for example) you don't execute the rest of the loop body if pipe creation fails, and you know exactly which API failed and why.
CreateProcessAsUser
- what user are you running the service as? Does that user have the right permissions - they may be different on Win7 versus WinXP.
Log GetLastError()
value after all Win32 failures. Make your own life easier, this could be in Production in a remote site one day and how will you troubleshoot it then?
What's that Sleep(2000);
for? Suspicious. If the pipe is open why not just send the data and avoid this delay. Is the Named Pipe server code this slow to post a read request on the pipe?
精彩评论