?
Solved

Stop NT service

Posted on 2003-02-23
1
Medium Priority
?
252 Views
Last Modified: 2010-04-01
How do I stop NT/Win2k service using code(not net stop)?
0
Comment
Question by:alto
[X]
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
1 Comment
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 8003990
You would do it like

void StopService ( LPCTSTR pszServiceName)
{
     SERVICE_STATUS ssStatus;
    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,
                                                       pszServiceName,
                                                       SERVICE_ALL_ACCESS
                                                  );

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

                                   Sleep     (     1000 );

                                   while     (     QueryServiceStatus     (     schService,    
                                                                                &ssStatus
                                                                           )
                                             )
                                             {
                                                  if     (          SERVICE_STOP_PENDING    
                                                            ==     ssStatus.dwCurrentState
                                                       )
                                                       {
                                                            wprintf     (     TEXT     (     "."));
                                                            Sleep( 1000 );
                                                       }
                                                  else
                                                            break;
                                             }

                                   if     (     SERVICE_STOPPED     ==     ssStatus.dwCurrentState)
                                             wprintf     (     TEXT     (     "\n%s stopped.\n"),
                                                            pszServiceName
                                                       );
                                   else
                                             wprintf     (     TEXT     (     "\n%s failed to stop.\n"),
                                                            pszServiceName
                                                       );

                              }

                    CloseServiceHandle     (     schService);
               }
          }
}
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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

765 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