我如何使用dllimport的结构与在C#中的参数? [英] How do I use DLLImport with structs as parameters in C#?

查看:162
本文介绍了我如何使用dllimport的结构与在C#中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以找到使用的DllImport从C#传递整数来回调用C ++代码的所有的例子。我可以得到这些例子的工作就好了。我需要调用的方法有两个结构作为它的进口参数,我不完全清楚,我怎样才能使这项工作。



下面是我得到了什么工作有:



我自己的C ++代码,这样我就可以做任何更改/添加到它,我需要



一个第三方应用程序将在启动时加载我的DLL,预计该DLLEXPORT被定义一定的方式,所以我不能真正改变方法签名多数民众赞成获取导出。



在C#应用程序我建设将被用来作为一个包装,所以我可以集成这个C ++一块到我们的一些其他应用程序,它们都用C#编写的。



我需要调用C ++方法签名看起来像这样

  DLLEXPORT INT计算(常量MathInputStuctType *输入, 
MathOutputStructType *输出,无效** formulaStorage)

和MathInputStructType被定义为以下

  typedef结构MathInputStuctTypeS {
INT _setData;
双_data [(INT)字段大小]
INT _setTdData;
} MathInputStuctType;


解决方案

MSDN主题的Passing 有一个很好的介绍,传递结构非托管代码结构。您也想看看封送处理数据与平台调用和的Marshaling类型的数组的。


All the examples I can find using DLLImport to call C++ code from C# passes ints back and forth. I can get those examples working just fine. The method I need call takes two structs as its import parameters, and I'm not exactly clear how I can make this work.

Here's what I've got to work with:

I own the C++ code, so I can make any changes/additions to it that I need to.

A third party application is going to load my DLL on startup and expects the DLLExport to be defined a certain way, so i can't really change the method signature thats getting exported.

The C# app I'm building is going to be used as a wrapper so i can integrate this C++ piece into some of our other applications, which are all written in C#.

The C++ method signature I need to call looks like this

DllExport int Calculate (const MathInputStuctType *input, 
    MathOutputStructType *output, void **formulaStorage)

And MathInputStructType is defined as the following

typedef struct MathInputStuctTypeS {
    int				_setData;
    double				_data[(int) FieldSize];
    int				_setTdData;
} MathInputStuctType;

解决方案

The MSDN topic Passing Structures has a good introduction to passing structures to unmanaged code. You'll also want to look at Marshaling Data with Platform Invoke, and Marshaling Arrays of Types.

这篇关于我如何使用dllimport的结构与在C#中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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