gpt4 book ai didi

c# - 将结构从 c# 传递到 C dll

转载 作者:太空宇宙 更新时间:2023-11-04 04:01:21 25 4
gpt4 key购买 nike

我正在尝试学习足够多的 C#,以便我可以通过引用 C DLL 来传递结构;但它永远不会到达“cFunction”。正如您在 cFunction 中看到的,我明确地将 streamSubset 值设置为 44;但回到 c# 部分,它不会返回“44”。这是 C 代码:

typedef struct s_mPlot
{
double price[100];
int streamSubset;
} mPlot;

extern "C" __declspec( dllexport )
void cFunction(mPlot *Mplot){
Mplot->streamSubset = 44;}

//这是c#代码

 using System;
using Vibe.Function;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential)]
public class MPLOT
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public double [] price;
public int streamSubset;
}

namespace Vibe.Indicator{
public class myIdx : IndicatorObject {

[DllImport("C:\\Users\\joe\\mcDll.dll", CharSet = CharSet.Auto)]
public static extern void cFunction(
[In, MarshalAs(UnmanagedType.LPStruct)] MPLOT mPlot );
public myIdx(object _ctx):base(_ctx){}
private IPlotObject plot1;

protected override void Create()
{
MPLOT mPlot = new MPLOT();
mPlot.streamSubset = 2;
cFunction(mPlot);

if (mPlot.streamSubset == 44)
go();
}

}

最佳答案

我可以看到以下内容:

  1. 您几乎肯定需要在DllImport 属性中指定cdecl 调用约定。添加 CallingConvention=CallingConvention.Cdecl
  2. 我相信 UnmanagedType.LPStruct 增加了一个额外的间接级别。但是您传递的是 C# class,它是一个引用类型。这意味着您正在将指针传递给指针。这是一个间接级别太多了。首先,完全删除 [In, MarshalAs(UnmanagedType.LPStruct)]。然后你的代码应该工作。如果您为 MPLOT 切换到结构而不是类,那么您需要通过 ref 来获取间接寻址。

我想我会有这样的代码:

[StructLayout(LayoutKind.Sequential)]
public struct MPLOT
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public double [] price;
public int streamSubset;
}

[DllImport("dllname.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern void cFunction(
ref MPLOT mPlot
);

关于c# - 将结构从 c# 传递到 C dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11305557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com