为C ++结构构造C#结构 [英] Construct C# Struct for C++ struct
本文介绍了为C ++结构构造C#结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从C#的DLL中调用C ++函数.我尝试了与c#不同的选项,但是值无法正确传递/接收.
I need to call C++ function in a DLL from C#.I have tried different options from c#,but values are not passing / receiving properly.
请找到以下我需要从C#调用的c ++代码和Calculate函数.谁能建议我如何在C#中使用适当的类型构造struct并将其传递给c ++.
Please find the below c++ code and the Calculate function which i need to call from C#. Can anyone suggest me how to construct struct with proper types in C# and pass it to c++.
C ++代码
typedef double MyFloat;
typedef int MyInt;
typedef short MyShort;
typedef char MyChar;
struct InParameter{
MyFloat mf;
MyInt mi;
mychar mc;
}
struct OutParameter{
MyFloat mf;
MyInt mi;
MyInt miArray[31];
MyShort ms;
mychar mc;
}
#define MY_DLL __declspec(dllexport)
#define MY_STD __stdcall
#define MY_EXTRN extern "C"
// C++ Function declaration to call from c#
MY_EXTRN MY_DLL MyShort MY_STD Calculate(InParameter &inp,OutParameter &oup)
推荐答案
嗨SureshRajamani,
您可以尝试这样的操作:
Hi SureshRajamani,
You can try something like this:
using System;
using System.Runtime.InteropServices;
namespace MyNameSpace{
public class YourClass{
[DllImport("DllFile.dll", EntryPoint = "HelloWorld")]
public static extern void HelloWorld();
[DllImport("DllFile.dll", EntryPoint = "ShowMe")]
public static extern void ShowMe();
}
}
最佳
问候!
如果有帮助,请提出答案.
Best
regards!
If this was helpful propose it as an answer.
这篇关于为C ++结构构造C#结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文