Solved

Application for Start and Stop NT services...

Posted on 2002-05-02
12
245 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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
 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 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