我想学习足够的C#,以便可以通过引用传递一个结构C DLL;但它永远不会达到“c功能”。正如你可以在cFunction中看到的那样,我明确地将streamSubset值设置为44;但回到C#部分它不会返回“44”。 这里是C代码:从C#传递结构到C DLL
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();
}
}
}
请提供更多的信息 - 你怎么看呢?崩溃?意外的结果?不正确的数据?你尝试过调试吗? – reuben
C#代码无法使用调试器运行。很长的解释,但它是一种商业产品,可以防止它。 – PaeneInsula