Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Service Control Manager Wrapper class (C++)



Permlink Replies: 0 Threads: [ Previous | Next ]
Arkady Semylio

Posts: 87
Registered: 9/18/15
Service Control Manager Wrapper class (C++)
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 16, 2016 9:46 AM
Attachment SvcCtrlMngr.zip (3.4 KB)
Jayme Jeffman wrote:
I was searching on the Internet a way of stopping a Windows service at runtime by an external interference.
All the methods I have found involve calls to the SCM ( Service Control Manager ) with complex code.

The class TServiceControlManager has several methods. Two of these methods are start and stop.
Here an example of to use the class:

Unit1.cpp
#include <vcl.h>
#pragma hdrstop
 
#include "Unit1.h"
 
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
 
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
    , SCM( 0, _T( "YourServiceName" ) )
{
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::btnStartClick(TObject *Sender)
{
    SCM.Start( 60000 );  // Wait 60 secs
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    SCM.Stop( 60000 );  // Wait 60 secs
}
//---------------------------------------------------------------------------


Unit1.h
//---------------------------------------------------------------------------
 
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
 
#include "SvcCtrlMngr.h"
 
//---------------------------------------------------------------------------
 
class TForm1 : public TForm
{
__published:	// IDE-managed Components
    TButton *btnStart;
    TButton *Button1;
    void __fastcall btnStartClick(TObject *Sender);
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall btnRestartClick(TObject *Sender);
private:	// User declarations
    TServiceControlManager SCM;
public:		// User declarations
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


Bye bye
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02