CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2007
    Posts
    10

    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
    Last edited by cppnewbie81; July 4th, 2007 at 01:18 AM.

  2. #2
    Join Date
    Jul 2007
    Posts
    10

    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

  3. #3
    Join Date
    Jul 2005
    Posts
    767

    Re: Win32 Service doesn't work arghhh

    Sorry I lost the link, however attached is a good sample.
    Attached Files Attached Files
    Last edited by MrBeans; June 2nd, 2010 at 04:25 AM.
    One's mistake cannot be your excuse!

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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.
    Best regards,
    Igor

  5. #5
    Join Date
    Jul 2007
    Posts
    10

    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

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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.
    Best regards,
    Igor

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    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).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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...
    Best regards,
    Igor

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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...
    See Thread vs. Process. Probably my bad for answering in the other thread.

  10. #10
    Join Date
    Jul 2007
    Posts
    10

    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...
    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 !!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured