• C

How to stop a running NT service?

Hi,
 We have an NT service running with a Java program as the process. We have prepared the NT service in C - program.
The question is when we press the "stop" for that NT Service, the services dialog is still showing that service as running.. If we try to start that service again immediately, it is giving an error.
 How to STOP a running service from the program?
Please reply as soon as possible,.

Thanks
Madhura
Madhura122398Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Use the following code to stop a service:

void StopService()
{
    SC_HANDLE   schService;
    SC_HANDLE   schSCManager;

    schSCManager = OpenSCManager(
                        NULL,                   // machine (NULL == local)
                        NULL,                   // database (NULL == default)
                        SC_MANAGER_ALL_ACCESS   // access required
                        );
    if ( schSCManager )
    {
        schService = OpenService(schSCManager, TEXT(SZSERVICENAME), SERVICE_ALL_ACCESS);

        if (schService)
        {
            // try to stop the service
            if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
            {
                _tprintf(TEXT("Stopping %s."), TEXT(SZSERVICEDISPLAYNAME));
                Sleep( 1000 );

                while( QueryServiceStatus( schService, &ssStatus ) )
                {
                    if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
                    {
                        _tprintf(TEXT("."));
                        Sleep( 1000 );
                    }
                    else
                        break;
                }

                if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
                    _tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) );
                else
                    _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) );

            }


            CloseServiceHandle(schService);
        }
        else
            _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));

        CloseServiceHandle(schSCManager);
    }
    else
        _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
}

Where 'SZSERVICENAME' is the internal service name (e.g. 'RasMan') and 'SZSERVICEDISPLAYNAME' is the display name (e.g. 'Ras Connection Manager')
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Madhura122398Author Commented:
Thnk you very much
This is working absolutely fine.
We have one more problem.
In some NT systems the Start of the service is giving DrWatson error. The exception is AccessViolation.
When starting and stopping the service we are logging into some log file which would be created in the winnt\system32 directory.
Does it cause any problem? What can be the reason?
Please reply asap.
Thanks in advance.
0
jkrCommented:
>>Does it cause any problem? What can
>>be the reason?

Logging should be no problem, unless you don't use the log file for several facilities - if so, you'll have to synchronize access to the file.

The reason for an access violation can be a variety of things, most likely is accessing memory through an uninitialized pointer...
0
tdubroffCommented:
jkr, do you recommend any books for this type stuff?
0
jkrCommented:
No, sorry, I can't - I prefer the 'learning by doing method', so all I could recommend is MSDN and the SDK docs...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.