如何使用DLLImport与structs作为参数在C#中? [英] How do I use DLLImport with structs as parameters in C#?

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

问题描述

所有的例子我可以找到使用DLLImport从C#调用C ++代码来回传递int。我可以得到这些例子工作正常。我需要调用的方法需要两个结构作为其导入参数,我不是很清楚如何使这项工作。

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.

这是我要工作与:

我拥有C ++代码,所以我可以对它进行任何更改/添加,我需要。

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

第三方应用程序将在启动时加载我的DLL,并期望DLLExport以某种方式定义,因此我无法真正更改导出的方法签名。

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.

我正在构建的C#应用​​程序将用作包装程序,因此我可以将这个C ++程序集成到我们的其他应用程序中,这些应用程序都用C#编写。

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#.

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

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

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

MathInputStructType定义如下

And MathInputStructType is defined as the following

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


推荐答案

MSDN主题 Passing Structures 有一个很好的介绍,将结构传递到非托管代码。您还需要查看使用平台调用统计数据调度类型数组

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与structs作为参数在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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