如何在我的 .NET 类中使用这个本机数组?

发布于 2024-10-07 20:38:14 字数 987 浏览 0 评论 0原文

我正在努力使用 .NET 和 WPF 更新本机 (MFC) 应用程序的 GUI。应用程序实时收集数据,GUI 应该(实时)可视化该数据。总体计划是使用 C++/CLI 作为本机应用程序和托管 GUI 部分之间的互操作层。

我有一个带有字节数组的本机类,其中包含一个“标头”,给出了数组的长度和其中元素的类型。我正在尝试用 C++/CLI 在此本机类和托管 .NET 类之间编写一个互操作层。

我想在应用程序的托管部分使用非托管字节数组。各种博客和文档中心上的信息让我有些不知所措。这是我到目前为止所得到的概要,它可以工作,但有时会崩溃*。我不确定这是否是最合适的解决方案,甚至不确定如何正确实施它。我想知道是否有更合适的方法?

// Fetch numbers from the native class into unmanaged memory.
IntPtr ipNumbers = InteropServices::Marshal::AllocHGlobal(sizeof(int) * arrayLength))
nativeClass->fetchNumbers((int *) ipNumbers.ToPointer(), arrayLength);

// Copy data from unmanaged memory into a managed array.
cli::array<int, 1> ^numbers = gcnew cli::array<int, 1>(arrayLength);
InteropServices::Marshal::Copy(ipNumbers, numbers, 0, arrayLength);
InteropServices::Marshal::FreeHGlobal(ipNumbers);

*) 崩溃消息:“Windows 在 MyProgram.exe 中触发了断点。这可能是由于堆损坏造成的,这表明 MyProgram.exe 或其已加载的任何 DLL 中存在错误。[...]” 。据我所知,是 FreeHGlobal 导致了崩溃。

I'm working on updating the GUI of a native (MFC) application using .NET and WPF. The application collects data in real-time and the GUI is supposed to visualize that data (in real-time). The overall plan is to use C++/CLI as an interoperability layer between the native application and the managed GUI part.

I've got a native class with byte arrays containing a "header" giving the length of the array and the type of the elements in it. I'm trying to write an interoperability layer in C++/CLI between this native class and a managed .NET class.

I want to use the unmanaged byte array in the managed part of the application. I'm somewhat overwhelmed by information on various blogs and documentation centers. This is the outline of what I've got so far, it kind of works but crashes sometimes*. I'm not sure if this is the most appropriate solution, or even how to properly implement it. I would like to know if there is a more suitable approach?

// Fetch numbers from the native class into unmanaged memory.
IntPtr ipNumbers = InteropServices::Marshal::AllocHGlobal(sizeof(int) * arrayLength))
nativeClass->fetchNumbers((int *) ipNumbers.ToPointer(), arrayLength);

// Copy data from unmanaged memory into a managed array.
cli::array<int, 1> ^numbers = gcnew cli::array<int, 1>(arrayLength);
InteropServices::Marshal::Copy(ipNumbers, numbers, 0, arrayLength);
InteropServices::Marshal::FreeHGlobal(ipNumbers);

*) Crash message: "Windows has triggered a breakpoint in MyProgram.exe. This may be due to a corruption of the heap, which indicates a bug in MyProgram.exe or any of the DLLs it has loaded. [...]". From what I can tell it's FreeHGlobal that causes the crash.

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

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

发布评论

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

评论(1

迷途知返 2024-10-14 20:38:14

考虑到 Mattias 的建议,我更改了代码,现在它可以工作了。

cli::array<int, 1> ^numbers = gcnew cli::array<int, 1>(arrayLength);
pin_ptr<int> pp = &numbers[0];
nativeClass->fetchNumbers(pp, arrayLength);

Taking Mattias' suggestion into consideration I changed the code, and it's now working.

cli::array<int, 1> ^numbers = gcnew cli::array<int, 1>(arrayLength);
pin_ptr<int> pp = &numbers[0];
nativeClass->fetchNumbers(pp, arrayLength);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文