Ejemplo n.º 1
0
 internal static extern SafeMemoryHandle CreateThread(
     [In] ref SecurityAttributes lpThreadAttributes,
     [Optional] int dwStackSize,
     [Optional] IntPtr lpStartAddress,
     IntPtr lpParameter,
     [Optional] ThreadCreationFlags dwCreationFlags,
     [Optional] out int lpThreadId);
Ejemplo n.º 2
0
        public static SafeMemoryHandle CreateThread(SafeMemoryHandle processHandle,
                                                    IntPtr startMethodAddress, IntPtr parameterAddress,
                                                    SecurityAttributes threadAttributes = default(SecurityAttributes),
                                                    ThreadCreationFlags creationFlags   = 0, int stackSize = 0)
        {
            int threadId;
            SafeMemoryHandle threadHandle = Imports.CreateRemoteThread(
                processHandle,
                ref threadAttributes,
                stackSize,
                startMethodAddress,
                parameterAddress,
                creationFlags,
                out threadId);

            if (threadHandle.IsInvalid)
            {
                throw new Win32Exception($"[Win32 Error: {Marshal.GetLastWin32Error()}] " +
                                         $"Unable to create thread with " +
                                         $"0x{processHandle.DangerousGetHandle().ToString("X")} using " +
                                         $"0x{startMethodAddress.ToString($"X{IntPtr.Size}")}(0x{parameterAddress.ToString($"X{IntPtr.Size}")}) " +
                                         $"Attributes[InheritHandle: {threadAttributes.InheritHandle}, " +
                                         $"Length: {threadAttributes.Length}, " +
                                         $"SecurityDescriptor: {threadAttributes.SecurityDescriptor.ToString($"X{IntPtr.Size}")}] " +
                                         $"CreationFlags: 0x{creationFlags.ToString("X")} StackSize: {stackSize}");
            }
            return(threadHandle);
        }