IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++Builder Discussion :

Fermer une application externe [Sources]


Sujet :

C++Builder

  1. #1
    Membre du Club
    Inscrit en
    Mars 2004
    Messages
    59
    Détails du profil
    Informations forums :
    Inscription : Mars 2004
    Messages : 59
    Points : 56
    Points
    56
    Par défaut Fermer une application externe
    Bonjour

    J'aimerais pouvoir ouvrir et fermer des programmes externe a partir de mon application. J'arrive a ouvrir sans probleme un programme externe grace a "CreateProcess" qui me permet en meme temps de recupérer le PID de l'application lancée. Mon probleme est de reussir a fermer cette application grace a ce PID. J'ai trouvé sur le net un code permettant de faire cela mais il doit comporter quelques erreur http://www.codeproject.com/cpp/kill_process.asp.

    voici le code dans le .h
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    vector<DWORD> processPid;
    static bool CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
    dans le .cpp
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    void TFDemarrageInternet::lancerLogiciel()
    {
       ...
       STARTUPINFO suInfo;
       PROCESS_INFORMATION procInfo;
       string m_Process;
       memset (&suInfo, 0, sizeof(suInfo));
       suInfo.cb = sizeof(suInfo);
       m_Process = cheminExecutable+executable;
     
       CreateProcess(m_Process.c_str(),NULL,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&suInfo,&procInfo);
       if (procInfo.dwThreadId == NULL)
          Application->MessageBoxA("Erreur","nope");
       else
          this->processPid = procInfo.dwProcessId;
       ...
    }
     
    void TFDemarrageInternet::quitterLogiciel()
    {
       ...
      HANDLE ps = OpenProcess( SYNCHRONIZE|PROCESS_TERMINATE,FALSE, this->processPid);
      DWORD wndPid;
      string Title;
     
      // processPid = procInfo.dwProcessId;
     
      // This function enumerates all widows,
      // using the EnumWindowsProc callback
      // function, passing the PID of the process
      // you started earlier.
      EnumWindows(EnumWindowsProc, this->processPid);     <<<--- Pb de compilation
     
      CloseHandle(ps) ;
      ...
    }
     
    bool CALLBACK TFDemarrageInternet::EnumWindowsProc(HWND hwnd, LPARAM lParam)
    {
      DWORD wndPid;
      char * Title;
     
      // lParam = procInfo.dwProcessId;
     
      // This gets the windows handle and pid of enumerated window.
      GetWindowThreadProcessId(hwnd, &wndPid);
     
      // This gets the windows title text
      // from the window, using the window handle
      //CWnd::FromHandle(hwnd)->GetWindowText(Title);     	<<<---ne compile pas sous Builder 6
      GetClassName(hwnd,Title,50);				<<<---Remplacé par cette ligne là
      //  this makes sure that the PID matches that PID we started, and window
      // text exists, before we kill it . I don't think this is really needed,
      // I included it because some apps have more than one window.
      if ( wndPid == (DWORD)lParam && sizeof(Title) != 0)
      {
        //  Please kindly close this process
        ::PostMessage(hwnd, WM_CLOSE, 0, 0);
        return false;
      }
      else
      {
        // Keep enumerating
        return true;
      }
    }
    J'ai une erreur sur EnumWindows(EnumWindowsProc, this->processPid); .Le compilateur m'indique ceci

    [C++ Erreur] F_DemarrageInternet.cpp(217): E2034 Impossible de convertir 'bool (__stdcall *)(void *,long)' en 'int (__stdcall *)()'
    [C++ Erreur] F_DemarrageInternet.cpp(217): E2342 Mauvaise correspondance de type dans le paramètre 'lpEnumFunc' ('int (__stdcall *)()' désiré, 'bool (__stdcall *)(void *,long)' obtenu)

    Je ne connaissais pas les fonctions CALLBACK avant, l'erreur vient peut etre de la. Sinon j'ai l'impression qu'il y a plus simple, n'existerait-il pas une fonction ou une commande windows permettant de quitter une application grace a son PID ?

    merci d'avance.

  2. #2
    Membre du Club
    Inscrit en
    Mars 2004
    Messages
    59
    Détails du profil
    Informations forums :
    Inscription : Mars 2004
    Messages : 59
    Points : 56
    Points
    56
    Par défaut
    j'ai trouvé une autre solution sur un autre site pour fermer une ou plusieurs applications externe :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
     
    #include "stdafx.h"
    #include <tlhelp32.h> //fonction CreateToolhelp32Snapshot
    #include <string.h> //fonction strstr
     
    bool ShellExit(char *ExeName);
     
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR lpCmdLine,
                         int nCmdShow)
    {
        if ( ShellExit("IEXPLORE") == true )
            MessageBox(NULL,"Programme fermé",NULL,NULL);
        else
            MessageBox(NULL, "Le programme n'a pas pu être fermé: processus non trouvé", NULL,NULL);
    }
     
    bool ShellExit(char *ExeName)
    {
        //Recherche du processus:
        HANDLE hSnapShot;
        PROCESSENTRY32 uProcess;
        bool r;
        short PID = 0; //variable qui va stocker l'ID du processus de l'application que l'on désire fermer.
     
        hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL,0);
        uProcess.dwSize = (DWORD) sizeof(PROCESSENTRY32);
     
        r = Process32First(hSnapShot, &uProcess);
        do // Cette boucle énnumère tout les processus
        {
            if ( strstr(uProcess.szExeFile, ExeName) )//on cherche le nom de notre application dans le chemin d'accès de l'éxécutable du processus.
                PID = (short) uProcess.th32ProcessID;
     
            r = Process32Next(hSnapShot, &uProcess);
        } while ( r );
     
        CloseHandle(hSnapShot);
     
        if ( PID == 0)
            return false;
     
        //Fermeture du processus:
        HANDLE hTemp;
     
        hTemp = OpenProcess(PROCESS_ALL_ACCESS, false, (DWORD) PID);
        TerminateProcess(hTemp,0);
     
        return true;
    //Attention: le nom de l'éxécutable est écrit ou tout en majuscule, ou comme il est orthografier sur le disque dur. Il faut parfois faire plusieurs text pour un seul nom d'executable.
    }
    voila pour ceux que ça interesse.

  3. #3
    Membre averti Avatar de niglo
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    379
    Détails du profil
    Informations personnelles :
    Âge : 41
    Localisation : France, Bas Rhin (Alsace)

    Informations forums :
    Inscription : Octobre 2004
    Messages : 379
    Points : 383
    Points
    383
    Par défaut
    Génial ce bout ce code, ca fonctionne nikel.
    Merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Fermer une Application Externe
    Par Invité(e) dans le forum Langage
    Réponses: 4
    Dernier message: 11/01/2011, 00h11
  2. Comment fermer une fenêtre externe à l'application ?
    Par Matt2094 dans le forum Delphi
    Réponses: 8
    Dernier message: 30/08/2006, 16h52
  3. Fermer une application Windows
    Par telecnop dans le forum Langage
    Réponses: 20
    Dernier message: 28/06/2006, 21h15
  4. [VB6]Fermer une application avec VB
    Par Mylou dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 04/04/2003, 21h32
  5. Fermer une application à l'aide de OnIdle
    Par Thierry Rapp dans le forum Composants VCL
    Réponses: 2
    Dernier message: 29/08/2002, 12h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo