diff --git a/DInvoke/DInvoke/DynamicInvoke/Win32.cs b/DInvoke/DInvoke/DynamicInvoke/Win32.cs index 2211c75..b096d71 100644 --- a/DInvoke/DInvoke/DynamicInvoke/Win32.cs +++ b/DInvoke/DInvoke/DynamicInvoke/Win32.cs @@ -77,6 +77,74 @@ public static bool IsWow64Process(IntPtr hProcess, ref bool lpSystemInfo) return retVal; } + public static bool InitializeProcThreadAttributeList(IntPtr lpAttributeList, int dwAttributeCount, ref IntPtr lpSize) + { + object[] funcargs = + { + lpAttributeList, + dwAttributeCount, + 0, + lpSize + }; + + bool retVal = (bool)Generic.DynamicAPIInvoke(@"kernel32.dll", @"InitializeProcThreadAttributeList", typeof(Delegates.InitializeProcThreadAttributeList), ref funcargs); + + lpSize = (IntPtr)funcargs[3]; + return retVal; + } + + public static bool UpdateProcThreadAttribute(IntPtr lpAttributeList, IntPtr Attribute, IntPtr lpValue) + { + object[] funcargs = + { + lpAttributeList, + (uint)0, + Attribute, + lpValue, + (IntPtr)IntPtr.Size, + IntPtr.Zero, + IntPtr.Zero + }; + + bool retVal = (bool)Generic.DynamicAPIInvoke("kernel32.dll", "UpdateProcThreadAttribute", typeof(Delegates.UpdateProcThreadAttribute), ref funcargs); + + return retVal; + } + + public static bool CreateProcess(string lpApplicationName, string lpCommandLine, uint dwCreationFlags, string lpCurrentDirectory, + ref Data.Win32.ProcessThreadsAPI._STARTUPINFOEX lpStartupInfo, out Data.Win32.ProcessThreadsAPI._PROCESS_INFORMATION lpProcessInformation) + { + Data.Win32.WinBase._SECURITY_ATTRIBUTES lpProcessAttributes = new Data.Win32.WinBase._SECURITY_ATTRIBUTES(); + Data.Win32.WinBase._SECURITY_ATTRIBUTES lpThreadAttributes = new Data.Win32.WinBase._SECURITY_ATTRIBUTES(); + lpProcessAttributes.nLength = (uint)Marshal.SizeOf(lpProcessAttributes); + lpThreadAttributes.nLength = (uint)Marshal.SizeOf(lpThreadAttributes); + + object[] funcargs = + { + lpApplicationName, + lpCommandLine, + lpProcessAttributes, + lpThreadAttributes, + false, + dwCreationFlags, + IntPtr.Zero, + lpCurrentDirectory, + lpStartupInfo, + null + }; + + bool retVal = (bool)Generic.DynamicAPIInvoke( + "kernel32.dll", + "CreateProcessA", + typeof(Delegates.CreateProcess), + ref funcargs, + true); + + lpProcessInformation = (Data.Win32.ProcessThreadsAPI._PROCESS_INFORMATION)funcargs[9]; + + return retVal; + } + public static class Delegates { [UnmanagedFunctionPointer(CallingConvention.StdCall)] @@ -99,6 +167,36 @@ UInt32 dwProcessId public delegate bool IsWow64Process( IntPtr hProcess, ref bool lpSystemInfo ); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate bool InitializeProcThreadAttributeList( + IntPtr lpAttributeList, + int dwAttributeCount, + int dwFlags, + ref IntPtr lpSize); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate bool UpdateProcThreadAttribute( + IntPtr lpAttributeList, + uint dwFlags, + IntPtr Attribute, + IntPtr lpValue, + IntPtr cbSize, + IntPtr lpPreviousValue, + IntPtr lpReturnSize); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate bool CreateProcess( + string lpApplicationName, + string lpCommandLine, + ref Data.Win32.WinBase._SECURITY_ATTRIBUTES lpProcessAttributes, + ref Data.Win32.WinBase._SECURITY_ATTRIBUTES lpThreadAttributes, + bool bInheritHandles, + uint dwCreationFlags, + IntPtr lpEnvironment, + string lpCurrentDirectory, + ref Data.Win32.ProcessThreadsAPI._STARTUPINFOEX lpStartupInfo, + out Data.Win32.ProcessThreadsAPI._PROCESS_INFORMATION lpProcessInformation); } } -} +} \ No newline at end of file diff --git a/DInvoke/DInvoke/SharedData/Win32.cs b/DInvoke/DInvoke/SharedData/Win32.cs index 0ba2392..c5934b8 100644 --- a/DInvoke/DInvoke/SharedData/Win32.cs +++ b/DInvoke/DInvoke/SharedData/Win32.cs @@ -542,9 +542,9 @@ public struct _SYSTEM_INFO [StructLayout(LayoutKind.Sequential)] public struct _SECURITY_ATTRIBUTES { - UInt32 nLength; - IntPtr lpSecurityDescriptor; - Boolean bInheritHandle; + public UInt32 nLength; + public IntPtr lpSecurityDescriptor; + public Boolean bInheritHandle; }; } @@ -827,7 +827,7 @@ public enum ACCESS_MASK : uint public class ProcessThreadsAPI { [Flags] - internal enum STARTF : uint + public enum STARTF : uint { STARTF_USESHOWWINDOW = 0x00000001, STARTF_USESIZE = 0x00000002, @@ -868,8 +868,8 @@ public struct _STARTUPINFO [StructLayout(LayoutKind.Sequential)] public struct _STARTUPINFOEX { - _STARTUPINFO StartupInfo; - // PPROC_THREAD_ATTRIBUTE_LIST lpAttributeList; + public _STARTUPINFO StartupInfo; + public IntPtr lpAttributeList; }; //https://msdn.microsoft.com/en-us/library/windows/desktop/ms684873(v=vs.85).aspx