|
begin process at 2008 08 08 21:40:21
Derniers logiciels
|
Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
GESTION DES SERVICES WINDOWS PAR LES API DANS UNE CLASSE
Information sur la source
Description
Pour les amateurs de programmations systèmes; voici un code source intéressant (du moin pour moi)pour contrôler le fonctionnement d'un service ou process sur pc local ou distant: -Apprentissage d'insertion de variable environnement (getenv)dans le code (Nom du pc local dans le constructeur par défaut). -Service STOP-START-RESTART (distant-local). -Exemple d'un lancement d'une commande système par le biais de son programme (system). -Lancement d'une commande d'un process de sont programme 'pskill'. Rem:par le biais d'un prog externe (psexec.exe) il y a moyen de lancer des commandes système sur d'autres pc. NB:Ce code n'est qu'a titre d'exemple pour donner des idées à la programmation C/C++ pas pour dire cela est le meilleur code - à bonne entendeur salut! j'accepte toutes les remarques valables au niveau programmation pour l'évolution. Ceci à été placé dans une classe pour démonstartion d'un code autre que procédurale. Code à la fin pour tester la classe (reamon est un process d'antivirus n'oublié pas de le changer pour vos tests). A+
Source
- #include <windows>
- #include <stdlib>
- #include <iostream.h>
-
- //--------------------------------------------------------
- // DEBUT CLASSE SERVICE
- //--------------------------------------------------------
- class Cl_SVR {
- protected:
- string service;
- string nompc;
- int status;
- SC_HANDLE hSCManager;
- SC_HANDLE hService;
- BOOL bServiceStatus;
- BOOL bControlService;
- BOOL bStartServices;
- SERVICE_STATUS ServiceStatus;
- void Status();
- string getStatusString(int);
-
- public: // Les données et les méthodes publiques.
- Cl_SVR();
- Cl_SVR(const char *,const char *);
- ~Cl_SVR();
- string StatusSVR();
- void ChangeService(const char *);
- void ChangePc(const char *);
- string NomPc()const;
- string Service()const;
- void Stop();
- void Start();
- void ReStart();
- void PsKill(const char *);
- };
- //--------------------------------------------------------
- // FIN CLASSE SERVICE
- //--------------------------------------------------------
- //------------------------------------------------------------------------------
- // FONCTION : Constructeur
- // DESCRIPTION : Initialisation par défaut
- //-------------------------------------------------------------------------------
- Cl_SVR::Cl_SVR(): service("inort"), nompc("\\\\"){
- nompc+=getenv("COMPUTERNAME");
- Status();
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : Constructeur
- // DESCRIPTION : Initialisation par défaut
- //-------------------------------------------------------------------------------
- Cl_SVR::Cl_SVR(const char * s,const char * pc): service(s), nompc("\\\\"){
- nompc+=pc;
- Status();
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : Destructeur
- // DESCRIPTION : Destruction de l'objet
- //-------------------------------------------------------------------------------
- Cl_SVR::~Cl_SVR(){
- }//end procedure
-
- //------------------------------------------------------------------------------
- // FONCTION : Service_Status
- // DESCRIPTION : Permet de savoir le status de fonctionnement du service
- // RUNNING,STOOPED,PAUSE,PENDING,etc....
- //-------------------------------------------------------------------------------
- void Cl_SVR::Status(){
- //Fait la liaison avec le process manager des services
- hSCManager = OpenSCManager(nompc.c_str(),NULL,SC_MANAGER_ALL_ACCESS);
- if (hSCManager !=0){
- hService = OpenService(hSCManager,service.c_str(),SERVICE_ALL_ACCESS);
- //Connection avec le service
- if (hService !=0){
- //Demande le status du service
- bServiceStatus = QueryServiceStatus(hService,&ServiceStatus);
- switch(ServiceStatus.dwCurrentState){
- case SERVICE_STOPPED:
- status=0;
- break;
- case SERVICE_START_PENDING:
- status=1;
- break;
- case SERVICE_STOP_PENDING:
- status=2;
- break;
- case SERVICE_RUNNING:
- status=3;
- break;
- case SERVICE_CONTINUE_PENDING:
- status=4;
- break;
- case SERVICE_PAUSE_PENDING:
- status=5;
- break;
- case SERVICE_PAUSED:
- status=6;
- break;
- }//end switch
- }else
- status=7;
- //end if
- CloseServiceHandle(hService);
- }//end if
- CloseServiceHandle(hSCManager);
- }//end process
- //------------------------------------------------------------------------------
- // FONCTION : Status
- // DESCRIPTION : Renvoie le status de fonctionnement du service
- //-------------------------------------------------------------------------------
- string Cl_SVR::StatusSVR(){
- return getStatusString(status);
- }//end function
- //------------------------------------------------------------------------------
- // FONCTION : ChangeService
- // DESCRIPTION : Change le nom du service
- //-------------------------------------------------------------------------------
- void Cl_SVR::ChangeService(const char *s){
- service=s;
- }//end function
- //------------------------------------------------------------------------------
- // FONCTION : ChangePc
- // DESCRIPTION : Change le nom du pc
- //-------------------------------------------------------------------------------
- void Cl_SVR::ChangePc(const char * pc){
- nompc=pc;
- }//end function
- //------------------------------------------------------------------------------
- // FONCTION : NomPc
- // DESCRIPTION : Renvoie le nom du pc
- //-------------------------------------------------------------------------------
- string Cl_SVR::NomPc()const{
- return nompc;
- }//end function
- //------------------------------------------------------------------------------
- // FONCTION : Service
- // DESCRIPTION : Renvoie le nom du Service
- //-------------------------------------------------------------------------------
- string Cl_SVR::Service()const{
- return service;
- }//end function
- //------------------------------------------------------------------------------
- // FONCTION : Service_Stop
- // DESCRIPTION : Permet d'arreter le fonctionnement d'un service.
- //-------------------------------------------------------------------------------
- void Cl_SVR::Stop(){
- //Fait la liaison avec le process manager des services
- hSCManager = OpenSCManager(nompc.c_str(),NULL,SC_MANAGER_ALL_ACCESS);
- if (hSCManager !=0){
- //Connection avec le service
- hService = OpenService(hSCManager,service.c_str(),SERVICE_ALL_ACCESS);
- if (hService !=0){
- bControlService = ControlService(hService,SERVICE_CONTROL_STOP,&ServiceStatus);
- if (bControlService != 0)
- status=0;
- //end if
- CloseServiceHandle(hService);
- }//End If
- CloseServiceHandle(hSCManager);
- }//End If
- }//end function
- //------------------------------------------------------------------------------
- // FONCTION : Service_Start
- // DESCRIPTION : Permet de demarrer un service.
- //-------------------------------------------------------------------------------
- void Cl_SVR::Start(){
- //Fait la liaison avec le process manager des services
- hSCManager = OpenSCManager(nompc.c_str(),NULL,SC_MANAGER_ALL_ACCESS);
- if (hSCManager !=0){
- //Connection avec le service
- hService = OpenService(hSCManager,service.c_str(),SERVICE_ALL_ACCESS);
- if (hService !=0){
- bStartServices = StartService(hService,0,0);
- if (bStartServices != 0)
- status=3;
- //end if
- CloseServiceHandle(hService);
- }//End If
- CloseServiceHandle(hSCManager);
- }//End If
- }//end function
- //------------------------------------------------------------------------------
- // FONCTION : ReStart
- // DESCRIPTION : Permet de restarter un service
- //-------------------------------------------------------------------------------
- void Cl_SVR::ReStart(){
- //Fait la liaison avec le process manager des services
- hSCManager = OpenSCManager(nompc.c_str(),NULL,SC_MANAGER_ALL_ACCESS);
- if (hSCManager !=0){
- //Connection avec le service
- hService = OpenService(hSCManager,service.c_str(),SERVICE_ALL_ACCESS);
- if (hService !=0){
- //stop du service
- bControlService = ControlService(hService,SERVICE_CONTROL_STOP,&ServiceStatus);
- //Boucle d'attente de l'arret
- do{
- bServiceStatus = QueryServiceStatus(hService,&ServiceStatus);
- }while(ServiceStatus.dwCurrentState != SERVICE_STOPPED);
- //end do
- status=0;
- cout << "Service '" << service << "' est en état " << StatusSVR() << endl;
- //start du service
- bStartServices = StartService(hService,0,0);
- //Boucle d'attente de demarrage
- do{
- bServiceStatus = QueryServiceStatus(hService,&ServiceStatus);
- }while(ServiceStatus.dwCurrentState != SERVICE_RUNNING);
- //end do
- status=3;
- cout << "Service '" << service << "' est en état " << StatusSVR() << endl;
- CloseServiceHandle(hService);
- }//End If
- CloseServiceHandle(hSCManager);
- }//End If
- }//end function
- //------------------------------------------------------------------------------
- // FONCTION : PsKill
- // DESCRIPTION : Renvoie le nom du Service
- //-------------------------------------------------------------------------------
- void Cl_SVR::PsKill(const char * proc){
- string cmd;
- cmd = "pskill -t " + nompc + " " + proc + " >log.txt";
- system(cmd.c_str());
- }//end function
-
- //------------------------------------------------------------------------------
- // FONCTION : PsKill
- // DESCRIPTION : Renvoie le nom du Service
- //-------------------------------------------------------------------------------
- string Cl_SVR::getStatusString(int nStatus){
- switch (nStatus){
- case 0: return "Stopped";
- case 1: return "Pending Start";
- case 2: return "Pending Stop";
- case 3: return "Running";
- case 4: return "Pending Continu";
- case 5: return "Pending Pause";
- case 6: return "Paused";
- default: break;
- }//end switch
- return "N\'exite pas";
- }//end function
-
- //------------------------------------------------------------------------------
- // START PROGRAM TEST
- //-------------------------------------------------------------------------------
- void main(){
- Cl_SVR p;
- cout << p.StatusSVR() << endl;
- p.Stop();
- cout << p.StatusSVR() << endl;
- // p.ReStart();
- // p.Start();
- // p.PsKill("Realmon.exe"); //Kill une application en mémoire(Seulement valable en local)
- // system("Realmon.exe"); //relance l'application en mémoire(Seulement valable en local)
-
- }//end programme
- //------------------------------------------------------------------------------
- // END PROGRAM TEST
- //-------------------------------------------------------------------------------
-
#include <windows>
#include <stdlib>
#include <iostream.h>
//--------------------------------------------------------
// DEBUT CLASSE SERVICE
//--------------------------------------------------------
class Cl_SVR {
protected:
string service;
string nompc;
int status;
SC_HANDLE hSCManager;
SC_HANDLE hService;
BOOL bServiceStatus;
BOOL bControlService;
BOOL bStartServices;
SERVICE_STATUS ServiceStatus;
void Status();
string getStatusString(int);
public: // Les données et les méthodes publiques.
Cl_SVR();
Cl_SVR(const char *,const char *);
~Cl_SVR();
string StatusSVR();
void ChangeService(const char *);
void ChangePc(const char *);
string NomPc()const;
string Service()const;
void Stop();
void Start();
void ReStart();
void PsKill(const char *);
};
//--------------------------------------------------------
// FIN CLASSE SERVICE
//--------------------------------------------------------
//------------------------------------------------------------------------------
// FONCTION : Constructeur
// DESCRIPTION : Initialisation par défaut
//-------------------------------------------------------------------------------
Cl_SVR::Cl_SVR(): service("inort"), nompc("\\\\"){
nompc+=getenv("COMPUTERNAME");
Status();
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : Constructeur
// DESCRIPTION : Initialisation par défaut
//-------------------------------------------------------------------------------
Cl_SVR::Cl_SVR(const char * s,const char * pc): service(s), nompc("\\\\"){
nompc+=pc;
Status();
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : Destructeur
// DESCRIPTION : Destruction de l'objet
//-------------------------------------------------------------------------------
Cl_SVR::~Cl_SVR(){
}//end procedure
//------------------------------------------------------------------------------
// FONCTION : Service_Status
// DESCRIPTION : Permet de savoir le status de fonctionnement du service
// RUNNING,STOOPED,PAUSE,PENDING,etc....
//-------------------------------------------------------------------------------
void Cl_SVR::Status(){
//Fait la liaison avec le process manager des services
hSCManager = OpenSCManager(nompc.c_str(),NULL,SC_MANAGER_ALL_ACCESS);
if (hSCManager !=0){
hService = OpenService(hSCManager,service.c_str(),SERVICE_ALL_ACCESS);
//Connection avec le service
if (hService !=0){
//Demande le status du service
bServiceStatus = QueryServiceStatus(hService,&ServiceStatus);
switch(ServiceStatus.dwCurrentState){
case SERVICE_STOPPED:
status=0;
break;
case SERVICE_START_PENDING:
status=1;
break;
case SERVICE_STOP_PENDING:
status=2;
break;
case SERVICE_RUNNING:
status=3;
break;
case SERVICE_CONTINUE_PENDING:
status=4;
break;
case SERVICE_PAUSE_PENDING:
status=5;
break;
case SERVICE_PAUSED:
status=6;
break;
}//end switch
}else
status=7;
//end if
CloseServiceHandle(hService);
}//end if
CloseServiceHandle(hSCManager);
}//end process
//------------------------------------------------------------------------------
// FONCTION : Status
// DESCRIPTION : Renvoie le status de fonctionnement du service
//-------------------------------------------------------------------------------
string Cl_SVR::StatusSVR(){
return getStatusString(status);
}//end function
//------------------------------------------------------------------------------
// FONCTION : ChangeService
// DESCRIPTION : Change le nom du service
//-------------------------------------------------------------------------------
void Cl_SVR::ChangeService(const char *s){
service=s;
}//end function
//------------------------------------------------------------------------------
// FONCTION : ChangePc
// DESCRIPTION : Change le nom du pc
//-------------------------------------------------------------------------------
void Cl_SVR::ChangePc(const char * pc){
nompc=pc;
}//end function
//------------------------------------------------------------------------------
// FONCTION : NomPc
// DESCRIPTION : Renvoie le nom du pc
//-------------------------------------------------------------------------------
string Cl_SVR::NomPc()const{
return nompc;
}//end function
//------------------------------------------------------------------------------
// FONCTION : Service
// DESCRIPTION : Renvoie le nom du Service
//-------------------------------------------------------------------------------
string Cl_SVR::Service()const{
return service;
}//end function
//------------------------------------------------------------------------------
// FONCTION : Service_Stop
// DESCRIPTION : Permet d'arreter le fonctionnement d'un service.
//-------------------------------------------------------------------------------
void Cl_SVR::Stop(){
//Fait la liaison avec le process manager des services
hSCManager = OpenSCManager(nompc.c_str(),NULL,SC_MANAGER_ALL_ACCESS);
if (hSCManager !=0){
//Connection avec le service
hService = OpenService(hSCManager,service.c_str(),SERVICE_ALL_ACCESS);
if (hService !=0){
bControlService = ControlService(hService,SERVICE_CONTROL_STOP,&ServiceStatus);
if (bControlService != 0)
status=0;
//end if
CloseServiceHandle(hService);
}//End If
CloseServiceHandle(hSCManager);
}//End If
}//end function
//------------------------------------------------------------------------------
// FONCTION : Service_Start
// DESCRIPTION : Permet de demarrer un service.
//-------------------------------------------------------------------------------
void Cl_SVR::Start(){
//Fait la liaison avec le process manager des services
hSCManager = OpenSCManager(nompc.c_str(),NULL,SC_MANAGER_ALL_ACCESS);
if (hSCManager !=0){
//Connection avec le service
hService = OpenService(hSCManager,service.c_str(),SERVICE_ALL_ACCESS);
if (hService !=0){
bStartServices = StartService(hService,0,0);
if (bStartServices != 0)
status=3;
//end if
CloseServiceHandle(hService);
}//End If
CloseServiceHandle(hSCManager);
}//End If
}//end function
//------------------------------------------------------------------------------
// FONCTION : ReStart
// DESCRIPTION : Permet de restarter un service
//-------------------------------------------------------------------------------
void Cl_SVR::ReStart(){
//Fait la liaison avec le process manager des services
hSCManager = OpenSCManager(nompc.c_str(),NULL,SC_MANAGER_ALL_ACCESS);
if (hSCManager !=0){
//Connection avec le service
hService = OpenService(hSCManager,service.c_str(),SERVICE_ALL_ACCESS);
if (hService !=0){
//stop du service
bControlService = ControlService(hService,SERVICE_CONTROL_STOP,&ServiceStatus);
//Boucle d'attente de l'arret
do{
bServiceStatus = QueryServiceStatus(hService,&ServiceStatus);
}while(ServiceStatus.dwCurrentState != SERVICE_STOPPED);
//end do
status=0;
cout << "Service '" << service << "' est en état " << StatusSVR() << endl;
//start du service
bStartServices = StartService(hService,0,0);
//Boucle d'attente de demarrage
do{
bServiceStatus = QueryServiceStatus(hService,&ServiceStatus);
}while(ServiceStatus.dwCurrentState != SERVICE_RUNNING);
//end do
status=3;
cout << "Service '" << service << "' est en état " << StatusSVR() << endl;
CloseServiceHandle(hService);
}//End If
CloseServiceHandle(hSCManager);
}//End If
}//end function
//------------------------------------------------------------------------------
// FONCTION : PsKill
// DESCRIPTION : Renvoie le nom du Service
//-------------------------------------------------------------------------------
void Cl_SVR::PsKill(const char * proc){
string cmd;
cmd = "pskill -t " + nompc + " " + proc + " >log.txt";
system(cmd.c_str());
}//end function
//------------------------------------------------------------------------------
// FONCTION : PsKill
// DESCRIPTION : Renvoie le nom du Service
//-------------------------------------------------------------------------------
string Cl_SVR::getStatusString(int nStatus){
switch (nStatus){
case 0: return "Stopped";
case 1: return "Pending Start";
case 2: return "Pending Stop";
case 3: return "Running";
case 4: return "Pending Continu";
case 5: return "Pending Pause";
case 6: return "Paused";
default: break;
}//end switch
return "N\'exite pas";
}//end function
//------------------------------------------------------------------------------
// START PROGRAM TEST
//-------------------------------------------------------------------------------
void main(){
Cl_SVR p;
cout << p.StatusSVR() << endl;
p.Stop();
cout << p.StatusSVR() << endl;
// p.ReStart();
// p.Start();
// p.PsKill("Realmon.exe"); //Kill une application en mémoire(Seulement valable en local)
// system("Realmon.exe"); //relance l'application en mémoire(Seulement valable en local)
}//end programme
//------------------------------------------------------------------------------
// END PROGRAM TEST
//-------------------------------------------------------------------------------
Conclusion
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland Cl_SVR.cpp: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland pas d'erreur de compilation
Historique
- 02 juin 2006 22:14:30 :
- Correction du code source suivant les bonnes
remarques de excrt. J'espère qu'il sera content.
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
CalendriCode
| | | L | M | M | J | V | S | D |
| | | | | 1 | 2 | 3 |
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
|
Téléchargements
Logiciels à télécharger sur le même thème :
|
|