繁体   English   中英

使用easyhook和一个函数将信息从dll发送到我的C#代码后,外部应用程序崩溃

[英]External application crashes after using easyhook and one function to send information from the dll to my c# code

我使用的是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#代码?

谢谢

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

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

旧问题,但万一有人偶然发现此问题以寻找答案。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM