Win32 Service doesn't work arghhh
I create a win32 service according to the instruction as in following URL:
http://www.codeproject.com/system/wi...nt_service.asp
I am able to install and start the service perfectly without any error by following those instruction.
In the example given, the service will just running and do nothing because there's nothing inside while { } statement in ServiceMain(DWORD argc, LPTSTR *argv) function. Therefore, i add some code inside the while statement. After added in the code, I'm able to install the service, but when i try to start the service it said :
"System error 1067 has occurred. The process terminated unexpectedly". What's actually happened anyone ? The following is my code in ServiceMain function:
Code:
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
DWORD status;
DWORD specificError;
m_ServiceStatus.dwServiceType = SERVICE_WIN32;
m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwServiceSpecificExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
m_ServiceStatusHandle = RegisterServiceCtrlHandler("ServiceC",
ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
{
return;
}
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
{
}
bRunning = true;
while (bRunning)
{
// The problem occur here, if i comment out all the codes within the while loops it works perfectly
TestClassr *tc;
tc=new TestClass();
tc->Resume();
bRunning = false;
delete tc;
tc = 0;
}
return;
}
For you all information, TestClass is a class that is generated from PThread.
PThread = one of the Class from Openh323 (www.openh323.org)
Hope somebobody can help me out. Thanks in advance
Re: Win32 Service doesn't work arghhh
OK i think I know what cause the problem, but too bad i don't know the solution .. I have a console application which is run perfectly before, therefore I put in what is already running inside "ServiceMain" function. And i believe calling the Thread class inside the ServiceMain function is what cause the problem (as shown following)
Code:
TestClass *tc;
tc=new TestClass();
tc->Resume()
I try to create another TestClass which is dervied from PObject instead of PThread, and it works perfectly. But this is not what i want, i want to call a Thread Class with a main loop that listen for request and, for each request, spawn a thread to handle the request. Any solution ? IM in crying need now, hope someone can help. Thanks
1 Attachment(s)
Re: Win32 Service doesn't work arghhh
Sorry I lost the link, however attached is a good sample.
Re: Win32 Service doesn't work arghhh
Ok, there're several points to be cleared.
First, what that thread exactly does? It might terminate the process as well, especially if it's done in the same manner the ServiceMain is - no decent return value checking.
Second, it would be good take a look at ServiceCtrlHandler which is really important for a service life cycle.
Third, the first developer's friend is an off-line debugging (as logging to a file, etc.), and post-mortem analysis is the second one. :)
Re: Win32 Service doesn't work arghhh
MrBeans - Thanks for the source code, but where should i put the code that i want service to process ? I try to look into each of the files but can't found it ...
Igor - The TestClass has a main loop that listen for request and, for each request, spawn a thread to handle the request. At first i thought it was the function inside the thread that terminated the service, therefore i comment out all the code inside each of the function. But the same error still occured.
And as i mention on the above post, here's where it cause the error :
Code:
while (bRunning)
{
// The problem occur here, if i comment out all the codes within the while loops it works perfectly
TestClassr *tc;
tc=new TestClass();
tc->Resume();
bRunning = false;
delete tc;
tc = 0;
}
I log the error in a log file, and here's where the service "DEAD" :
Code:
if (!StartServiceCtrlDispatcher(DispatchTable))
{
switch(GetLastError())
{
case ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
// Log ERROR_FAILED_SERVICE_CONTROLLER_CONNECT error
return -1;
break;
case ERROR_INVALID_DATA:
// Log ERROR_INVALID_DATA: error
return -1;
break;
case ERROR_SERVICE_ALREADY_RUNNING:
// Log ERROR_SERVICE_ALREADY_RUNNING: error
return -1;
default:
// Log default error
return -1;
}
printf("\n\nError Starting Service\n");
}
I get ERROR_FAILED_SERVICE_CONTROLLER_CONNECT error when i call StartServiceCtrlDispatcher function
Re: Win32 Service doesn't work arghhh
Let's go to the source of wisdom:
Quote:
Originally Posted by MSDN
Return code
ERROR_FAILED_SERVICE_CONTROLLER_CONNECT
Description
Typically, this error indicates that the program is being run as a console application rather than as a service.
Re: Win32 Service doesn't work arghhh
Sounds like (as Igor quoted) there is some console I/O going in. Search all of your code for any such things (cout, cin, printf, etc).
Re: Win32 Service doesn't work arghhh
I always love this part when original questioner falls into a dead silence... not even a sound of reply... :rolleyes:
Re: Win32 Service doesn't work arghhh
Quote:
Originally Posted by Igor Vartanov
I always love this part when original questioner falls into a dead silence... not even a sound of reply... :rolleyes:
See Thread vs. Process. Probably my bad for answering in the other thread.
Re: Win32 Service doesn't work arghhh
Quote:
Originally Posted by Igor Vartanov
I always love this part when original questioner falls into a dead silence... not even a sound of reply... :rolleyes:
lol ... i'll be away for 3 weeks for summer vacation .. now in cyber cafe !!
will get back to you guys soon after i get back !!