Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Services / Debugging Services 1

Status
Not open for further replies.

Skute

Programmer
Joined
Jul 21, 2003
Messages
272
Location
GB
Hi,

Ive got a problem with a service im writing.

I make the following call:

SERVICE_TABLE_ENTRY DispatchTable[] =
{
{ Name, ServiceMainFunction},
{ NULL, NULL }
};

if (!StartServiceCtrlDispatcher(DispatchTable))
{
}

...

But just as "ServiceMainFunction" is returning i get an error and XP tries to send an error report. The debug message is trying to write to NULL. But nothing happens to cause this:

VOID ServiceMainFunction(DWORD argc, LPTSTR *argv)
{
g_hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)MainServerThread, NULL, 0, &g_dwThreadID);

WaitForSingleObject(g_hThread, INFINITE);

MessageBox(NULL, "Service Finished!", "Test", MB_ICONINFORMATION | MB_OK);
//TerminateThread(g_hThread, 0);
}


The message box gets displayed at the end of the function, and as you can see, there is no further code after that!

Whats going on!?

----


Also, if anyone has any debug tips for services please let me know, as you cant easily debug a service. ie, how do you attach a debugger to a service etc?

Thanks
 
if you want to debug s service which you have created,
1. Open the project in VisualStudio
2. Put breakpoints where would you like
3. Lanch the service as you usualy launch it. Do not launch it from VisualStudio.
4.
4.1. If service fails, you will see a system error dialog box, with ok and cancel, if you want to debug press cancel and attach the process to the instance of visualstudio you have open in 1.
4.2 If service does not fail, press magic <ctrl><alt><del>, look in TaskManager in the list of processes your service, right click it and choose debug. You should attach the process th the instance of VisualStudio you have open in 1.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Thanks Ion, seems to be working.

It seems to be when im terminating the service after it has started. Is there anything special i should be doing once the main thread in a service is finishing?
 
very often are used message loops to maintain the main thread:

MSG msg;
while(GetMessage(&msg, 0, 0, 0))DispatchMessage(&msg);

this approach id used in most COM objects, and in many services. The while loop will exit when somewhere in the program is called PostQuitMessage(someExitCode)

after the while loop ends, the msg.wParam will contain that someExitCode. You could return it to operation system.

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top