C# 加载 C++ DLL - 退出时出现问题

发布于 2024-11-04 19:38:20 字数 480 浏览 0 评论 0原文

好的,我有一个使用 C++ DLL 的 C# 用户界面。该DLL实际上是一个OpenGL/SDL游戏。 游戏结束后,它会返回到 C# UI。据我所知,这一切都运行得很好,而且是正确的。

当我尝试退出实际程序时,问题就出现了。 C# 表单关闭,但很快就会出现一个错误,这是相当难以描述的。我认为这与 DLL 有关,也许它仍然是打开的?如何确保 DLL 已正确关闭?或者你如何将它们整合在一起?

我按如下方式打开 DLL:

    [DllImport("AsteroidGame.dll")]
    public static extern int EntryPoint();

    private void rungame()
    {
            EntryPoint();
    }

提前致谢。

编辑

错误只是说:

vshost32.exe已停止工作

Ok so I have a C# user interface which uses a C++ DLL. The DLL is actually an OpenGL/SDL game.
Once the game has finished it goes back to the C# UI. This all works nicely and as far as I know, correctly.

The problem comes when I try to exit the actual program. The C# form closes, however an error follows shortly which is pretty undescriptive. I assume it is something to do with the DLL, perhaps it is still open? How does one make sure that the DLL has closed properly? Or how do you close it all together?

I'm opening the DLL as follows:

    [DllImport("AsteroidGame.dll")]
    public static extern int EntryPoint();

    private void rungame()
    {
            EntryPoint();
    }

Thanks in advance.

EDIT

The error simply says:

vshost32.exe has stopped working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身仙ぐ女味 2024-11-11 19:38:20

当应用程序退出时,Windows 会卸载该 dll。在此过程中,dll 的静态变量将被破坏。如果您的游戏没有正确结束,并且某些循环仍在向静态类发送事件,而静态类又将它们路由到您的 C# UI,您可能会收到此类错误。

首先,您应该在退出 C# UI 之前检查是否调用了游戏引擎的所有清理例程。如果这没有帮助,您需要进一步调试非托管代码。

约鲁斯,
阿洛伊斯·克劳斯

The dll is unloaded by Windows when the application exits. During this process the static variables of your dll will be destroyed. If your game did not properly end and some loop is still sending events to e.g. a static class which in turn routes them into your C# UI you can get this sort of error.

At first you should check if you did call all cleanup routines of your game engine before you exit the C# UI. If that does not help you need to debug further into unmanaged code.

Yorus,
Alois Kraus

梦里寻她 2024-11-11 19:38:20

您无法从 C# 卸载外部程序集,只能卸载加载它的应用程序域。您可以创建一个应用程序域 (http://msdn.microsoft.com/en-us/library/system.appdomain.aspx),然后从此处加载 C++ 程序集。完成游戏后,卸载应用程序域。

You cant unload an external assembly from c#, you can only unload the App Domain that loaded it. You could create an App Domain (http://msdn.microsoft.com/en-us/library/system.appdomain.aspx) then load the c++ assembly from here. When you are finished with the game, unload the App Domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文