-
-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is it possible to target DllImport methods? #268
Comments
External methods might have different calling conventions than normal methods. This isn’t an exact science and depends a lot on your runtime. The underlying mechanics are undocumented so you need to test and hope it works. I got preliminary support for external methods as a pull request from someone and assumed it might work good enough but I am not surprised to see that it does not work. To be clear: the only way this would ever work is to use a transpiler and ignore the input in it and let it create the full replacement method. |
I'm trying this on windows atm, but ideally, I want it working on mac & Linux as well. Usually, I use CoreHook or EasyHook which allows me to intercept native calls, but in this case, the call is actually to a function implemented as unmanaged code inside CLR itself (QCall). I'm completely fine with doing this via transpiler, but I think there's a validation happening that prevents this from even being invoked. It's throwing here: Harmony/Harmony/Internal/MethodPatcher.cs Line 541 in 4f5c658
My current test to see if this will even hook looks like this: public static async Task Main(string[] args)
{
var harmony = new Harmony("main");
harmony.PatchAll();
await Task.Delay(100);
}
[HarmonyPatch]
class Patch
{
static MethodBase TargetMethod() => AccessTools.Method(Type.GetType("System.Threading.TimerQueue"),"CreateAppDomainTimer", new []{typeof(uint),typeof(int)});
[HarmonyTranspiler]
static IEnumerable<CodeInstruction> Transpiler(uint dueTime, int id, ref object __result)
{
Console.WriteLine("Test");
return Enumerable.Empty<CodeInstruction>();
}
} @pardeike On a completely unrelated note, I sent you an email on Jan 29 titled "Harmony usages". My company may be able to offer support for your project as we're using it heavily, so let me know what kind of support you could use. |
I think the oversight in this case is the missing check for an empty prefixes list (in which the exception shouldn’t be thrown). Can you verify if that solves the problem? |
Ok, I went back to commit d3c7a06 and checked that it does in fact call my transpiler as expected. Something in that code changed since then that made it stop working. |
I’ll fix this in the next update. Hopefully by the end of this week. |
Harmony 2.3 breaks dllimport support, while 2.2 runs well. |
And what does that mean in detail? |
The following code runs normally in version 2.2, with no issues and getting the result 42, while in version 2.3.3 it prompts: Error applying patch: HarmonyLib.HarmonyException: Patching exception in method static System.UInt32 Program::GetTickCount() ---> System.InvalidOperationException: Could not get entry point normally, GetFunctionPointer() = 00000000036a082c
|
Is your feature request related to a problem? Please describe.
I'm trying to replace one of the methods declared as DllImport. Wonder if this is possible? The regular hook says to use Transpiler when running, but even when applying transpiler same message comes up.
Describe the solution you'd like
Targeting and replacing the functionality of a method that is defined externally (DllImport)
Additional info:
This is the method I'm trying to replace with my own implementation
The text was updated successfully, but these errors were encountered: