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

Windows Discussion :

Trouver un autre processus à partir du nom de l'executable.


Sujet :

Windows

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Septembre 2002
    Messages
    20
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2002
    Messages : 20
    Points : 17
    Points
    17
    Par défaut Trouver un autre processus à partir du nom de l'executable.
    salut,

    pour une fonctionnalité de mise a jour, je souhaite que mon programme trouve l'autre instance de ce meme programme(version anterieur) et detruise cette meme instance. Je n'ai que son nom.

    Comment je peux retrouver deja l'identificateur de mon application à partir du nom de l'executable, et comment je peux tuer le processus une fois le pid trouvé ?

    il faut savoir que mon application cree un mutex avec CreateMutex a son lancement pour eviter qu'une autre instance s'execute. Y'atil un moyen de recuperer le pid du programme ayant crée le mutex ? Ca serait la solution la plus propre je trouve.

    je vais continuer mes recherches en attendant.

    merci :)

  2. #2
    Rédacteur


    Profil pro
    Inscrit en
    Janvier 2003
    Messages
    7 171
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2003
    Messages : 7 171
    Points : 15 060
    Points
    15 060
    Billets dans le blog
    1
    Par défaut
    Salut,
    regarde ici, c'est du code Delphi mais cela peut être une source d'inspiration.

  3. #3
    Membre à l'essai
    Profil pro
    Inscrit en
    Septembre 2002
    Messages
    20
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2002
    Messages : 20
    Points : 17
    Points
    17
    Par défaut
    Merci.

    Alors j'ai trouvé un code pour trouver une instance par son nom puis la tuer.
    Je l'ecris ici au cas ou ca pourrait interesser quelqu'un..
    Le code ne marchait pas avec devcpp alors je lai un peu change.
    L'original est la : http://www.codeproject.com/threads/killprocess.asp

    mon adaptation est la :
    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
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    
    #ifndef CKILLPROCESSUNI_CPP
    #define CKILLPROCESSUNI_CPP
    
    #include <windows.h>
    #include <tlhelp32.h>
    
    //
    // Some definitions from NTDDK and other sources
    //
    
    typedef LONG    NTSTATUS;
    typedef LONG    KPRIORITY;
    
    #define NT_SUCCESS&#40;Status&#41; &#40;&#40;NTSTATUS&#41;&#40;Status&#41; >= 0&#41;
    
    #define STATUS_INFO_LENGTH_MISMATCH &#40;&#40;NTSTATUS&#41;0xC0000004L&#41;
    
    #define SystemProcessesAndThreadsInformation    5
    
    typedef struct _CLIENT_ID &#123;
        DWORD        UniqueProcess;
        DWORD        UniqueThread;
    &#125; CLIENT_ID;
    
    typedef struct _UNICODE_STRING &#123;
        USHORT        Length;
        USHORT        MaximumLength;
        PWSTR        Buffer;
    &#125; UNICODE_STRING;
    
    typedef struct _VM_COUNTERS &#123;
        SIZE_T        PeakVirtualSize;
        SIZE_T        VirtualSize;
        ULONG        PageFaultCount;
        SIZE_T        PeakWorkingSetSize;
        SIZE_T        WorkingSetSize;
        SIZE_T        QuotaPeakPagedPoolUsage;
        SIZE_T        QuotaPagedPoolUsage;
        SIZE_T        QuotaPeakNonPagedPoolUsage;
        SIZE_T        QuotaNonPagedPoolUsage;
        SIZE_T        PagefileUsage;
        SIZE_T        PeakPagefileUsage;
    &#125; VM_COUNTERS;
    
    typedef struct _SYSTEM_THREADS &#123;
        LARGE_INTEGER   KernelTime;
        LARGE_INTEGER   UserTime;
        LARGE_INTEGER   CreateTime;
        ULONG            WaitTime;
        PVOID            StartAddress;
        CLIENT_ID        ClientId;
        KPRIORITY        Priority;
        KPRIORITY        BasePriority;
        ULONG            ContextSwitchCount;
        LONG            State;
        LONG            WaitReason;
    &#125; SYSTEM_THREADS, * PSYSTEM_THREADS;
    
    // Note that the size of the SYSTEM_PROCESSES structure is
    // different on NT 4 and Win2K, but we don't care about it,
    // since we don't access neither IoCounters member nor
    //Threads array
    
    typedef struct _SYSTEM_PROCESSES &#123;
        ULONG            NextEntryDelta;
        ULONG            ThreadCount;
        ULONG            Reserved1&#91;6&#93;;
        LARGE_INTEGER   CreateTime;
        LARGE_INTEGER   UserTime;
        LARGE_INTEGER   KernelTime;
        UNICODE_STRING  ProcessName;
        KPRIORITY        BasePriority;
        ULONG            ProcessId;
        ULONG            InheritedFromProcessId;
        ULONG            HandleCount;
        ULONG            Reserved2&#91;2&#93;;
        VM_COUNTERS        VmCounters;
    #if _WIN32_WINNT >= 0x500
        IO_COUNTERS        IoCounters;
    #endif
        SYSTEM_THREADS  Threads&#91;1&#93;;
    &#125; SYSTEM_PROCESSES, * PSYSTEM_PROCESSES;
    
    
    class CKillProcess
    &#123;
    private&#58;
        //Functions loaded from Kernel32
        typedef HANDLE &#40;WINAPI *PFCreateToolhelp32Snapshot&#41;&#40;
            DWORD dwFlags,
            DWORD th32ProcessID
            &#41;;
    
        typedef BOOL &#40;WINAPI *PFProcess32First&#41;&#40;
            HANDLE hSnapshot,
            LPPROCESSENTRY32 lppe
            &#41;;
    
        typedef BOOL &#40;WINAPI *PFProcess32Next&#41;&#40;
            HANDLE hSnapshot,
            LPPROCESSENTRY32 lppe
            &#41;;
    
        // Native NT API Definitions
        typedef NTSTATUS &#40;WINAPI * PFZwQuerySystemInformation&#41;
            &#40;UINT, PVOID, ULONG, PULONG&#41;;
        typedef HANDLE &#40;WINAPI* PFGetProcessHeap&#41;&#40;VOID&#41;;
        typedef LPVOID &#40;WINAPI* PFHeapAlloc&#41;
            &#40;HANDLE,DWORD,SIZE_T&#41;;
        typedef BOOL &#40;WINAPI* PFHeapFree&#41;&#40;HANDLE,DWORD,LPVOID&#41;;
    public&#58;
        CKillProcess&#40;&#41; &#58; FCreateToolhelp32Snapshot&#40;NULL&#41;,
            FProcess32First&#40;NULL&#41;, FProcess32Next&#40;NULL&#41;,
            m_hKernelLib&#40;NULL&#41;,
            m_hNTLib&#40;NULL&#41;
        &#123;
            m_hKernelLib = &#58;&#58;LoadLibraryA&#40;"Kernel32"&#41;;
            if &#40;m_hKernelLib&#41;
            &#123;
                // Find ToolHelp functions
                FCreateToolhelp32Snapshot =
                    &#40;PFCreateToolhelp32Snapshot&#41;
                    &#58;&#58;GetProcAddress&#40;m_hKernelLib,
                    __TEXT&#40;"CreateToolhelp32Snapshot"&#41;&#41;;
                FProcess32First = &#40;PFProcess32First&#41;
                    &#58;&#58;GetProcAddress&#40;m_hKernelLib,
                    __TEXT&#40;"Process32First"&#41;&#41;;
                FProcess32Next = &#40;PFProcess32Next&#41;
                    &#58;&#58;GetProcAddress&#40;m_hKernelLib,
                    __TEXT&#40;"Process32Next"&#41;&#41;;
            &#125;
            if&#40;!FCreateToolhelp32Snapshot ||
                !FProcess32First || !FProcess32Next&#41;
            &#123; // i.e. we couldn't find the ToolHelp functions,
                //so we must be on NT4. Let's load the
                // undocumented one instead.
                if&#40;!m_hKernelLib&#41;
                    return; // can't do anything at all without
                //the kernel.
    
                m_hNTLib = &#58;&#58;LoadLibraryA&#40;"ntdll.dll"&#41;;
                if&#40;m_hNTLib&#41;
                &#123;
                    FQuerySysInfo =
                        &#40;PFZwQuerySystemInformation&#41;
                        &#58;&#58;GetProcAddress&#40;m_hNTLib,
                        __TEXT&#40;"ZwQuerySystemInformation"&#41;&#41;;
                    // load some support funcs from the kernel
                    FGetProcessHeap = &#40;PFGetProcessHeap&#41;
                        &#58;&#58;GetProcAddress&#40;m_hKernelLib,
                        __TEXT&#40;"GetProcessHeap"&#41;&#41;;
                    FHeapAlloc = &#40;PFHeapAlloc&#41;
                        &#58;&#58;GetProcAddress&#40;m_hKernelLib,
                        __TEXT&#40;"HeapAlloc"&#41;&#41;;
                    FHeapFree = &#40;PFHeapFree&#41;
                        &#58;&#58;GetProcAddress&#40;m_hKernelLib,
                        __TEXT&#40;"HeapFree"&#41;&#41;;
                &#125;
            &#125;
        &#125;
        ~CKillProcess&#40;&#41;
        &#123;
            if&#40;m_hKernelLib&#41;
                FreeLibrary&#40;m_hKernelLib&#41;;
            if&#40;m_hNTLib&#41;
                FreeLibrary&#40;m_hNTLib&#41;;
        &#125;
        bool KillProcess&#40;IN const char* pstrProcessName&#41;
        &#123;
            DWORD dwId;
            HANDLE hProcess = FindProcess&#40;pstrProcessName,
                dwId&#41;;
            BOOL bResult;
            if&#40;!hProcess&#41;
                return false;
    
            // TerminateAppEnum&#40;&#41; posts WM_CLOSE to all windows whose PID
            // matches your process's.
            &#58;&#58;EnumWindows&#40;&#40;WNDENUMPROC&#41;
                CKillProcess&#58;&#58;TerminateAppEnum,
                &#40;LPARAM&#41; dwId&#41;;
            // Wait on the handle. If it signals, great.
            //If it times out, then you kill it.
            if&#40;WaitForSingleObject&#40;hProcess, 5000&#41;
                !=WAIT_OBJECT_0&#41;
                bResult = TerminateProcess&#40;hProcess,0&#41;;
            else
                bResult = TRUE;
            CloseHandle&#40;hProcess&#41;;
            return bResult == TRUE;
        &#125;
    private&#58;
        HANDLE FindProcess&#40;IN const char* pstrProcessName,
            OUT DWORD& dwId&#41;
        &#123;
            if&#40;!m_hKernelLib&#41;
                return NULL;
    
            if&#40;FCreateToolhelp32Snapshot && FProcess32First &&
                FProcess32Next&#41; // use toolhelpapi
                return THFindProcess&#40;pstrProcessName, dwId&#41;;
            if&#40;FQuerySysInfo && FHeapAlloc &&
                FGetProcessHeap && FHeapFree&#41; // use NT api
                return NTFindProcess&#40;pstrProcessName, dwId&#41;;
            // neither one got loaded. Strange.
            return NULL;
        &#125;
        HANDLE THFindProcess&#40;IN const char* pstrProcessName,
            OUT DWORD& dwId&#41;
        &#123;
            HANDLE            hSnapShot=NULL;
            HANDLE            hResult = NULL;
            PROCESSENTRY32    processInfo;
            char*            pstrExeName;
    
            bool bFirst = true;
            &#58;&#58;ZeroMemory&#40;&processInfo, sizeof&#40;PROCESSENTRY32&#41;&#41;;
            processInfo.dwSize = sizeof&#40;PROCESSENTRY32&#41;;
            hSnapShot = FCreateToolhelp32Snapshot&#40;
                TH32CS_SNAPPROCESS, 0&#41;;
            if&#40;hSnapShot == INVALID_HANDLE_VALUE&#41;
                return NULL;
    
            // ok now let's iterate with Process32Next until we
            // match up the name of our process
            while&#40;&#40;bFirst ? FProcess32First&#40;hSnapShot,
                &processInfo&#41; &#58; FProcess32Next&#40;hSnapShot,
                &processInfo&#41;&#41;&#41;
            &#123;
                bFirst = false;
                // we need to check for path... and extract
                // just the exe name
                pstrExeName = strrchr&#40;processInfo.szExeFile,
                    '\\'&#41;;
                if&#40;!pstrExeName&#41;
                    pstrExeName = processInfo.szExeFile;
                else
                    pstrExeName++; // skip the \
                // ok now compare against our process name
                if&#40;stricmp&#40;pstrExeName, pstrProcessName&#41; == 0&#41;
                    // wee weee we found it
                &#123;
                    // let's get a HANDLE on it
                    hResult=OpenProcess&#40;
                        SYNCHRONIZE|PROCESS_TERMINATE, TRUE,
                        processInfo.th32ProcessID&#41;;
                    dwId = processInfo.th32ProcessID;
                    break;
                &#125;
            &#125; // while&#40;Process32Next&#40;hSnapShot, &processInfo&#41;&#123;
            if&#40;hSnapShot&#41;
                CloseHandle&#40;hSnapShot&#41;;
            return hResult;
        &#125;
        HANDLE NTFindProcess&#40;IN const char* pstrProcessName,
            OUT DWORD& dwId&#41;
        &#123;
            HANDLE hHeap = FGetProcessHeap&#40;&#41;;
            NTSTATUS Status;
            ULONG cbBuffer = 0x8000;
            PVOID pBuffer = NULL;
            HANDLE hResult = NULL;
            // it is difficult to say a priory which size of
            // the buffer will be enough to retrieve all
            // information, so we startwith 32K buffer and
            // increase its size until we get the
            // information successfully
            do
            &#123;
                pBuffer = HeapAlloc&#40;hHeap, 0, cbBuffer&#41;;
                if &#40;pBuffer == NULL&#41; &#123;
                  SetLastError&#40;ERROR_NOT_ENOUGH_MEMORY&#41;;
                    return NULL;
                  &#125;
    
                Status = FQuerySysInfo&#40;
                    SystemProcessesAndThreadsInformation,
                    pBuffer, cbBuffer, NULL&#41;;
    
                if &#40;Status == STATUS_INFO_LENGTH_MISMATCH&#41;
                &#123;
                    HeapFree&#40;hHeap, 0, pBuffer&#41;;
                    cbBuffer *= 2;
                &#125;
                else if &#40;!NT_SUCCESS&#40;Status&#41;&#41;
                &#123;
                    HeapFree&#40;hHeap, 0, pBuffer&#41;;
                    SetLastError&#40;Status&#41;;
                    return NULL;
                &#125;
            &#125;
            while &#40;Status == STATUS_INFO_LENGTH_MISMATCH&#41;;
    
            PSYSTEM_PROCESSES pProcesses =
                &#40;PSYSTEM_PROCESSES&#41;pBuffer;
    
            for &#40;;;&#41;
            &#123;
                PCWSTR pszProcessName =
                    pProcesses->ProcessName.Buffer;
                if &#40;pszProcessName == NULL&#41;
                    pszProcessName = L"Idle";
    
                CHAR szProcessName&#91;MAX_PATH&#93;;
                WideCharToMultiByte&#40;CP_ACP, 0, pszProcessName,
                    -1,szProcessName, MAX_PATH, NULL, NULL&#41;;
    
                if&#40;stricmp&#40;szProcessName, pstrProcessName&#41;
                    == 0&#41; // found it
                &#123;
                    hResult=OpenProcess&#40;
                        SYNCHRONIZE|PROCESS_TERMINATE, TRUE,
                        pProcesses->ProcessId&#41;;
                    dwId = pProcesses->ProcessId;
                    break;
                &#125;
    
                if &#40;pProcesses->NextEntryDelta == 0&#41;
                    break;
    
                // find the address of the next process
                // structure
                pProcesses = &#40;PSYSTEM_PROCESSES&#41;&#40;
                    &#40;&#40;LPBYTE&#41;pProcesses&#41;
                    + pProcesses->NextEntryDelta&#41;;
            &#125;
    
            HeapFree&#40;hHeap, 0, pBuffer&#41;;
            return hResult;
        &#125;
        // callback function for window enumeration
        static BOOL CALLBACK TerminateAppEnum&#40; HWND hwnd,
            LPARAM lParam &#41;
        &#123;
            DWORD dwID ;
    
            GetWindowThreadProcessId&#40;hwnd, &dwID&#41; ;
    
            if&#40;dwID == &#40;DWORD&#41;lParam&#41;
            &#123;
                PostMessage&#40;hwnd, WM_CLOSE, 0, 0&#41; ;
            &#125;
    
            return TRUE ;
        &#125;
        HMODULE            m_hNTLib;
        HMODULE            m_hKernelLib;
        // ToolHelp related functions
        PFCreateToolhelp32Snapshot    FCreateToolhelp32Snapshot;
        PFProcess32First            FProcess32First;
        PFProcess32Next                FProcess32Next;
        // native NT api functions
        PFZwQuerySystemInformation    FQuerySysInfo;
        PFGetProcessHeap            FGetProcessHeap;
        PFHeapAlloc                    FHeapAlloc;
        PFHeapFree                    FHeapFree;
    &#125;;
    
    #endif

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

Discussions similaires

  1. Réponses: 2
    Dernier message: 03/01/2012, 17h28
  2. Réponses: 2
    Dernier message: 15/04/2010, 08h56
  3. Réponses: 2
    Dernier message: 10/03/2010, 21h56
  4. Tuer un processus à partir de son nom
    Par 1tox dans le forum Linux
    Réponses: 4
    Dernier message: 14/02/2007, 17h48

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