在非托管堆上分配引用类对象

发布于 2024-11-08 18:21:04 字数 655 浏览 0 评论 0原文

如果我在 C++/CLI 中定义一个新的 ref class,我可以使用该类型作为标准非托管类吗?如果可以,ref 关键字是否会更改内存布局数据类型到底是什么?

就我而言,有一个类在一个非常非常大的 C++ 应用程序中到处使用。我们将其称为 MyExampleThing

class MyExampleThing
{
    int a = 0;
    int b = 1;
}

我们正在研究通过 C++/CLI 慢慢将更多 .net 框架组件集成到此应用程序中的可能性。如果我将此类移植到ref class,它(在正常情况下)会继续在其他普通的旧非托管 C++ 代码中工作吗?或者我是否必须保持原样并编写一个 ref class 包装器来处理 .net 互操作?

需要明确的是:我知道我不能将非托管指针传递到托管代码中,我只是问我是否可以将该类单独用作托管或托管或非托管。

值类也是如此吗?

同样重要的是:如果我可以在非托管堆上创建引用类,那么内存布局是否与常规 C++ 类相同? (此类的非托管版本将逐字节序列化为二进制数据。是的,我知道这样做会带来字节序问题,不,我没有做出这个决定。)

If I define a new ref class in C++/CLI, can I then use this type as a standard unmanaged class, and if so, does the ref keyword alter the memory layout of the datatype at all?

In my case, there is a class which is used all over the place in a very very large C++ application. We'll call it MyExampleThing:

class MyExampleThing
{
    int a = 0;
    int b = 1;
}

We're looking at the possibility of slowly integrating more .net framework components into this application through C++/CLI. If I port this class to a ref class would it (under normal circumstances) continue to work in other plain old unmanaged C++ code? Or would I have to leave it as is and write a ref class wrapper to handle .net interop?

To be clear: I know I can't just pass an unmanaged pointer into managed code, I'm just asking if I can use the class separately as either managed or unmanaged.

Is the same true for value class?

Also important: If I can create ref classes on the unmanaged heap, is the memory layout the same as if it were a regular C++ class? (The unmanaged versions of this class get serialized byte for byte as binary data. Yes, I'm aware of endianness issues of doing it that way, no I didn't make that decision.)

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

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

发布评论

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

评论(2

一抹微笑 2024-11-15 18:21:04

ref 不是我熟悉的关键字,所以我会冒险说非托管 C++ 可能不支持它。

如果您有可重用的库类,我只会制作它们在非托管 C++ 中,可以轻松地由托管代码导入和使用,并使 .NET 组件和托管代码分开。更喜欢解耦的类和接口,而不是尽可能做一些事情的对象。

当您确实需要一侧与另一侧通信时,您还可以创建在托管代码和非托管代码之间进行封送的包装器。我的经验仅限于让 C# 应用程序调用 C++ 库,我曾经尝试过 CLI,但我自己并没有发现它特别有用。

ref is not a keyword I'm familiar with so I'm going to go out on a limb and say it's probably not supported by unmanaged C++

If you have reusable classes for a library I would just make them in unmanaged C++ which can easily be imported and used by managed code and keep your .NET components and managed code separated. Prefer decoupled classes and interfaces over objects that do a little bit of everything whenever you can.

You can also make wrappers which marshal between managed and unmanaged code when you do need to have one side communicate with the other. My experience is limited to having a C# application calling into a C++ library I experimented with CLI at one point but didn't find it particularly useful myself.

伴随着你 2024-11-15 18:21:04

可以为托管客户端和本机客户端编写混合模式 DLL。看看这里 http://msdn.microsoft.com/en-us/magazine /cc300632.aspx

就我个人而言,我总是最终用 C++/CLI 编写一个包装器来将 C++ 类公开给 C#。

It is possible to write a mixed mode DLL for both managed and native clients. Take a look here http://msdn.microsoft.com/en-us/magazine/cc300632.aspx.

Personally I have always ended up writing a wrapper in C++/CLI to expose C++ classes to C#.

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