用于DOTNET的包装,以C ++ CLI BestWay编写的本机传递结构? [英] Wrapper for DOTNET to native written in C++ CLI BestWay to pass structures?

查看:60
本文介绍了用于DOTNET的包装,以C ++ CLI BestWay编写的本机传递结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但是,我正在用C ++ CLI为我们的应用程序编写包装器,以节省一些新部分(用C#编写)并易于访问旧的本机库.因此,我需要将一些结构从C#传递到C ++.这些结构是在C ++ Cli(dotnet)和C ++中定义的.

Yet I am writing 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中提供了一些函数,它们通过值,引用(基准百分比)和指针基准*的形式接受该类型作为点网类型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编写的本机传递结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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