Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Application for Start and Stop NT services...

Posted on 2002-05-02
12
246 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

856 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