Thread: Win32 Service doesn't work arghhh

  1. #1
    Registered User
    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; 07-04-2007 at 01:01 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Are you sure you can delete pThread class while the thread is still running?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Hi,

    Thanks for your suggestion.
    I comment out the code so it looks something like this :

    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();
    	}
    Same error still occur .. Any idea ? Is believe it has something to do with the thread (correct me if im wrong)

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You constantly creating the new threads? After some time - you should be out of resources...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    If you removed the statement
    Code:
    bRunning = false;
    which would make the loop to run only once, the problem could be your TestClass. Can you show it to us?

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Quote Originally Posted by vart View Post
    You constantly creating the new threads? After some time - you should be out of resources...
    Hi, according to the article :

    Code:
            bRunning = true;
            while (bRunning) 
            {
                 // Put what you want the service to process here
            }
    What i want the service to process is actually a multhithread class that will keep listening to the port and receiving the data ... what should i do then ? u make the valid point, if constantly creating the new threads at some points it will out of resource. Is that mean i can't put multhithread inside the while statement ? If so then any solution to this ?


    Mortissus , yeh i remove "bRunning = false;" so that it can run only once so it wont out of resource, but the same error still occured. As for my TestClass, for testing purpose i delete all the function member and only left Main function, it looks like following:

    Code:
    class  TestClass : public PThread
    {
      
    public:
       void Main() 
    }
    
    TestClass:Main()
    {
        // for testing purpose i leave it empty
    }
    For your information, PThread is one of the class in OpenH323. There's no need for me to call the 'Main' function, instead all i need to do is create an object of that class, then call 'resume' instead of 'Main' as shown following:
    Code:
    	  tc=new TestClass();
    	  tc->Resume();
    Im in crying need now. Hope someone can help me out. Your effort is appreciated !!

  7. #7
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Quote Originally Posted by cppnewbie81 View Post
    What i want the service to process is actually a multhithread class that will keep listening to the port and receiving the data ... what should i do then ? u make the valid point, if constantly creating the new threads at some points it will out of resource. Is that mean i can't put multhithread inside the while statement ? If so then any solution to this ?
    Could you explain it better? Do you want a main loop that listen for request and, for each request, spawn a thread to handle the request?

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Quote Originally Posted by Mortissus View Post
    Could you explain it better? Do you want a main loop that listen for request and, for each request, spawn a thread to handle the request?
    Yep. That's exactly what i want.
    See, I have a console application which is running perfectly before, therefore I put in what is already running inside "ServiceMain" function. And it seems to me that calling the PThread class inside ServiceMain function will cause the error.

    I try to create another TestClass which is dervied from PObject instead of PThread. Everything 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 as what you mentioned earlier. Any solution ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ text file
    By statquos in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2008, 01:42 PM
  2. Windows service status checking
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-01-2008, 08:19 AM
  3. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM
  4. Getting textcolor in conio.h to work with Win32
    By MMD_Lynx in forum C Programming
    Replies: 5
    Last Post: 09-02-2004, 01:32 PM
  5. Win32 API Tutorials?
    By c++_n00b in forum C++ Programming
    Replies: 9
    Last Post: 05-09-2002, 03:51 PM