调用非托管 c++ C# 代码与 STL 混合

发布于 2024-08-27 15:23:13 字数 384 浏览 3 评论 0原文

嘿,我想在 C# 中调用非托管 C++ 代码 函数接口如下(我简化了它以使其易于理解)

Face genMesh(int param1, int param2);

Face 是一个结构体,定义为:

struct Face{
    vector<float> nodes;
    vector<int>  indexs;
}

我在 google 上搜索并阅读了 MSDN 文档,找到了在 C# 中调用简单的 c/c++ unmanged 代码的方法,也知道如何传递结构体作为返回值。我的问题是如何处理“向量”。我没有找到有关 C# 中向量和某些类型之间映射的规则,

谢谢!

Hey, I want to call unmanaged c++ code in C#
The function interface is like following(I simplified it to make it easy to understand)

Face genMesh(int param1, int param2);

Face is a struct defined as:

struct Face{
    vector<float> nodes;
    vector<int>  indexs;
}

I googled and read the MSDN docs found ways to call simple c/c++ unmanged code in C#, also know how to hand the struct as return value. And My question is how to handle "vector". I did not find rules about mapping between vector and some types in C#

Thanks!

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

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

发布评论

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

评论(3

∝单色的世界 2024-09-03 15:23:13

如果可能的话,您希望避免在纯非托管代码之外的任何内容中使用 STL。当您将其与 C++/CLI(或托管 C++)混合使用时,您可能最终会得到 STL 代码作为托管运行而客户端代码作为非托管运行。发生的情况是,当您迭代向量时,对向量方法的每次调用都会转换为托管代码并再次转换回来。

有关类似问题,请参阅此处

You want, if possible, to avoid using STL in anything but pure UNmanaged code. When you mix it with C++/CLI (or Managed C++), you will likely end up with the STL code running as managed and the client code running as unmanaged. What happens is that when you, say, iterate over a vector, every call to a vector method will transition into managed code and back again.

See here for a similar question.

辞别 2024-09-03 15:23:13

您可能需要传递原始数组,除非您确实想通过互操作跳过一些麻烦,因为规则指定类型必须可由框架编组,或者您已经为框架提供了特定的结构可以马歇尔。这对于矢量来说可能是不可能的。 一样定义 C++ 结构,

#pragma pack(push, 8)
struct ReflSettings
 {
double* Q;
    double* DisplayQ;
 }
 #pragma pack(pop)

因此,您可以像C# 结构

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 8)]
    public class ModelSettings:IDisposable
    {

        [XmlIgnore] internal IntPtr Q;
        [XmlIgnore] internal IntPtr DisplayQ;
    }

希望这会有所帮助。

You're probably going to need to pass the raw arrays unless you really want to jump through some hoops with the interop as the rules specify the types have to either be marhallable by the framework, or you've given the framework a specific structure it can marshall. This probably is not possible for vector. So, you can define you C++ struct as

#pragma pack(push, 8)
struct ReflSettings
 {
double* Q;
    double* DisplayQ;
 }
 #pragma pack(pop)

then you C# struct would be

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 8)]
    public class ModelSettings:IDisposable
    {

        [XmlIgnore] internal IntPtr Q;
        [XmlIgnore] internal IntPtr DisplayQ;
    }

Hope this helps.

策马西风 2024-09-03 15:23:13

最简单的事情可能是在 C++ 中创建一个托管类来表示“Face”结构,并将其内容复制到新的托管类中。然后,您的 C# 代码应该能够理解这些数据。

您可以使用 ArrayList 代替向量。

Probably the simplest thing to do is to create a managed class in C++ to represent the 'Face' structand copy the it's contents into the new managed class. Your c# code should then be able to understand the data.

You could use an ArrayList in place of the vectors.

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