Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am always getting error at this line (in Runtime, not in Editor):

return Marshal.PtrToStringUni(ptr, object_TYPE_INFORMATION.Name.Length >> 1);

Here is my full code:

        public static string getObjectTypeName(Win32API.SYSTEM_HANDLE_INFORMATION shHandle, Process process)
        IntPtr hSourceProcessHandle = Win32API.OpenProcess(Win32API.ProcessAccessFlags.All, false, process.Id);
        IntPtr zero = IntPtr.Zero;
        Win32API.OBJECT_BASIC_INFORMATION object_BASIC_INFORMATION = default(Win32API.OBJECT_BASIC_INFORMATION);
        IntPtr intPtr = IntPtr.Zero;
        Win32API.OBJECT_TYPE_INFORMATION object_TYPE_INFORMATION = default(Win32API.OBJECT_TYPE_INFORMATION);
        IntPtr intPtr2 = IntPtr.Zero;
        IntPtr zero2 = IntPtr.Zero;
        int num = 0;
        IntPtr ptr = IntPtr.Zero;
        bool flag = !Win32API.DuplicateHandle(hSourceProcessHandle, shHandle.Handle, Win32API.GetCurrentProcess(), out zero, 0u, false, 2u);
        string result;
        if (flag)
            result = null;
            intPtr = Marshal.AllocHGlobal(Marshal.SizeOf<Win32API.OBJECT_BASIC_INFORMATION>(object_BASIC_INFORMATION));
            Win32API.NtQueryObject(zero, 0, intPtr, Marshal.SizeOf<Win32API.OBJECT_BASIC_INFORMATION>(object_BASIC_INFORMATION), ref num);
            object_BASIC_INFORMATION = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(intPtr, object_BASIC_INFORMATION.GetType());
            Marshal.FreeHGlobal(intPtr);
            intPtr2 = Marshal.AllocHGlobal(object_BASIC_INFORMATION.TypeInformationLength);
            num = object_BASIC_INFORMATION.TypeInformationLength;
            while (Win32API.NtQueryObject(zero, 2, intPtr2, num, ref num) == -1073741820)
                Marshal.FreeHGlobal(intPtr2);
                intPtr2 = Marshal.AllocHGlobal(num);
            object_TYPE_INFORMATION = (Win32API.OBJECT_TYPE_INFORMATION)Marshal.PtrToStructure(intPtr2, object_TYPE_INFORMATION.GetType());
            bool flag2 = Win32Processes.Is64Bits();
            if (flag2)
                ptr = new IntPtr(Convert.ToInt64(object_TYPE_INFORMATION.Name.Buffer.ToString(), 10) >> 32);
                ptr = object_TYPE_INFORMATION.Name.Buffer;
            Marshal.FreeHGlobal(intPtr2);
        return Marshal.PtrToStringUni(ptr, object_TYPE_INFORMATION.Name.Length >> 1);

Error:

System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

.NET Framework Version: 4.6.1

What am I doing wrong?

There is a question featuring the exact same code. Both look like decompliled code? Anyway you could look at the anser given there and see if it helps. – Christian.K Dec 16, 2018 at 9:39 Additionally, check github apparently the "original version" of this code is somewhere from there. Also consider looking at the actual docs. – Christian.K Dec 16, 2018 at 9:42 Then check my references to github in the second comment. Make sure you consider the license(s) of the code. Note that decompilers might have errors as well. Make sure you understand what you are trying to achieve or find a library that can do what you actually want to do (I guess it is not just getting NT kernel object names). – Christian.K Dec 16, 2018 at 9:49 Very little one can do here. Mind you, that the root cause might be in some other code, executed before that line (because that is how Access-Violations roll). As always with P/Invoke code, make sure you check every line for proper sizes, calling conventions, memory allocations/deallocations, etc. – Christian.K Dec 16, 2018 at 10:01

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.