Exemplo n.º 1
0
        public static bool MutexExists(Process process, string mutexType, string mutexName, bool partial)
        {
            Log(string.Format("Checking if mutex '{0}' of type '{1}' exists in process '{2} (pid {3})'", mutexName, mutexType, process.MainWindowTitle, process.Id));
            // 4 tries
            for (int i = 0; i < 4; i++)
            {
                try
                {
                    var handles = Win32Processes.GetHandles(process, mutexType, "", mutexName);

                    if (partial)
                    {
                        bool foundHandle = false;

                        foreach (var handle in handles)
                        {
                            string strObjectName = Win32Processes.getObjectName(handle, Process.GetProcessById(handle.ProcessID));
                            if (!string.IsNullOrWhiteSpace(strObjectName) && strObjectName.Contains(mutexName))
                            {
                                Log(string.Format("Found mutex that contains search criteria. Located at {0}", strObjectName));
                                if (!foundHandle)
                                {
                                    foundHandle = true;
                                }
                            }
                        }

                        return(foundHandle);
                    }
                    else
                    {
                        if (handles.Count > 0)
                        {
                            Log("Found the following mutexes:");
                            foreach (var handle in handles)
                            {
                                string strObjectName = Win32Processes.getObjectName(handle, Process.GetProcessById(handle.ProcessID));
                                Log(strObjectName);
                            }

                            return(true);
                        }
                    }
                }
                catch (IndexOutOfRangeException)
                {
                    Log(string.Format("The process name '{0}' is not currently running", process.MainWindowTitle));
                }
                catch (ArgumentException)
                {
                    Log(string.Format("The mutex '{0}' was not found in the process '{1}'", mutexName, process.MainWindowTitle));
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        public static bool KillMutex(Process process, string mutexType, string mutexName, bool partial)
        {
            Log(string.Format("Attempting to kill mutex"));
            // 4 tries
            for (int i = 1; i < 4; i++)
            {
                Log("Attempt #" + i);
                Console.WriteLine("Loop " + i);

                if (partial)
                {
                    var handles = Win32Processes.GetHandles(process, mutexType);
                    foreach (var handle in handles)
                    {
                        string strObjectName = Win32Processes.getObjectName(handle, Process.GetProcessById(handle.ProcessID));

                        if (!string.IsNullOrWhiteSpace(strObjectName) && strObjectName.Contains(mutexName))
                        {
                            Log(string.Format("Killing mutex located at '{0}'", strObjectName));
                            IntPtr ipHandle = IntPtr.Zero;
                            if (!Win32API.DuplicateHandle(Process.GetProcessById(handle.ProcessID).Handle, handle.Handle, Win32API.GetCurrentProcess(), out ipHandle, 0, false, Win32API.DUPLICATE_CLOSE_SOURCE))
                            {
                                Log(string.Format("DuplicateHandle() failed, error = {0}", Marshal.GetLastWin32Error()));
                                Console.WriteLine("DuplicateHandle() failed, error = {0}", Marshal.GetLastWin32Error());
                            }
                            else
                            {
                                Log("Mutex was killed successfully");
                                return(true);
                            }
                        }
                        //Log("----------------END-----------------");
                    }
                    return(true);
                }
                else
                {
                    var handles = Win32Processes.GetHandles(process, mutexType, "", mutexName);

                    if (handles.Count == 0)
                    {
                        Log(string.Format("{0} not found in process handles", mutexName));
                        continue;
                    }

                    foreach (var handle in handles)
                    {
                        string strObjectName = Win32Processes.getObjectName(handle, Process.GetProcessById(handle.ProcessID));
                        Log(string.Format("Killing mutex {0}", strObjectName));
                        if (!string.IsNullOrWhiteSpace(strObjectName) && strObjectName.EndsWith(mutexName))
                        {
                            IntPtr ipHandle = IntPtr.Zero;
                            if (!Win32API.DuplicateHandle(Process.GetProcessById(handle.ProcessID).Handle, handle.Handle, Win32API.GetCurrentProcess(), out ipHandle, 0, false, Win32API.DUPLICATE_CLOSE_SOURCE))
                            {
                                Console.WriteLine("DuplicateHandle() failed, error = {0}", Marshal.GetLastWin32Error());
                            }
                            else
                            {
                                Log("Mutex was killed successfully");
                                return(true);
                            }
                        }
                    }
                }
            }
            Log("Mutex was not killed");
            //Log("----------------END-----------------");
            return(false);
        }