Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 99 additions & 1 deletion DInvoke/DInvoke/DynamicInvoke/Win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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);
}
}
}
}
12 changes: 6 additions & 6 deletions DInvoke/DInvoke/SharedData/Win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down