コード例 #1
0
        public static string getObjectTypeName(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;
            var    objObjectType     = new MutexCloseHelper.OBJECT_TYPE_INFORMATION();
            IntPtr ipObjectType      = IntPtr.Zero;
            IntPtr ipObjectName      = IntPtr.Zero;
            string strObjectTypeName = "";
            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);


            ipObjectType = Marshal.AllocHGlobal(objBasic.TypeInformationLength);
            nLength      = objBasic.TypeInformationLength;
            while ((uint)(nReturn = MutexCloseHelper.NtQueryObject(ipHandle, (int)MutexCloseHelper.ObjectInformationClass.ObjectTypeInformation, ipObjectType, nLength, ref nLength)) == MutexCloseHelper.STATUS_INFO_LENGTH_MISMATCH)
            {
                Marshal.FreeHGlobal(ipObjectType);
                ipObjectType = Marshal.AllocHGlobal(nLength);
            }
            //Win32API.CloseHandle(ipHandle);
            objObjectType = (MutexCloseHelper.OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(ipObjectType, objObjectType.GetType());
            if (Is64Bits())
            {
                ipTemp = new IntPtr(Convert.ToInt64(objObjectType.Name.Buffer.ToString(), 10) >> 32);
            }
            else
            {
                ipTemp = objObjectType.Name.Buffer;
            }

            strObjectTypeName = Marshal.PtrToStringUni(ipTemp, objObjectType.Name.Length >> 1);
            Marshal.FreeHGlobal(ipObjectType);
            return(strObjectTypeName);
        }
コード例 #2
0
ファイル: MutexCloseHelper.cs プロジェクト: AiDaTou/DnTool
        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;
        }
コード例 #3
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);
        }