问题
I found out about the error because I saw it in the windows built-in event viewer:
Description: The process was terminated due to an unhandled exception. Exception Info: System.MissingMethodException Stack: at Injection.Main.DrawText_Hooked(...)
I have a c# application using easyhook. My dll critical code:
public void Run(RemoteHooking.IContext InContext, String InChannelName)
{
// Install system hook to detect calls to DrawTextExW that is made by the client and call the function DrawText_Hooked when ever this happens
try
{
DrawTextExHook = LocalHook.Create(LocalHook.GetProcAddress("user32.dll", "DrawTextExW"), new DDrawTextEx(DrawText_Hooked), this);
DrawTextExHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
}....
And my delegate to handle the hooked function is:
int DrawText_Hooked(...)
{
Interface.Read(hdc, lpString, cchText, dwDTFormat);
return DrawTextExW(hdc, lpString, cchText, ref lprc, dwDTFormat, ref dparams);
}
When I shut down my main application everything works fine unless I use Interface.Read(...)
: in this case, the hooked application crashes. I've read it's probably because Interface.Read(...)
doesn't exist anymore once I exit my app but I don't know how to tell my dll to stop doing that or simply unload so that it doesn't try to do Interface.Read(...)
and finds out it doesn't actually exist anymore. How shall I do it?
回答1:
Two days looking for the answer and after posting it I discover it myself after 10':
What I did was to declare the hook static:
static LocalHook DrawTextExHook;
So from my main code, on exit, I could call a static method that points it to null
, therefore stopping calling my Interface.Read(...)
.
public static void stopIt()
{
DrawTextExHook = null;
}
来源:https://stackoverflow.com/questions/31794934/system-missingmethodexception-in-a-dll-when-i-shut-down-c-sharp-application