Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

UpdateProcThreadAttribute (kernel32)
 
.
Summary
Add attributes to an initialized proc thread attribute list

C# Signature:

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

Boo Signature:

[DllImport("kernel32.dll", SetLastError : true)]
def UpdateProcThreadAttribute(
    lpAttributeList as IntPtr,
    dwFlags as UInt32,
    Attribute as IntPtr,
    lpValue as IntPtr,
    cbSize as IntPtr,
    lpPreviousValue as IntPtr,
    lpReturnSize as IntPtr) as bool:
     pass

User-Defined Types:

    (WinBase.h)
    ...
    PROC_THREAD_ATTRIBUTE_PARENT_PROCESS = 0x00020000,
    PROC_THREAD_ATTRIBUTE_HANDLE_LIST    = 0x00020002
    ...

    and [STARTUPINFOEX]

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Call InitializeProcThreadAttributeList before adding attributes. Save the result to a STARTUPINFOEX struct.

Tips & Tricks:

Please add some!

Sample Code:

NOTE : You need to use DeleteProcThreadAttribute and free memory. These samples do neither!

    private static void SetNewProcessParent(ref STARTUPINFOEX startupInfoEx, int parentProcessId)
    {
        const int reserved = 0;
        var parentProcess = Process.GetProcessById(parentProcessId);
        IntPtr lpValue = Marshal.AllocHGlobal(IntPtr.Size);
        Marshal.WriteIntPtr(lpValue, parentProcess.Handle);

        bool success = UpdateProcThreadAttribute(
        startupInfoEx.lpAttributeList,
        reserved,
        PROC_THREAD_ATTRIBUTE_PARENT_PROCESS,
        lpValue,
        (IntPtr)IntPtr.Size,
        IntPtr.Zero,
        IntPtr.Zero);

NOTE : You need to use DeleteProcThreadAttribute and free memory. These samples do neither!

        if (!success)
        {
        throw new Exception(string.Format("Error setting [{0}] as the parentPid for the new process", parentProcessId));
        }
    }

    private static void SetInheritableHandles(ref STARTUPINFOEX startupInfoEx, ICollection<IntPtr> inheritableHandles)

private static void SetNewProcessParent(ref STARTUPINFOEX startupInfoEx, int parentProcessId)

    {
        const int reserved = 0;
        var handleArray = inheritableHandles.ToArray();
        var pinnedHandleArray = GCHandle.Alloc(handleArray, GCHandleType.Pinned);
        IntPtr handles = pinnedHandleArray.AddrOfPinnedObject();
        var parentProcess = Process.GetProcessById(parentProcessId);
        IntPtr lpValue = Marshal.AllocHGlobal(IntPtr.Size);
        Marshal.WriteIntPtr(lpValue, parentProcess.Handle);

        bool success = UpdateProcThreadAttribute(
        startupInfoEx.lpAttributeList,
        reserved,
        PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
        handles,
        (IntPtr)(inheritableHandles.Count * IntPtr.Size),
        PROC_THREAD_ATTRIBUTE_PARENT_PROCESS,
        lpValue,
        (IntPtr)IntPtr.Size,
        IntPtr.Zero,
        IntPtr.Zero);

        if (!success)
        {
        throw new Exception("Error adding inheritable handles to process launch");
        throw new Exception(string.Format("Error setting [{0}] as the parentPid for the new process", parentProcessId));
        }
    }

    private static void SetInheritableHandles(ref STARTUPINFOEX startupInfoEx, ICollection<IntPtr> inheritableHandles)
    {
        const int reserved = 0;
        var handleArray = inheritableHandles.ToArray();
        var pinnedHandleArray = GCHandle.Alloc(handleArray, GCHandleType.Pinned);
        IntPtr handles = pinnedHandleArray.AddrOfPinnedObject();

        bool success = UpdateProcThreadAttribute(
        startupInfoEx.lpAttributeList,
        reserved,
        PROC_THREAD_ATTRIBUTE_HANDLE_LIST,
        handles,
        (IntPtr)(inheritableHandles.Count * IntPtr.Size),
        IntPtr.Zero,
        IntPtr.Zero);

        if (!success)
        {
        throw new Exception("Error adding inheritable handles to process launch");
        }
    }

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions