包装器DOTNET到本机编写的C ++ CLI BestWay传递strutures?

编程入门 行业动态 更新时间:2024-10-25 00:26:01
本文介绍了包装器DOTNET到本机编写的C ++ CLI BestWay传递strutures?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

然而,我正在写一个包装器在C + + CLI为我们的应用程序给一些新的部分(用C#编写)保存和容易访问旧的本地库。因此,我需要通过一些结构从c#到c ++。这些结构在C ++ Cli(dotnet)和C ++中定义。

Yet I am writting a wrapper in C++ CLI for our application to give some new parts (written in C#) save and easy access to old native libraries. Therefore I need to pass some structures from c# to c++. These structures are defined in C++ Cli (dotnet) and also in C++.

示例:

\\C+++ typedef struct { INFO16 jahr ; INFO8 monat ; INFO8 tag ; INFO8 stunde ; INFO8 minute ; } SDATUM; \\c++ cli [StructLayout(LayoutKind::Explicit)] public value struct SDATUM { public: [FieldOffset(0)] UInt16 jahr; [FieldOffset(2)] Byte monat; [FieldOffset(3)] Byte tag; [FieldOffset(4)] Byte stunde; [FieldOffset(5)] Byte minute; };

现在我在C ++ cli中提供一些函数,接受这种类型的dotnet类型为SDATUM值,通过引用(sdatum%)和指针sdatum *,就像有相应的本机函数。我需要转换/转换这些结构?

Now I provide some functions in C++ cli which accept this type as dotnet type SDATUM in form of pass by value,by reference (sdatum%) and pointer sdatum* like there corresponding native functions. What do I need to convert/cast these struct?

推荐答案

我发现另一个解决方案是非常容易,简短,复制数据。 你可以这样调用想要C SDATUM的本地函数:

I found another solution which is very easy, short and does not need copying data. You could call native functions which want a C SDATUM in this way:

//signature void someFunction(SDATUM datum); void someFunctionWrapper(SDATUM datum){ pin_ptr<SDATUM> datum_pin=&datum; //::SDATUM refers to the C-Type someFunction(*(::SDATUM*)datum_pin); }

我测试了它,因为两个SDATUM结构都有相同的位结构。因为我只调用短的本地函数,我认为碎片没有问题。

I tested it and it works because both SDATUM Structs have the same bit structure. Because I only call short native functions I think fragmentation is no problem.

更多推荐

包装器DOTNET到本机编写的C ++ CLI BestWay传递strutures?

本文发布于:2023-05-25 10:39:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226424.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:本机   DOTNET   CLI   strutures   BestWay

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!