[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

Application for Start and Stop NT services...

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
masvmasv
Asked:
masvmasv
  • 6
  • 2
  • 2
  • +1
1 Solution
 
masvmasvAuthor Commented:
For a better history...
I cant use "NET START" and
"NET STOP" commands.. ok ?
0
 
jhanceCommented:
Why can't you use NET START and NET STOP?
0
 
jkrCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mblatCommented:
Microsoft has TN in MSDN

Creating a Simple Win32 Service in C++

by Nigel Thompson


Hope it helps
0
 
jkrCommented:
>>Creating a Simple Win32 Service in C++

Unfortunately, this document is no longer available via MSDN...
0
 
mblatCommented:
Hmm... I am running January 2001 library and it is there...
0
 
jkrCommented:
Well, I meant the online version :o)
0
 
masvmasvAuthor Commented:
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
 
jkrCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now