コード例 #1
0
        public static IObjectHandle GetHandle(int processId, string objectName, bool exactMatch)
        {
            int    infoLength  = 0x10000;
            int    length      = 0;
            IntPtr _info       = Marshal.AllocHGlobal(infoLength);
            IntPtr _handle     = IntPtr.Zero;
            long   handleCount = 0;

            try
            {
                while ((Win32Api.NtQuerySystemInformation(CNST_SYSTEM_EXTENDED_HANDLE_INFORMATION, _info, infoLength, ref length)) == STATUS_INFO_LENGTH_MISMATCH)
                {
                    infoLength = length;
                    Marshal.FreeHGlobal(_info);
                    _info = Marshal.AllocHGlobal(infoLength);
                }

                if (IS_64)
                {
                    handleCount = Marshal.ReadInt64(_info);
                    _handle     = new IntPtr(_info.ToInt64() + 16);
                }
                else
                {
                    handleCount = Marshal.ReadInt32(_info);
                    _handle     = new IntPtr(_info.ToInt32() + 8);
                }

                Win32Api.SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX        handleInfo;
                List <Win32Api.SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX> handles = new List <Win32Api.SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX>();

                handleInfo = new Win32Api.SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX();
                int  infoSize = Marshal.SizeOf(handleInfo);
                Type infoType = handleInfo.GetType();

                for (long i = 0; i < handleCount; i++)
                {
                    if (IS_64)
                    {
                        handleInfo = (Win32Api.SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX)Marshal.PtrToStructure(_handle, infoType);
                        _handle    = new IntPtr(_handle.ToInt64() + infoSize);
                    }
                    else
                    {
                        handleInfo = (Win32Api.SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX)Marshal.PtrToStructure(_handle, infoType);
                        _handle    = new IntPtr(_handle.ToInt32() + infoSize);
                    }

                    if (processId > 0 && handleInfo.UniqueProcessId.ToUInt32() != processId)
                    {
                        continue;
                    }

                    string name = GetObjectName(handleInfo);
                    if (name == null)
                    {
                        continue;
                    }

                    if (exactMatch)
                    {
                        if (!name.Equals(objectName))
                        {
                            continue;
                        }
                    }
                    else if (!name.Contains(objectName))
                    {
                        continue;
                    }

                    return(new ObjectHandle(handleInfo));
                }
            }
            finally
            {
                Marshal.FreeHGlobal(_info);
            }

            return(null);
        }