Skip to content

Conversation

@rasta-mouse
Copy link

Initial draft for providing wrappers for InitializeProcThreadAttributeList, InitializeProcThreadAttributeList and CreateProcess. DeleteProcThreadAttributeList to follow.

Feedback welcome - particularly interested in your option on issues like:
https://github.com/rasta-mouse/DInvoke/blob/dev/DInvoke/DInvoke/DynamicInvoke/Win32.cs#L117-L120

I've currently abstracted SECURITY_ATTRIBUTES away from the user, as I feel most use cases would not have a need to change them. But should we not do that? Should they be optional overloads?

Changes can be tested with the following:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

using DInvoke.DynamicInvoke;
using Data = DInvoke.Data;

namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var si = new Data.Win32.ProcessThreadsAPI._STARTUPINFOEX();
            si.StartupInfo.cb = (uint)Marshal.SizeOf(si);
            si.StartupInfo.dwFlags = (uint)Data.Win32.ProcessThreadsAPI.STARTF.STARTF_USESHOWWINDOW;

            var lpValue = Marshal.AllocHGlobal(IntPtr.Size);

            var lpSize = IntPtr.Zero;

            var success = Win32.InitializeProcThreadAttributeList(
                IntPtr.Zero,
                2,
                ref lpSize);

            si.lpAttributeList = Marshal.AllocHGlobal(lpSize);

            success = Win32.InitializeProcThreadAttributeList(
                si.lpAttributeList,
                2,
                ref lpSize);

            if (Is64Bit)
            {
                Marshal.WriteIntPtr(lpValue, new IntPtr(0x100000000000));
            }
            else
            {
                Marshal.WriteIntPtr(lpValue, new IntPtr(unchecked((uint)0x100000000000)));
            }

            success = Win32.UpdateProcThreadAttribute(
                si.lpAttributeList,
                (IntPtr)0x20007,
                lpValue);

            var hParent = Process.GetProcessesByName("explorer")[0].Handle;
            lpValue = Marshal.AllocHGlobal(IntPtr.Size);
            Marshal.WriteIntPtr(lpValue, hParent);

            success = Win32.UpdateProcThreadAttribute(
                si.lpAttributeList,
                (IntPtr)0x00020000,
                lpValue);

            success = Win32.CreateProcess(
                null,
                "notepad",
                0x00080000,
                null,
                ref si,
                out Data.Win32.ProcessThreadsAPI._PROCESS_INFORMATION pi);
        }

        static bool Is64Bit
        {
            get
            {
                return IntPtr.Size == 8;
            }
        }
    }
}

@TheWover TheWover added this to the 2.0 milestone Dec 3, 2020
@TheWover TheWover self-assigned this Dec 3, 2020
@TheWover TheWover added the enhancement New feature or request label Dec 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants