// JP opened flex table

Click to See Complete Forum and Search --> : Restart a windows service


v_sowmya
January 23rd, 2008, 01:33 AM
Hi,

I hope this is the correct forum for my query..
My problem is to restart a windows service whenever a particular exception arises.
My main() function is as below:

void main(int argc,char ** argv)
{
SERVICE_TABLE_ENTRY ServiceTable[] =
{
{
argv[0],
(LPSERVICE_MAIN_FUNCTION) ServiceMain
},
{NULL, NULL}
};

BOOL success;
//Register with SCM
success = StartServiceCtrlDispatcher(ServiceTable) ;
if (!success)
{
printf("Error in StartServiceCtrlDispatcher call ");
ErrorHandler("In StartServiceCtrlDispatcher", GetLastError()) ;
}
return;
}


Pls suggest how to write the code to restart the service. I tried to invoke the StartServiceCtrlDispatcher again to restart the service. But that does not work and the return value is 0 (during the second invocation).
Pls help. I am completely new to C++.

golanshahar
January 23rd, 2008, 04:43 PM
This article (http://codeguru.earthweb.com/cpp/w-p/system/ntservices/article.php/c5725/) might help you.

Cheers

Igor Vartanov
January 24th, 2008, 03:24 AM
I hope this is the correct forum for my query..
My problem is to restart a windows service whenever a particular exception arises.

Pls suggest how to write the code to restart the service. I tried to invoke the StartServiceCtrlDispatcher again to restart the service. But that does not work and the return value is 0 (during the second invocation).
Pls help. I am completely new to C++.The problem has nothing to do with C++, as far as I can see.

First of all, in case exception gets thrown from your code, you have to catch it and decide if you can recover the situation. Do you catch it?

Second, what do you mean "particular exception"? Does it mean you exactly know what exception is thrown? Then maybe it would be reasonable to gebug and fix the root problem?

Third, Windows already takes care about service recovery.

And the last - here's the code (http://www.codeguru.com/forum/showthread.php?p=1672017#post1672017) that allows the service restart itself.

//JP added flex table