begin process at 2012 02 08 22:33:49
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

API

 > GESTION DES SERVICES WINDOWS PAR LES API DANS UNE CLASSE

GESTION DES SERVICES WINDOWS PAR LES API DANS UNE CLASSE


 Information sur la source

Note :
Aucune note
Catégorie :API Classé sous :process, service, api Niveau :Initié Date de création :02/06/2006 Date de mise à jour :02/06/2006 22:14:29 Vu :7 229

Auteur : UKR6900

Ecrire un message privé
Commentaire sur cette source (5)
Ajouter un commentaire et/ou une note

 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 du même auteur

RÉCUPÉRATION DES DATES D'UN FICHIER EN CLAIR
GESTION BASE DE REGISTRE (LOCAL OU DISTANTE)AVEC DES FONCTIO...
Source avec Zip CRÉATION D'UNE CLASSE CL_STRING (GESTION DES CHAÎNES)
GESTION D'UN ARBRE BINAIRE PAR LES CLASSES
GESTION D'UNE FILE PAR LES CLASSES

 Sources de la même categorie

Source avec Zip WIN32 TLS LENT par dguilmain
Source avec Zip VIDER ELEMENTS DE CORBEILLE WINDOWS7 (WIN64) par BruNews
Source avec Zip Source avec une capture FIND TEXT (WIN64) par BruNews
Source avec Zip DELETE DIRECTORY (WIN64) par BruNews
Source avec Zip ENUM DIRECTORY (WIN64) par BruNews

 Sources en rapport avec celle ci

Source avec Zip VIDER ELEMENTS DE CORBEILLE WINDOWS7 (WIN64) par BruNews
Source avec Zip Source avec une capture FIND TEXT (WIN64) par BruNews
Source avec Zip DELETE DIRECTORY (WIN64) par BruNews
Source avec Zip ENUM DIRECTORY (WIN64) par BruNews
Source avec Zip Source avec une capture [C/WIN32][DRIVER] DÉTECTION DE CRÉATION OU DE SUPPRESSION DE... par deck_bsd

Commentaires et avis

Commentaire de excrt le 02/06/2006 17:49:12

pour la Xeme fois

#include <stdlib.h> // C
#include <cstdlib> // C++
#include <iostream.h> // BAD! sans .h << cette version est obsolète(elle existe encore pour une raison de compatibilité avec de ~vieux programmes)
#include <iostream> // GOOD

ta gestion des chaines de caractères est _Complètement_ nulle

Cl_SVR::Cl_SVR(){
    service="\0"; // c'est un char*, pas d'allocation mémoire ni rien d'autre du genre
    strcat(service,"inort"); // ca va faire !!!CABOUME!!!
    nompc="\\\\"; strcat(nompc,getenv("COMPUTERNAME")); // ca aussi!
    Status();
}//end procedure

pourquoi est-ce que tu n'utiliserais pas les basic_string hein?

#include <string>

class Cl_SVR
{
// ...
string service;
string nompc;
// ...
};

Cl_SVR::Cl_SVR() : service("inort"), nompc("\\\\")
{
    nompc += getenv("COMPUTERNAME");
    Status();
}//end procedure

Cl_SVR::Cl_SVR(const char * s,const char * pc) : service(s), nompc("\\\\")
{
    nompc += pc;
    Status();
}//end procedure

service.c_str() pour obtenir un pointeur sur la chaine

const char* c_str() const {
  return un_pointeur_sur_la_chaine;
}


etc...
etc...
etc...

void Cl_SVR::PsKill(const char * proc)
{
  //char * cmd = new (char[100]);
  char cmd[100+1]; // si tu ne veux pas libérer la mémoire alors pas de « new » !
  snprintf(cmd, 100, "pskill -t %s %s >log.txt", nompc, proc);
  //strcpy(cmd,"\0");
  //strcat(cmd,"pskill -t ");
  //strcat(cmd,nompc);
  //strcat(cmd," ");
  //strcat(cmd,proc);
  //strcat(cmd," >log.txt");
  system(cmd);
}//end function

Je suis désolé de te le dire mais ton code n'est pas bon, mais pas du tout!
Va reviser la « Gestion Des Chaînes De Caractères » !!!

si tu utilise un « char* », tu dois lui assigner/allouer de la _Mémoire_
tu ne peux pas manipuler ton pointeur, qui, pointe sur _Rien_ !

