~ruther/csharp-dll-injector

ref: f606b7433c934d799b2c94bf8caf1a6f3bd38c13 csharp-dll-injector/DllUtils/Injector.cs -rw-r--r-- 2.5 KiB
f606b743 — František Boháček chore: add license 4 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DllUtils.Modules;
using DllUtils.Process;

namespace DllUtils
{
    public static class Injector
    {
        /// <summary>
        /// Returns RemoteProcessHandle that can be used to Inject dll
        /// </summary>
        /// <param name="processName"></param>
        /// <returns></returns>
        public static RemoteProcessHandle GetRemoteProcess(string processName)
        {
            return GetRemoteProcess(System.Diagnostics.Process.GetProcessesByName(processName).FirstOrDefault());
        }

        /// <summary>
        /// Returns RemoteProcessHandle that can be used to Inject dll
        /// </summary>
        /// <param name="processId"></param>
        /// <returns></returns>
        public static RemoteProcessHandle GetRemoteProcess(int processId)
        {
            return GetRemoteProcess(System.Diagnostics.Process.GetProcessById(processId));
        }

        /// <summary>
        /// Returns RemoteProcessHandle that can be used to Inject dll
        /// </summary>
        /// <param name="process"></param>
        /// <returns></returns>
        public static RemoteProcessHandle GetRemoteProcess(System.Diagnostics.Process process)
        {
            if (process == null)
            {
                return null;
            }

            return new RemoteProcessHandle(process);
        }

        /// <summary>
        /// Injects dll into process using its name
        /// </summary>
        /// <param name="processName"></param>
        /// <param name="dllPath"></param>
        /// <returns></returns>
        public static InjectedModule Inject(string processName, string dllPath)
            => GetRemoteProcess(processName).Inject(dllPath);

        /// <summary>
        /// Injects dll into process using its id
        /// </summary>
        /// <param name="processId"></param>
        /// <param name="dllPath"></param>
        /// <returns></returns>
        public static InjectedModule Inject(int processId, string dllPath)
            => GetRemoteProcess(processId).Inject(dllPath);

        /// <summary>
        /// Injects dll into process using System.Diagnostics.Process
        /// </summary>
        /// <param name="process"></param>
        /// <param name="dllPath"></param>
        /// <returns></returns>
        public static InjectedModule Inject(System.Diagnostics.Process process, string dllPath)
            => GetRemoteProcess(process).Inject(dllPath);
    }
}
Do not follow this link