warbler / SharpMonoInjector

A tool for injecting assemblies into Mono embedded applications, commonly Unity Engine based games
MIT License
310 stars 108 forks source link

Injection failed: mono_class_from_name() returned NULL #11

Open PandaSt0rm opened 5 years ago

PandaSt0rm commented 5 years ago

So I've come across a rather interesting issue, and I have no idea how to actually resolve it. Essentially I've just been testing injecting into a game process, and it was working fine initially, however after a few loads and unloads I started getting a mono_class_from_name error.

I've tested with a new project, still getting the same error.

This is the 'loader' class

using UnityEngine;

namespace Test
{
    public class Loader
    {
        public static GameObject _Load;

        public static void Load()
        {
            _Load = new GameObject();
            // Add component classes to load into the game
            _Load.AddComponent<Main>();
            Object.DontDestroyOnLoad(_Load);
        }

        public static void Unload()
        {
            Object.Destroy(_Load);
        }

    }
}

And what happens when I attempt to inject (same result for either GUI or Console)

.\smi.exe inject -p DEMO -a .\Test.dll -n Test -c Loader -m Load                                                                         Failed to inject assembly: SharpMonoInjector.InjectorException: mono_class_from_name() returned NULL
   at SharpMonoInjector.Injector.ThrowIfNull(IntPtr ptr, String methodName)
   at SharpMonoInjector.Injector.GetClassFromName(IntPtr image, String namespace, String className)
   at SharpMonoInjector.Injector.Inject(Byte[] rawAssembly, String namespace, String className, String methodName)
   at SharpMonoInjector.Console.Program.Inject(Injector injector, CommandLineArguments args)

Something I have noticed is that if I name the Load method as Init, it """works""" (nothing is actually injected but smi reports success).

In fact as I was typing this out it started working again (using Load as the class name)

.\smi.exe inject -p DEMO -a .\Test.dll -n Test -c Loader -m Load                                                                         Test.dll: 0x00000193B4DDE5F0

Could .NET be caching something, because I was trying to load/unload immediately before, but now DEMO was closed for around 10 minutes before I attempted the same command on the same dll, and it worked :shrug:

PandaSt0rm commented 5 years ago

Close process, reopen, try to inject, get Injection successful, but nothing is actually injected at all now

PandaSt0rm commented 5 years ago

I can't explain the behavior. Sometimes it works, sometimes it doesn't. Pulled the code, debugged, and the debugger isn't indicating anything that would be causing such random issues... 🤷‍♂

safazi commented 5 years ago

Encountering the same issue now.

frap129 commented 4 years ago

I had this issue, fix (For me at least) was to compile my dll with the same version of .NET framework as the game I was injecting it into.

mapleint commented 4 years ago

How did you find the version of the game?