Click to See Complete Forum and Search --> : Service Control Dispatcher
Daeriel
March 4th, 2003, 08:47 PM
Dear Gurus!
It looks like to be hard to get an answer from you, but I'll try a chance again.
Question: Is there any reasonable way to get whether the service was started from the service control dispatcher to be attached to (and to run StartServiceControlDispatcher) or not. Of course I can get a handle to parent process and check whether it was 'services.exe' or not, but is there any other way?
I just don't want to call StartServiceControlDispatcher if the application is not intended to run as service.
Thanks!
MemoryLeak
March 5th, 2003, 09:46 AM
Not sure if this helps, but if you call StartServiceCtrlDispatcher and it fails and GetLastError() returns ERROR_FAILED_SERVICE_CONTROLLER_CONNECT you're running as an exe. I can't recall now, but on W95/98/ME StartServiceCtrlDispatcher may result in a not supported error code.
I normally check for a command-line argument (usually -exe) before I do this since on OS's that support services it can take several seconds to fail if you're not running as a service. I then switch on GetLastError along these lines:
if(lstrcmpi(pCmdLine,"-exe"))
{
if(!StartServiceCtrlDispatcher(serviceTable))
{
DWORD dwErr;
dwErr = GetLastError();
switch(dwErr)
{
case ERROR_INVALID_DATA:
//something is wrong with service
//data structures, need to terminate
break;
//Any other error
default:
//running as exe
}
}
}
Daeriel
March 5th, 2003, 06:45 PM
As you said it takes a few seconds to get an error code from system when you call StartServiceControlDisatcher for the first time at the current windows session. It's not good. I just need to detect this situation before call to StartServiceControlDispatcher.
Thanks anyway.
Mick
March 5th, 2003, 07:18 PM
Thanks MemoryLeak for your comment, because quite frankly I was confused by the question, because as an answer I would have said check the command line arguments, but now I'm still confused, what is it that you want? Your really not making sense :) sorta but not really :)
Daeriel
March 5th, 2003, 10:29 PM
It's not hard to check the command line arguments, but service control manager doesn't pass any additional arguments so there's nothing to check. And if I run my service executable from windows explorer or command line console there will be no command line args too. So I must MANUALLY pass special parameters to skip call to StartServiceControlDispatcher.
Actually I use -debug command line argument to start the service code in the debug mode. I just want to check the situation when someone starts an application from windows explorer or from command-line with no arguments passed and my service executable was able to output a help prompt and not to start a service in debug mode or do something else including a try to run as a service by calling to StartServiceControlDispatcher.
Am I clear to understand now? :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.