包装器DOTNET到本机编写的C ++ CLI BestWay传递strutures? [英] Wrapper for DOTNET to native written in C++ CLI BestWay to pass strutures?

查看:237
本文介绍了包装器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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