C++, How to determine if a Windows Process is running?

前端 未结 13 1164
清酒与你
清酒与你 2020-11-27 15:27

This is concerning Windows XP processes.

I have a process running, let\'s call it Process1. Process1 creates a new process, Process2, and saves its id.

Now,

13条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 16:24

    This is a solution that I've used in the past. Although the example here is in VB.net - I've used this technique with c and c++. It bypasses all the issues with Process IDs & Process handles, and return codes. Windows is very faithful in releasing the mutex no matter how Process2 is terminated. I hope it is helpful to someone...

    **PROCESS1 :-**
    
        Randomize()
        mutexname = "myprocess" & Mid(Format(CDbl(Long.MaxValue) * Rnd(), "00000000000000000000"), 1, 16)
        hnd = CreateMutex(0, False, mutexname)
    
        ' pass this name to Process2
        File.WriteAllText("mutexname.txt", mutexname)
    
        
        
    
        pr = WaitForSingleObject(hnd, 0)
        ReleaseMutex(hnd)
    
        If pr = WAIT_OBJECT_0 Then
    
             
    
        Else
    
             
    
        End If
        ...
    
        CloseHandle(hnd)
        EXIT
    
        **PROCESS2 :-**
    
        mutexname = File.ReadAllText("mutexname.txt")
        hnd = OpenMutex(MUTEX_ALL_ACCESS Or SYNCHRONIZE, True, mutexname)
        ...
    
        ReleaseMutex(hnd)
        CloseHandle(hnd)
        EXIT
    

提交回复
热议问题