Re: Restart Window Service



If someone attempts to stop the service, it has to already be running. If it is already
running, you could not start it because it is already running. If you return
SERVICE_RUNNING when the SCM has requested a stop, then you are failing to conform to what
it requested, so your service would be incorrect.

If you don't want the service stopped, just create it without the ability to stop it.

The problem is you are showing code that does something wrong, without telling us what the
goal of the problem is. (And the code shown is incorrect no matter what the goal is).

The errors I point out are clearly NOT in the URL you gave, so your adaptation of the code
in the URL is erroneous.
joe

On Mon, 3 Mar 2008 04:30:02 -0800, cleohm <cleohm@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi,

Thanks for your reply. Currently, I am using the example from the
following url. I wanted to extend it to start the service when someone
attempts to stop the service. Is this possible? If so how should I go about
ammending the codes?

http://www.codersource.net/win32_nt_service.html

Regards

cleohm

"Joseph M. Newcomer" wrote:

What does this have to do with the code that is trying to restart the service?

Many serious problems with this code, see below.

On Sun, 2 Mar 2008 20:34:01 -0800, cleohm <cleohm@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi,

I am running the following code on WinXP Prof. Personally, I feel that this
is a handler out of scope issue but do not know how I can go about resolving
this. Do help shed some light if possible. Thanks.

service_main(){
serviceStatus.dwServiceType = SERVICE_WIN32;
serviceStatus.dwCurrentState = SERVICE_START_PENDING;
serviceStatus.dwControlsAccepted = 0;
serviceStatus.dwWin32ExitCode = NO_ERROR;
serviceStatus.dwServiceSpecificExitCode = NO_ERROR;
serviceStatus.dwCheckPoint = 0;
serviceStatus.dwWaitHint = 3000;

hServiceStatusHandle = RegisterServiceCtrlHandler(pServiceName,
XYNTServiceHandler);

if (hServiceStatusHandle==0)
{
long nError = GetLastError();
char pTemp[121];
****
Why do you think 121 is a reasonable value? Why do you think AfxMessageBox is reasonable
from a service? Why are you using an obsolete and dangerous C library function like
sprintf? Use either strsafe.h/StringCchPrintf or VS2005+ sprintf_s.
****
sprintf(pTemp, "RegisterServiceCtrlHandler failed, error code = %d",
nError);
AfxMessageBox(pTemp);
return;
}

SetServiceStatus(hServiceStatusHandle, &serviceStatus);
****
If the service is started, why did you not change the state to SERVICE_RUNNING? This may
be the source of your problem. You set start-pending but never set running!

Note that when you return from service_main, the service should be considered terminated;
the correct behavior is to either implement the service loop at this point, or block on an
event object until the stop request comes in. I see no actual service code here, and
there is no evidence a thread started to implement the service (one or more threads are
required to implement the service, and that means either the service is in this thread or
in secondary threads created from this thread) and this function just returns. Given it
has never set the service as running, and then exits, it is not at all surprising the SCM
has become confused.

Read the MSDN on building services, since this code does not resemble any of the examples
I am aware of. Both examples I have found have comments that say things like
"Initialization code goes here" or "This is where the service does its work". and there is
no sign here of either case.
****
}

VOID WINAPI ServiceHandler(DWORD fdwControl)
{
switch(fdwControl)
{
case SERVICE_CONTROL_STOP:
serviceStatus.dwCurrentState =
SERVICE_STOPPED;
serviceStatus.dwCheckPoint
= 0;
serviceStatus.dwWaitHint = 1000;
****
There is no reason to use a wait hint other than 0 if the type is not a _PENDING type
****


SetServiceStatus(hServiceStatusHandle, &serviceStatus));
serviceStatus.dwCurrentState =
SERVICE_RUNNING;
****
Why are you setting SERVICE_RUNNING when you just stopped the service? No wonder the SCM
is confused; you are giving it bogus information.
joe
****

SetServiceStatus(hServiceStatusHandle, &serviceStatus);

break;
}
}

Regards

cleohm

"Joseph M. Newcomer" wrote:

Which version of Windows? What does the code look like? What ACLs were set for the
service? What service?

Otherwise, the answer is "you are doing something either incorrectly, or forbidden. Either
fix the bug, or don't do that"
hie

On Sun, 2 Mar 2008 00:34:01 -0800, cleohm <cleohm@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi,

I have an application that attempts to start a service that is detected to
be stopped by the user using SetServiceStatus. However, this is only
successful on the first time but fails on the subsequent tries with an error
message 'Could not stop the <service name> service on local computer. The
service did not return an error. This could be an internal windows error or
an internal service error. if problem persists, contact your system
administrator'. Anyone knows why?


Regards

cleohm
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Restart Window Service
    ... There is no reason to use a wait hint other than 0 if the type is not a _PENDING type ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: Strong Names and LGPL
    ... that wasn't signed at compile time since the PE file will have an incorrect ... > William Stacey, MVP ... >> The library is not strong named for that reason. ...
    (microsoft.public.dotnet.security)
  • Re: Restart Window Service
    ... There is no reason to use a wait hint other than 0 if the type is not a _PENDING type ...
    (microsoft.public.vc.mfc)
  • Re: You are a f*****t. A moronic, clueless, cretinous, imbecilic, idiotic,
    ... this is one MVP who certainly *will not* stick up for her. ... >to come alive.For some reason some of the regulars in this group (the xp ... >groups) and a few of the MVP's stick up for her, maybe because she is nasty ... >> 3) Your 'advice', as well as being dangerous, is so poorly written ...
    (microsoft.public.windowsxp.newusers)
  • Re: Why is it dangerous?
    ... is offensive for some bizarre reason, fine, someone coin another word. ... being called by a name that they considered to be incorrect. ... English deciding that the term "European" ... "European" might apply to others. ...
    (comp.lang.c)