0

我正在使用easyhook,退出我的应用程序后,被钩住的应用程序崩溃了。我知道它发生在哪里,但不知道如何解决它。我正在连接 DrawText。我什么都不做:

        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);
    }

但是,如果我取消注释Interface.Read(...)它会在我退出我的 c# 程序时崩溃我的钩子应用程序(否则它会完美运行)。

该函数在我的 c# 代码中,看起来是这样的:

        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);
        }
    }

我能做些什么来防止这个外部应用程序崩溃?或者如何在不出现此问题的情况下将信息从我的 dll 发送到 c# 代码?

谢谢

4

2 回答 2

0

老问题,但万一有人偶然发现这个寻找答案。

崩溃的实际原因是当您关闭 C# 程序时 IPC 服务器已关闭,因此尝试访问该Interface对象将失败。

要修复,请在 Interface.Read 调用周围使用一个try..catch,或者在退出托管 IPC 服务器的 C# 程序之前通过发出信号来禁用挂钩(卸载)。

于 2016-11-24T09:07:11.423 回答
0

我不知道为什么会发生这种情况,经过进一步调查,一旦我知道这是什么错误,我就得到了答案。在完全解决之前我问了另一个问题,这是链接:

当我关闭 c# 应用程序时,dll 中的 System.MissingMethodException

于 2015-08-03T19:18:50.367 回答