如何从 C++ 调用我的 C# 代码dll?访问冲突

发布于 2024-09-15 10:51:18 字数 1141 浏览 11 评论 0原文

背景:

大型 C++ MFC 应用程序不会很快被重写。几年前,我需要添加功能,并认为使用 C# 和 .NET 构建会更容易,因此我需要一种使用 C++ 应用程序调用 .NET 函数的方法。当时我们使用 VC6.0 作为 MFC 应用程序,并使用 VS.NET 2003 和 .NET 1.1 构建 .NET 功能。在 VS.NET 2003 中,我们构建了一个常规的 win32 dll(无 MFC),其中包含 MFC 应用程序可以使用的导出函数。 dll 函数引用了我们的 .NET 程序集,一切都运行良好。现在,我们的MFC应用程序仍然是MFC应用程序,但它是用VS9编译的,并且与旧的2003编译的dll仍然可以正常工作。然而,我们现在面临着创建一个新的 DLL(新功能),并使用 VS9 和 .NET 3.5 来构建它。

问题:

我使用 /clr 选项创建了新的 DLL。我在 .NET 程序集中有一个选项表单,我试图从导出的 dll 函数中调用它。

我收到一条未处理的异常消息,如下所示:

IP2000.exe 中 0x04359108 处未处理的异常:0xC0000005:读取位置 0x00000000 时发生访问冲突

它不喜欢的代码如下所示:

FooTools::frmConfiguration ^ frmConfig = gcnew FooTools::frmConfiguration();
frmConfig -> ShowDialog();

语法已更改。我们曾经使用 *new 代替 ^gcnew,但旧语法无法编译(不确定这是否是一个问题)。

我已经测试过,通过放置直接的 win32 ::MessageBox 来调用导出的函数,并且它被调用。我还尝试从导出的​​函数调用一些不同的托管代码,即。 System::Windows::Forms::MessageBox->Show ... ,而且效果也很好。它只是不想运行此表单。

在我看来,VS 2003 和 VS9 之间一定发生了一些变化,这让我有些悲伤,但我似乎看不出它是什么。 如果从测试 Windows.Forms 应用程序调用,此表单可以正常工作。

Background:

Big C++ MFC app that's not going to get rewritten any time soon. A few years ago I needed to add features and decided that it would be easier built with C# and .NET, so I needed a way to call into .NET functions with the C++ app. At the time we were using VC6.0 for the MFC app, and we built the .NET functionality using VS.NET 2003 and .NET 1.1. In VS.NET 2003 we built a regular win32 dll (no MFC) with exported functions that the MFC app could consume. The dll functions referenced our .NET assemblies and it all worked great. Now, our MFC app is still an MFC app, but it is compiled with VS9, and it still works fine with the old 2003 compiled dll. However, we are now faced with creating a new DLL (new features) and are building it with VS9 and .NET 3.5.

The problem:

I've created the new DLL with the /clr option. I've got an options form in our .NET assembly that I'm trying to invoke from the exported dll function.

I get an unhandled exception message like this:

Unhandled exception at 0x04359108 in IP2000.exe: 0xC0000005: Access violation reading location 0x00000000

The code that it doesn't like looks something like this:

FooTools::frmConfiguration ^ frmConfig = gcnew FooTools::frmConfiguration();
frmConfig -> ShowDialog();

The syntax has changed. We used to use * and new instead of ^ and gcnew, but the old syntax won't compile (not sure if that's an issue anyhow).

I've tested that the exported function is getting called by placing a straight win32 ::MessageBoxand it gets called. I also tried calling some different managed code from the exported function, ie. System::Windows::Forms::MessageBox->Show ... , and it also worked fine. It just doesn't want to run this form.

It seems to me that something must have changed between VS 2003 and VS9 that is causing me some grief, but I can't seem to see what it is.
This form works fine if called from a test Windows.Forms app.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文