Solved

Application for Start and Stop NT services...

Posted on 2002-05-02
12
241 Views
Last Modified: 2010-04-02
Hi !

I need a C/C++ source code for make a application for start and stop services... example:

MyApp ServiceName Start
MyApp ServiceName Stop

Thanks

0
Comment
Question by:masvmasv
  • 6
  • 2
  • 2
  • +1
12 Comments
 

Author Comment

by:masvmasv
ID: 6986781
For a better history...
I cant use "NET START" and
"NET STOP" commands.. ok ?
0
 
LVL 32

Expert Comment

by:jhance
ID: 6986789
Why can't you use NET START and NET STOP?
0
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 6986791
Use

void CmdStopService( LPCTSTR szServiceName)
{
    SERVICE_STATUS  ssStatus;       // current status of the service
     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, szServiceName, SERVICE_ALL_ACCESS);

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

                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"), szServiceName);
                else
                    _tprintf(TEXT("\n%s failed to stop.\n"), szServiceName );

            }


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

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

The same code can also start the service - just replace 'STOP' by 'START'.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6986796
0
 
LVL 86

Expert Comment

by:jkr
ID: 6986800
0
 
LVL 86

Expert Comment

by:jkr
ID: 6986803
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:mblat
ID: 6986900
Microsoft has TN in MSDN

Creating a Simple Win32 Service in C++

by Nigel Thompson


Hope it helps
0
 
LVL 86

Expert Comment

by:jkr
ID: 6986935
>>Creating a Simple Win32 Service in C++

Unfortunately, this document is no longer available via MSDN...
0
 
LVL 4

Expert Comment

by:mblat
ID: 6986944
Hmm... I am running January 2001 library and it is there...
0
 
LVL 86

Expert Comment

by:jkr
ID: 6986970
Well, I meant the online version :o)
0
 

Author Comment

by:masvmasv
ID: 6987130
Hi !

This sample works fine for stop services...
But for Start service is not only change the STOP to START... but I found another example...

But this your example was good for help me...

Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 6987150
Sorry, you are right - you'd have to use 'StartService()' instead of 'ControlService()' in the above sample in order to adapt it to start a service...
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now