sinon, comme j'ai dit plus haut, utilise basic_string >> #include <string>
avec basic_string, pas besoin de t'occuper de la gestion de la mémoire, basic_string le fait tout seul comme un grand!

autre petite chose, pour le « status », utilise un entier quelconque pour connaître le status et non une chaine, avec un entier ca serait tellement plus simple/rapide/... et surtout, beaucoup mais beaucoup moins lourd à gérer. si tu tiens absolument a obtenir le status en « texte », ajoute une simple/petite/... fonction qui va te retourner le status(en texte)

const char* getStatusString(int nStatus)
{
  switch (nStatus)
  {
    case X: return "status X";
    case Y: return "status Y";
    // ...
    default: break;
  }
  return "unknown status";
}


...
...
...

Commentaire de excrt le 02/06/2006 17:50:47

en passant, retire cette phrase « pas d'erreur de compilation »
je ne veux pas être méchant mais c'est ridicule puisque
« 0 error(s), 0 warning(s) » ne veut strictement _rien_ dire ...

Commentaire de excrt le 02/06/2006 17:59:41

dernière petite chose

BOOL hServiceStatus;
BOOL hControlService;
BOOL hStartServices;

« h » désigne un « HANDLE », pas un BOOL

BOOL bServiceStatus;
BOOL bControlService;
BOOL bStartServices;
// serait plus ... approprié

  // ton code
  hServiceStatus = QueryServiceStatus( ... );

quand on lit ca, on ce dit, ah! QueryServiceStatus() retourne un « HANDLE », mais non! c'est faux! QueryServiceStatus() retourne un « BOOL » !

Commentaire de BruNews le 02/06/2006 20:28:14 administrateur CS

C'est très clair, la compil ok indique seulement que la syntaxe du langage est correcte, que le prog ne plante pas est une toute autre affaire.

Commentaire de Micky0001 le 02/06/2009 21:23:25

je sais ca deterre mais il y a un probleme
ne pas oublier le using namepsace std;
sinon ca fais tt foirer ^^

 Ajouter un commentaire


Discussions en rapport avec ce code source dans le forum

C - WIN32 API - [ par philip63 ] Bonjour,Mon service tourne sousW2k en Local System; il est non-interactif (Service-0x0-3e7$\Default). Aprés avoir testé l'absence du Shell par defaut DDK et service. [ par deck_bsd ] Yop à tous,Voila je travail sur un service que j'avais codé sous XP et qui fait un acces à la registry avec les API Reg... et qui biensur ne fonctionn comment executer une fonction toutes les 3secondes [ par 3xodiuS ] Bonjour,j'essaie de un service windows qui cache la barre des taches. Cependant losrque je l'executais, des fois elle revenais toute seule, donc j'ai crypter ou cacher appel aux api [ par jb212121 ] Bonsoir,J'aimerai savoir s' il est possible de crypter ou de cacher l'appel d'une apiMerci CreateRemoteThread [ par draluorg ] Salut a tous,J'essai d'appeler une fonction dans un autre process en utilisant CreateRemoThread mais j'ai quelques problemes.En fait, j'ai aucun probl Treeview API WINDOWS [ par lektrosonic ] Bonjour,je code en C et en API Windows une applicationJ'ai un treeview et je voudrais mettre a jour le texte de un de ses elements sans le supprimer.J Affichage selection d'un listview quand il n'a pas le focus [ par hwoarang ] Bonjour,J'ai cherché sur internet mais je n'ai pas reussi a trouver comment, avec l'API windows (en C). En C++, j'ai trouvé le membre HideSelection qu treeview API windows [ par lektrosonic ] Bonsoir, en C et avec api windows..je souhaite modifier le texte d un element d un treeview sans le supprimer.j'ai esseye le message TVM_SETITEM mais service et base de donnee reseaux [ par gamemonde ] oui bonjourj&lt;ai creer un service windows et je dois me connecter a l'aide de ado sur une base de donnee d&lt;un autre reseauxl&lt;orsque c&lt;est u Icones et service [ par albanovisch ] Bonjour, je cherche à savoir quel est le message envoyé par Windows pour indiquer qu'un utilisateur c'est logé sur le poste.Voila j'ai déclaré un serv


Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,295 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales