Solved

Application for Start and Stop NT services...

Posted on 2002-05-02
12
242 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

914 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

14 Experts available now in Live!

Get 1:1 Help Now