【发布时间】:2020-12-14 23:40:28
【问题描述】:
我需要包含一个 .dll,其中包含以下 C/C++ 函数原型和结构定义:
typedef struct
{
unsigned short us1;
unsigned short us2;
unsigned short data[16];
} myStruct;
int myFunc(myStruct *MYSTRUCT);
为了使用这个函数,我创建了一个新类:
static class DLL
{
public struct MyStruct
{
public ushort us1;
public ushort us2;
public ushort[] data;
public PDPORT(ushort[] temp1, ushort temp2, ushort temp3)
{
us1 = temp2;
us2 = temp3;
data = temp1;
}
};
[DllImport("PDL65DLL.dll")]
public static extern int mvb_PutPort(ref MyStruct CommData);
}
在我的主类中,我初始化结构变量并像这样调用函数:
DLL.MyStruct communicationData = new DLL.MyStruct(new ushort[16], new ushort(), new ushort());
DLL.MyFunc( ref communicationData);
但是,这似乎不起作用。该函数正在传递一些东西,但不是正确的值,我建议它与指针使用有关。也许 struct* 与 ref struct 不一样......有人能解释一下问题是什么吗?
【问题讨论】:
-
轻微观察:导出为
myFunc,extern为mvb_PutPort,没有什么可以告诉它的新名称;那只是复制/粘贴的东西吗?因为:那也不会让事情变得快乐
标签: c# c++ pointers struct parameters