Пример #1
0
        public static List <Win32API.SYSTEM_HANDLE_INFORMATION> GetHandles(Process process = null, string objectTypeName = null, string ojectName = null)
        {
            var handleInfoSize = 0x10000;
            var handlePointer  = Marshal.AllocHGlobal(handleInfoSize);
            var handleSizeInfo = 0;

            while (Win32API.NtQuerySystemInformation(CnstSystemHandleInformation, handlePointer, handleInfoSize, ref handleSizeInfo) == StatusInfoLengthMismatch)
            {
                handleInfoSize = handleSizeInfo;
                Marshal.FreeHGlobal(handlePointer);
                handlePointer = Marshal.AllocHGlobal(handleSizeInfo);
            }

            var handleSizeInfoBuffer = new byte[handleSizeInfo];

            Marshal.Copy(handlePointer, handleSizeInfoBuffer, 0, handleSizeInfo);

            long   handleCount;
            IntPtr handleSizeInfoPointer;

            if (Is64Bits())
            {
                handleCount           = Marshal.ReadInt64(handlePointer);
                handleSizeInfoPointer = new IntPtr(handlePointer.ToInt64() + 8);
            }
            else
            {
                handleCount           = Marshal.ReadInt32(handlePointer);
                handleSizeInfoPointer = new IntPtr(handlePointer.ToInt32() + 4);
            }

            var lstHandles = new List <Win32API.SYSTEM_HANDLE_INFORMATION>();

            for (long index = 0; index < handleCount; index++)
            {
                var systemHandleInformation = new Win32API.SYSTEM_HANDLE_INFORMATION();
                if (Is64Bits())
                {
                    systemHandleInformation = (Win32API.SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(handleSizeInfoPointer, systemHandleInformation.GetType());
                    handleSizeInfoPointer   = new IntPtr(handleSizeInfoPointer.ToInt64() + Marshal.SizeOf(systemHandleInformation) + 8);
                }
                else
                {
                    handleSizeInfoPointer   = new IntPtr(handleSizeInfoPointer.ToInt64() + Marshal.SizeOf(systemHandleInformation));
                    systemHandleInformation = (Win32API.SYSTEM_HANDLE_INFORMATION)Marshal.PtrToStructure(handleSizeInfoPointer, systemHandleInformation.GetType());
                }

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

                if (objectTypeName != null)
                {
                    var strObjectTypeName = GetObjectTypeName(systemHandleInformation, Process.GetProcessById(systemHandleInformation.ProcessID));
                    if (strObjectTypeName != objectTypeName)
                    {
                        continue;
                    }
                }

                if (ojectName != null)
                {
                    var strObjectName = GetObjectName(systemHandleInformation, Process.GetProcessById(systemHandleInformation.ProcessID));
                    if (strObjectName != ojectName)
                    {
                        continue;
                    }
                }

                var strObjectName2 = GetObjectName(systemHandleInformation, Process.GetProcessById(systemHandleInformation.ProcessID));
                if (strObjectName2 != null && (strObjectName2 == @"\Sessions\1\BaseNamedObjects\enb_mutex_lock" || strObjectName2.Length == 31))
                {
                    lstHandles.Add(systemHandleInformation);
                }
            }

            return(lstHandles);
        }