0

I'm using easyhook and after exiting my application, the one which was hooked crashes. I know where it happens but not how to solve it. I'm hooking DrawText. I do nothing:

        int DrawText_Hooked(IntPtr hdc, [In, Out, MarshalAs(UnmanagedType.LPTStr)] string lpString, int cchText, [In, Out, MarshalAs(UnmanagedType.Struct)] ref RECT lprc, uint dwDTFormat, [In, Out, MarshalAs(UnmanagedType.Struct)] ref DRAWTEXTPARAMS dparams)
    {
        //Interface.Read(hdc, lpString, cchText, dwDTFormat);
        return DrawTextExW(hdc, lpString, cchText, ref lprc, dwDTFormat, ref dparams);
    }

But if I uncomment Interface.Read(...) it will crash my hooked application when I quit my c# program (otherwise it works perfect).

The function is in my c# code and looks this way:

        public class interfaceA : MarshalByRefObject
    {
        public void ReportException(Exception InInfo)
        {
        }
        public void Ping()
        {
        }
        public void Read(IntPtr hdc, string lpString, int cchText, uint dwDTFormat)
        {
            Console.WriteLine(lpString);
        }
    }

What can I do to prevent this external application to crash? Or how do I send information from my dll to the c# code without having this issue?

Thanks

сами J.D.
  • 483
  • 2
  • 5
  • 19

2 Answers2

0

I didn't know why this was happenning and after further investigation I got the answer once I knew what error it was. I asked another question before solving it completely, here's the link:

System.MissingMethodException in a dll when I shut down c# application

Community
  • 1
  • 1
сами J.D.
  • 483
  • 2
  • 5
  • 19
0

Old question but in case someone stumbles across this looking for an answer.

The actual reason for the crash is that the IPC server is shutdown when you close the your C# program, and therefore trying to access the Interface object will fail.

To fix either use a try..catch around the Interface.Read call, or disable the hook (uninstall) by signalling before exiting the C# program that hosts the IPC server.

Justin Stenning
  • 1,837
  • 1
  • 15
  • 19