Mbucari / InjectDotnet

Inject dotnet .NET/.NET Framework dlls into Win32 or Win64 native processes and hook native functions
GNU General Public License v3.0
58 stars 3 forks source link

Problem injecting .NET 7 DLL into .NET 7 target #5

Open lahma0 opened 10 months ago

lahma0 commented 10 months ago

Hi again @Mbucari , I'm attempting to use your library again but this time I'm trying to inject a .NET 7 DLL into a .NET 7 process. Unfortunately, after injecting (WaitForReturn: true), Marshal.ThrowExceptionForHR is throwing a COM exception and the 1st line of code in my injected DLL isn't even running (1st line is debug output which isn't appearing). My Inject method looks like this:

targetProcess.Inject("ColorControlPatcher.runtimeconfig.json", "ColorControlPatcher.dll", "ColorControlPatcher.ColorControlPatcher, ColorControlPatcher, Version=1.0.0.0, Culture=neutral, PublicKey=null", "MyMethod", string.Empty, true);

I also tried using the following as the fully qualified name as it is what is actually returned by 'methodInfo.DeclaringType.AssemblyQualifiedName':

"ColorControlPatcher+ColorControlPatcher, ColorControlPatcher, Version=1.0.0.0, Culture=neutral, PublicKey=null"

Since .NET 7 DLLs don't normally output a runtimeconfig.json file, I included <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> in the project file of my DLL that is injected (so a runtimeconfig.json file is being output now).

Here is an image of the exception being generated: image

The error code being returned is -1073741819 or 0xC0000005.

Here is the beginning of the bootstrap method in my dll that is being injected:

namespace ColorControlPatcher
{
    public static class ColorControlPatcher
    {
        public static int MyMethod(string arg)
        {
            WriteDebugLine($"'{nameof(ColorControlPatcher)}' injected successfully!");
[...]

My injector is set to x64 and the process I'm injecting into running as a 64-bit process. I've tried following your examples as closely as possible and I've tried every variation and permutation I can think of but nothing is working. I'm sure I'm doing something wrong though.. Is there anything you can see that I'm explicitly doing wrong or is there any advice you can offer? I really appreciate your help. Thanks!

Mbucari commented 10 months ago

@lahma0 I'm almost certain that your problem will be fixed by replacing <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> with <EnableDynamicLoading>true</EnableDynamicLoading>. See the sample project.

If that doesn'5t work, please attach a copy of your code for me to test. Thanks!

lahma0 commented 10 months ago

@Mbucari Unforunately, that did not work. I've attached a zip which includes my solution containing both the injector and the patcher. It also contains a copy of the program that I'm attempting to inject the patcher DLL into ('ColorControl' directory). This is totally not important so if you're busy or have better things to worry about at the moment, please don't waste time on it. If you can get around to looking at it at some point, that would be great, but seriously, no hurry. ColorControlPatcher.zip