.NET : How to PInvoke UpdateProcThreadAttribute

后端 未结 1 1885
有刺的猬
有刺的猬 2021-01-20 20:27

I am trying to PInvoke UpdateProcThreadAttribute() on Windows 7 but my attempts just keep returning FALSE with a Last Win32 Error of 50.

Functio         


        
相关标签:
1条回答
  • 2021-01-20 21:16

    You have a few problems with your declaration but the one that is giving you the not supported error is the Attribute parameter. A DWORD_PTR is not a pointer but rather a pointer sized unsigned integer so rather than ref uint it should be an IntPtr.

    The declaration I would use is:

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UpdateProcThreadAttribute(
            IntPtr lpAttributeList, uint dwFlags, IntPtr Attribute,
            IntPtr lpValue, IntPtr cbSize, IntPtr lpPreviousValue, 
            IntPtr lpReturnSize);
    

    EDIT:

    I tried to do this as a comment but it doesn't take to code very well.

    For a process handle you need an IntPtr to hold the handle. So you would need something like:

    IntPtr hProcess //previously retrieved.
    IntPtr lpAttributeList //previously allocated using InitializeProcThreadAttributeList and Marshal.AllocHGlobal.
    
    const int PROC_THREAD_ATTRIBUTE_PARENT_PROCESS = 0x00020000;
    IntPtr lpValue = Marshal.AllocHGlobal(IntPtr.Size); 
    Marshal.WriteIntPtr(lpValue, hProcess);
    if(UpdateProcThreadAttribute(lpAttributeList, 0, (IntPtr)PROC_THREAD_ATTRIBUTE_PARENT_PROCESS, lpValue, (IntPtr)IntPtr.Size, IntPtr.Zero, IntPtr.Zero))
    {
        //do something
    }
    
    //Free lpValue only after the lpAttributeList is deleted.
    
    0 讨论(0)
提交回复
热议问题