I apologize for the inconvenience. DLL injection can be a complex task, and the code provided might not work in all scenarios. Here is an alternative approach you can try:
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace DllInjector
{
public static class InjectDll
{
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Boolean bInheritHandle, Int32 dwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, uint dwSize, AllocationType flAllocationType, MemoryProtection flProtect);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, IntPtr lpThreadId);
[Flags]
private enum AllocationType
{
Commit = 0x1000,
Reserve = 0x2000,
Reset = 0x80000,
LargePages = 0x20000000,
Physical = 0x400000,
TopDown = 0x100000,
WriteWatch = 0x200000
}
[Flags]
private enum MemoryProtection
{
Execute = 0x10,
ExecuteRead = 0x20,
ExecuteReadWrite = 0x40,
ExecuteWriteCopy = 0x80,
NoAccess = 0x01,
ReadOnly = 0x02,
ReadWrite = 0x04,
WriteCopy = 0x08,
GuardModifierflag = 0x100,
NoCacheModifierflag = 0x200,
WriteCombineModifierflag = 0x400
}
public static void Inject(string targetProcessName, string embeddedResourceDllName)
{
try
{
Process targetProcess = Process.GetProcessesByName(targetProcessName)[0];
byte[] dllBytes;
using (Stream dllStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedResourceDllName))
{
dllBytes = new byte[dllStream.Length];
dllStream.Read(dllBytes, 0, dllBytes.Length);
}
IntPtr targetProcessHandle = OpenProcess(0x1F0FFF, false, targetProcess.Id);
IntPtr allocatedMemory = VirtualAllocEx(targetProcessHandle, IntPtr.Zero, (uint)dllBytes.Length, AllocationType.Commit, MemoryProtection.ReadWrite);
WriteProcessMemory(targetProcessHandle, allocatedMemory, dllBytes, (uint)dllBytes.Length, out _);
IntPtr loadLibraryAddress = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
CreateRemoteThread(targetProcessHandle, IntPtr.Zero, 0, loadLibraryAddress, allocatedMemory, 0, IntPtr.Zero);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
}
Make sure to replace "TargetProcessName" with the name of the target process and "EmbeddedResourceDllName" with the name of your embedded resource DLL.
In your console application's entry point, call the InjectDll.Inject method:
class Program
{
static void Main(string[] args)
{
InjectDll.Inject("TargetProcessName", "EmbeddedResourceDllName");
}
}
Please note that DLL injection can be highly sensitive and might not work in every scenario. Additionally, be cautious when using DLL injection, and ensure that you have proper permissions and legal rights to inject DLLs into other processes.