Service Application-easy example

V Turbo C++ si vytvoríme nový project:
Súbor->Nový->Ostatné->Service Application / (File->New->Other->Service Application)
Pridáme udalosť komponenty TService1:Object inspector ->Events->dvojklik na OnExecute čo nás prepne do editoru, kde sa nám vytvorila nová udalosť do ktorej pridáme:

while(!Terminated)
{
Beep();
Sleep(1000);
ServiceThread->ProcessRequests(false);
} 

Inštalácia služby:
Run->Parameters a do prostredného textboxu(Parameters:) dopíšeme hodnotu: /INSTALL
Odinštalácia služby:
Run->Parameters a do prostredného textboxu(Parameters:) dopíšeme hodnotu: /UNINSTALL
Spustenie služby:
Start->Control panel->Administrative Tools->Services->nájdeme a označíme názov našej služby a klikneme na Start (pred spustením je samozrejme službu nainštalovať cez parameter /INSTALL

[všetky úkony sa samozrejme dajú spraviť aj cez command line (cesta_k_službe /INSTALL /UNINSTALL /NAME=nazov a podobne)]

---Service_test.cpp---

//---------------------------------------------------------------------------
#include "Service_test.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TService1 *Service1;
//---------------------------------------------------------------------------
__fastcall TService1::TService1(TComponent* Owner)
	: TService(Owner)
{
}

TServiceController __fastcall TService1::GetServiceController(void)
{
	return (TServiceController) ServiceController;
}

void __stdcall ServiceController(unsigned CtrlCode)
{
	Service1->Controller(CtrlCode);
}
//---------------------------------------------------------------------------
void __fastcall TService1::ServiceExecute(TService *Sender)
{

while(!Terminated)
{
Beep();
Sleep(1000);
ServiceThread->ProcessRequests(false);
}
}
//---------------------------------------------------------------------------

---Service_test.h---

//---------------------------------------------------------------------------
#ifndef Service_testH
#define Service_testH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Classes.hpp>
#include <SvcMgr.hpp>
#include <vcl.h>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TService1 : public TService
{
__published:    // IDE-managed Components
	void __fastcall ServiceExecute(TService *Sender);
private:        // User declarations
public:         // User declarations
	__fastcall TService1(TComponent* Owner);
	TServiceController __fastcall GetServiceController(void);

	friend void __stdcall ServiceController(unsigned CtrlCode);
};
//---------------------------------------------------------------------------
extern PACKAGE TService1 *Service1;
//---------------------------------------------------------------------------
#endif 

Leave a Reply