~ruther/csharp-dll-injector

ref: cdae945a3820ed9f45639ad2915dd54c099dceaf csharp-dll-injector/DllUtils/Threads/RemoteThread.cs -rw-r--r-- 2.6 KiB
cdae945a — František Boháček Add cpp examples 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using DllUtils.Interop;
using DllUtils.Process;

namespace DllUtils.Threads
{
    public class RemoteThread
    {
        private int _waitForSingleObjectResult;
        private bool _waitedForObject;

        public RemoteThread(ProcessHandle process, IntPtr startAddress, IntPtr param, IntPtr handle)
        {
            Handle = handle;
            if (handle != IntPtr.Zero)
            {
                IsOpened = true;
            }

            StartAddress = startAddress;
            Param = param;
            Handle = handle;
            Process = process;
        }

        public bool IsOpened { get; private set; }

        public IntPtr Handle { get; private set; }

        public IntPtr StartAddress { get; }

        public IntPtr Param { get; }

        public ProcessHandle Process { get; }


        public bool Create()
        {
            if (!IsOpened)
            {
                Process.Open();

                Handle = Kernel32.CreateRemoteThread(Process.Handle, (IntPtr) null, IntPtr.Zero, StartAddress, Param, 0,
                    (IntPtr) null);

                int error = Marshal.GetLastWin32Error();

                if (Handle != IntPtr.Zero)
                {
                    IsOpened = true;
                }
            }

            return Handle != IntPtr.Zero;
        }

        public int WaitForResult(int timeout = 100000)
        {
            _waitForSingleObjectResult = Kernel32.WaitForSingleObject(Handle, timeout);
            _waitedForObject = true;

            return _waitForSingleObjectResult;
        }

        public bool CheckResult()
        {
            if (!_waitedForObject)
            {
                WaitForResult();
            }

            int result = _waitForSingleObjectResult;
            if (result == 0x00000080L || result == 0x00000102L || result == 0xFFFFFFF)
            { 
                Close();
                return false;
            }

            return true;
        }

        public IntPtr GetResult()
        {
            bool success = Kernel32.GetExitCodeThread(Handle, out IntPtr result);
            if (!success)
            {
                return IntPtr.Zero;
            }

            return result;
        }

        public void Close()
        {
            if (Handle != IntPtr.Zero)
            {
                Kernel32.CloseHandle(Handle);
                IsOpened = false;
                Handle = IntPtr.Zero;
            }
        }
    }
}
Do not follow this link