C#获取句柄列表,AcessViolationException

问题描述 投票:2回答:2

信息:

  • .Net 4.5

测试:

  • Win7 64位
  • Win10 64位(虚拟盒)

我正在尝试获取外部进程的句柄列表并将其名称作为字符串返回,以便我可以在之后关闭特定的句柄。因此,我使用Win32API编写此函数,它将检查句柄是否是我要关闭的句柄:`

        const int CNST_SYSTEM_HANDLE_INFORMATION = 16;
        const uint STATUS_INFO_LENGTH_MISMATCH = 0xc0000004;

        public static string getObjectTypeName(Win32API.SYSTEM_HANDLE_INFORMATION shHandle, Process process)
        {
            IntPtr m_ipProcessHwnd = Win32API.OpenProcess(Win32API.ProcessAccessFlags.All, false, process.Id);
            IntPtr ipHandle = IntPtr.Zero;
            var objBasic = new Win32API.OBJECT_BASIC_INFORMATION();
            IntPtr ipBasic = IntPtr.Zero;
            var objObjectType = new Win32API.OBJECT_TYPE_INFORMATION();
            IntPtr ipObjectType = IntPtr.Zero;
            IntPtr ipObjectName = IntPtr.Zero;
            string strObjectTypeName = "";
            int nLength = 0;
            int nReturn = 0;
            IntPtr ipTemp = IntPtr.Zero;


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



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

            ipObjectType = Marshal.AllocHGlobal(objBasic.TypeInformationLength);
            nLength = objBasic.TypeInformationLength;
            while ((uint)(nReturn = Win32API.NtQueryObject(
                ipHandle, (int)Win32API.ObjectInformationClass.ObjectTypeInformation, ipObjectType,
                  nLength, ref nLength)) ==
                Win32API.STATUS_INFO_LENGTH_MISMATCH)
            {
                Marshal.FreeHGlobal(ipObjectType);
                ipObjectType = Marshal.AllocHGlobal(nLength);
            }

            objObjectType = (Win32API.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);
            Win32API.CloseHandle(ipHandle);
            return strObjectTypeName;
        }`

但问题是这个代码适用于Win7 64bit,而不是Win10! - >在Win 10中strObjectTypeName = Marshal.PtrToStringUni();抛出一个AcessViolationException(代码中的最后几行)

System.AccessViolationException尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

我在这里遗漏了一些关于如何在win10中访问非托管内存的问题?

c# memory unmanaged handle
2个回答
0
投票

我刚遇到同样的问题。我没有尝试过Win7,但是当你将Win10(x64)上的代码运行为32位(例如设置应用程序的“Prefer 32-bit flag”)时,它应该可以工作。发生异常时,将变量“ipTemp”拖放到Visual Studio的“内存窗口”,如果它只显示问号或错误消息,则表示没有有效指针。据我所知,在这个API使用的64位版本的结构中有(更多)填充字节:OBJECT_TYPE_INFORMATION包含UNICODE_STRING,UNICODE_STRING在64位模式的缓冲区之前有4个填充字节。我的工作方式是这样的:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct UNICODE_STRING
{
    private IntPtr _dummy; // the two ushorts seem to be padded with 4 bytes in 64bit mode only

    /// <summary>
    /// The length, in bytes, of the string stored in Buffer. If the string is null-terminated, Length does not include the trailing null character.
    /// </summary>
    public ushort Length
    {
        get { return (ushort)Marshal.ReadInt16(this, 0); }
    }

    /// <summary>
    /// The length, in bytes, of Buffer.
    /// </summary>
    public ushort MaximumLength
    {
        get { return (ushort)Marshal.ReadInt16(this, 2); }
    }

    public IntPtr Buffer;
}

在我的研究过程中,我发现了很多关于这个主题的问题,基本上有两种样本代码已经在互联网上被复制,我正在考虑创建一个名为WinKernelObjectsDotNet的开源库。

更新:图书馆现在可用here。它支持使用单行代码查找锁定文件或串行端口(COM)的进程。


0
投票

我建议以这种方式更改UNICODE_STRING结构。

    public struct UNICODE_STRING
    {
            public ushort Length;
            public ushort MaximumLength;
            [MarshalAs(UnmanagedType.LPWStr)] public string Buffer;
    }

所以getObjectTypeName方法将是这样的,并将适用于32/64:

    public static string getObjectTypeName(SYSTEM_HANDLE_INFORMATION shHandle, Process process) {
        IntPtr ipProcessHwnd = OpenProcess(ProcessAccessFlags.All, false, process.Id);
        if (!DuplicateHandle(ipProcessHwnd, shHandle.Handle, GetCurrentProcess(), out IntPtr ipHandle, 0, false, DUPLICATE_SAME_ACCESS)) {
            return null;
        }

        OBJECT_BASIC_INFORMATION objBasicInformation = new OBJECT_BASIC_INFORMATION();
        IntPtr ipBasicInformation = Marshal.AllocHGlobal(Marshal.SizeOf(objBasicInformation));
        int iBasicInformationLength = 0;
        NtQueryObject(ipHandle, (int) ObjectInformationClass.ObjectBasicInformation, ipBasicInformation, Marshal.SizeOf(objBasicInformation), ref iBasicInformationLength);
        objBasicInformation = (OBJECT_BASIC_INFORMATION) Marshal.PtrToStructure(ipBasicInformation, typeof(OBJECT_BASIC_INFORMATION));
        Marshal.FreeHGlobal(ipBasicInformation);

        int iObjectTypeInformationLength = objBasicInformation.TypeInformationLength;
        IntPtr ipObjectTypeInformation = Marshal.AllocHGlobal(iObjectTypeInformationLength);
        while (Win32API.STATUS_INFO_LENGTH_MISMATCH == (uint) (NtQueryObject(ipHandle, (int) ObjectInformationClass.ObjectTypeInformation, ipObjectTypeInformation, iObjectTypeInformationLength, ref iObjectTypeInformationLength))) {
            Marshal.FreeHGlobal(ipObjectTypeInformation);
            ipObjectTypeInformation = Marshal.AllocHGlobal(iObjectTypeInformationLength);
        }
        CloseHandle(ipHandle);

        OBJECT_TYPE_INFORMATION objObjectType = (OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(ipObjectTypeInformation, typeof(OBJECT_TYPE_INFORMATION));
        Marshal.FreeHGlobal(ipObjectTypeInformation);

        return objObjectType.Name.Buffer;
    }
© www.soinside.com 2019 - 2024. All rights reserved.