C#怎样从dll文件中读取结构体变量
我在dll文件中定义了一个结构体变量,如下:
//funcdll2.dll
typedef struct __packed PIBif
{
char DAK[HPAVKEY_SHA_LEN * 3];
char MAC[HPAVKEY_SHA_LEN * 3];
}
PIBif;
static struct PIBif * pibif;
并在主程序中通过函数返回该指针
extern _declspec(dllexport) struct PIBif * readpib( )
{
extern struct PIBif * pibif;
..........
return(pibif);
}
然后C#程序中定义如下:
namespace macload.winApp
{
public partial class Form1 : Form
{
public struct PIBif
{
public string DAK;
public string MAC;
}
[DllImport("funcdll2.dll")]
public static extern PIBif readpib( );
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PIBif pibif = readpib();
textBox1.Text = pibif.DAK.ToString();
}
}
}
编译链接通过,生成了.exe程序,可是在exe程序中一点按键就发生错误退出,提示“方法的类型与PINVOKE不兼容”,请问这是怎么回事?
------解决方案--------------------
[DllImport("funcdll2.dll")]
public static extern PIBif readpib( );
这里改成
[DllImport("funcdll2.dll")]
public static extern IntPtr readpib( );
然后用Marshal.PtrToStructure手动拆封IntPrt为具体类型