Example #1
0
        public static List <MutexCloseHelper.SYSTEM_HANDLE_INFORMATION> GetHandles(Process process = null, string IN_strObjectTypeName = null, string IN_strObjectName = null)
        {
            uint   nStatus;
            int    nHandleInfoSize = 0x10000;
            IntPtr ipHandlePointer = Marshal.AllocHGlobal(nHandleInfoSize);
            int    nLength         = 0;
            IntPtr ipHandle        = IntPtr.Zero;

            while ((nStatus = MutexCloseHelper.NtQuerySystemInformation(CNST_SYSTEM_HANDLE_INFORMATION, ipHandlePointer, nHandleInfoSize, ref nLength)) == STATUS_INFO_LENGTH_MISMATCH)
            {
                nHandleInfoSize = nLength;
                Marshal.FreeHGlobal(ipHandlePointer);
                ipHandlePointer = Marshal.AllocHGlobal(nLength);
            }

            byte[] baTemp = new byte[nLength];
            Marshal.Copy(ipHandlePointer, baTemp, 0, nLength);

            long lHandleCount = 0;

            if (Is64Bits())
            {
                lHandleCount = Marshal.ReadInt64(ipHandlePointer);
                ipHandle     = new IntPtr(ipHandlePointer.ToInt64() + 8);
            }
            else
            {
                lHandleCount = Marshal.ReadInt32(ipHandlePointer);
                ipHandle     = new IntPtr(ipHandlePointer.ToInt32() + 4);
            }

            MutexCloseHelper.SYSTEM_HANDLE_INFORMATION        shHandle;
            List <MutexCloseHelper.SYSTEM_HANDLE_INFORMATION> lstHandles = new List <MutexCloseHelper.SYSTEM_HANDLE_INFORMATION>();

            IntPtr newHandle = IntPtr.Zero;

            for (long lIndex = 0; lIndex < lHandleCount; lIndex++)
            {
                shHandle = new MutexCloseHelper.SYSTEM_HANDLE_INFORMATION();
                if (Is64Bits())
                {
                    shHandle = (MutexCloseHelper.SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(ipHandle, shHandle.GetType());
                    ipHandle = new IntPtr(ipHandle.ToInt64() + Marshal.SizeOf(shHandle) + 8);
                }
                else
                {
                    ipHandle = new IntPtr(ipHandle.ToInt64() + Marshal.SizeOf(shHandle));
                    shHandle = (MutexCloseHelper.SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(ipHandle, shHandle.GetType());
                }



                if (process != null)
                {
                    if (shHandle.ProcessID != process.Id)
                    {
                        continue;
                    }
                }


                string strObjectTypeName = "";
                if (IN_strObjectTypeName != null)
                {
                    strObjectTypeName = getObjectTypeName(shHandle, Process.GetProcessById(shHandle.ProcessID), out newHandle);
                    try
                    {
                        if (strObjectTypeName != IN_strObjectTypeName)
                        {
                            continue;
                        }
                        else
                        {
                            MutexCloseHelper.CloseHandle(newHandle);
                        }
                    }
                    catch
                    {
                    }
                }

                string strObjectName = "";
                if (IN_strObjectName != null)
                {
                    strObjectName = getObjectName(shHandle, Process.GetProcessById(shHandle.ProcessID), out newHandle);
                    try
                    {
                        MutexCloseHelper.CloseHandle(newHandle);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }
                    if (strObjectName != IN_strObjectName)
                    {
                        continue;
                    }
                }

                //string strObjectTypeName2 = getObjectTypeName(shHandle, Process.GetProcessById(shHandle.ProcessID), out newHandle);
                //try
                //{
                //    Win32API.CloseHandle(newHandle);
                //}
                //catch
                //{

                //}
                //string strObjectName2 = getObjectName(shHandle, Process.GetProcessById(shHandle.ProcessID), out newHandle);
                //try
                //{
                //    Win32API.CloseHandle(newHandle);
                //}
                //catch
                //{

                //}
                //Console.WriteLine("{0}   {1}   {2}", shHandle.ProcessID, strObjectTypeName2, strObjectName2);


                lstHandles.Add(shHandle);
            }
            return(lstHandles);
        }
Example #2
0
        public static string getObjectName(MutexCloseHelper.SYSTEM_HANDLE_INFORMATION shHandle, Process process, out IntPtr ipHandle)
        {
            IntPtr m_ipProcessHwnd = MutexCloseHelper.OpenProcess(Win32API.ProcessAccessFlags.All, false, process.Id);

            ipHandle = IntPtr.Zero;
            var    objBasic      = new MutexCloseHelper.OBJECT_BASIC_INFORMATION();
            IntPtr ipBasic       = IntPtr.Zero;
            IntPtr ipObjectType  = IntPtr.Zero;
            var    objObjectName = new MutexCloseHelper.OBJECT_NAME_INFORMATION();
            IntPtr ipObjectName  = IntPtr.Zero;
            string strObjectName = "";
            int    nLength       = 0;
            int    nReturn       = 0;
            IntPtr ipTemp        = IntPtr.Zero;

            if (!MutexCloseHelper.DuplicateHandle(m_ipProcessHwnd, shHandle.Handle, MutexCloseHelper.GetCurrentProcess(), out ipHandle, 0, false, MutexCloseHelper.DUPLICATE_SAME_ACCESS))
            {
                return(null);
            }

            ipBasic = Marshal.AllocHGlobal(Marshal.SizeOf(objBasic));
            MutexCloseHelper.NtQueryObject(ipHandle, (int)MutexCloseHelper.ObjectInformationClass.ObjectBasicInformation, ipBasic, Marshal.SizeOf(objBasic), ref nLength);
            objBasic = (MutexCloseHelper.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(ipBasic, objBasic.GetType());
            Marshal.FreeHGlobal(ipBasic);


            nLength = objBasic.NameInformationLength;

            ipObjectName = Marshal.AllocHGlobal(nLength);
            while ((uint)(nReturn = MutexCloseHelper.NtQueryObject(ipHandle, (int)MutexCloseHelper.ObjectInformationClass.ObjectNameInformation, ipObjectName, nLength, ref nLength)) == MutexCloseHelper.STATUS_INFO_LENGTH_MISMATCH)
            {
                Marshal.FreeHGlobal(ipObjectName);
                ipObjectName = Marshal.AllocHGlobal(nLength);
            }

            //Win32API.CloseHandle(ipHandle);
            objObjectName = (MutexCloseHelper.OBJECT_NAME_INFORMATION)Marshal.PtrToStructure(ipObjectName, objObjectName.GetType());

            if (Is64Bits())
            {
                ipTemp = new IntPtr(Convert.ToInt64(objObjectName.Name.Buffer.ToString(), 10) >> 32);
            }
            else
            {
                ipTemp = objObjectName.Name.Buffer;
            }

            if (ipTemp != IntPtr.Zero)
            {
                byte[] baTemp2 = new byte[nLength];
                try
                {
                    Marshal.Copy(ipTemp, baTemp2, 0, nLength);

                    strObjectName = Marshal.PtrToStringUni(Is64Bits() ? new IntPtr(ipTemp.ToInt64()) : new IntPtr(ipTemp.ToInt32()));
                    return(strObjectName);
                }
                catch (AccessViolationException)
                {
                    return(null);
                }
                finally
                {
                    Marshal.FreeHGlobal(ipObjectName);
                    MutexCloseHelper.CloseHandle(ipHandle);
                }
            }
            return(null);
        }