好像是数组就是不行,我又改成这样了: [DllImport("vp.dll", EntryPoint="InitVp",CharSet=CharSet.Auto)] public static extern int F_InitVp(ref commconf pConf);public struct commconf { [MarshalAs(UnmanagedType.I2)] public short total; [MarshalAs(UnmanagedType.I2)] public short level; [MarshalAs(UnmanagedType.I2)] public ushort tcp_port; [MarshalAs(UnmanagedType.I2)] public short cli_num; [MarshalAs(UnmanagedType.I4)] public int BW ; [MarshalAs(UnmanagedType.I4)] public int BL; [MarshalAs(UnmanagedType.I2)] ushort [] cliport; [MarshalAs(UnmanagedType.I1)] byte [,] cli; public commconf(int intParam) { total=0; level=0; tcp_port=0; cli_num=0; BW=0; BL=0; cliport=new ushort[CLINUM_MAX]; cli=new byte[CLINUM_MAX,20]; } };调用 if (F_InitVp(ref myCOMMCONF)<0) 出错: 未处理的“System.TypeLoadException”类型的异常出现在 TCenter.exe 中。其他信息: 无法封送类型 commconf 的字段 cliport:该类型无法作为结构字段进行封送处理。
这个应该可以满足你的要求,你试试看!
例如:
Vc的引出函数对应C#的函数:
private IntPtr abc(IntPtr a, IntPtr b, int c, bool d, IntPtr e);
这个函数中返回值和函数的参数有几个都是指针类型的
对于结构体的定义,结构体内部的指针类型也需要用IntPtr类型
对于IntPtr和结构体类型的转换可以用Marshal中的许多方法。在此不罗列了。
需要using System.Runtime.InteropServices;
欢迎一起研究.net技术
我的Email:[email protected]
如果是数组(只说了一维),这样写:
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]我的结构里恰好是一个多维数组:
unsigned char cli[CLINUM_MAX][20];
文章里没有说怎么定义
[DllImport("vp.dll", EntryPoint="InitVp",CharSet=CharSet.Auto)]
public static extern int F_InitVp(ref commconf pConf);public struct commconf
{
[MarshalAs(UnmanagedType.I2)]
public short total;
[MarshalAs(UnmanagedType.I2)]
public short level;
[MarshalAs(UnmanagedType.I2)]
public ushort tcp_port;
[MarshalAs(UnmanagedType.I2)]
public short cli_num;
[MarshalAs(UnmanagedType.I4)]
public int BW ;
[MarshalAs(UnmanagedType.I4)]
public int BL;
[MarshalAs(UnmanagedType.I2)]
ushort [] cliport;
[MarshalAs(UnmanagedType.I1)]
byte [,] cli;
public commconf(int intParam)
{
total=0;
level=0;
tcp_port=0;
cli_num=0;
BW=0;
BL=0;
cliport=new ushort[CLINUM_MAX];
cli=new byte[CLINUM_MAX,20];
}
};调用
if (F_InitVp(ref myCOMMCONF)<0)
出错:
未处理的“System.TypeLoadException”类型的异常出现在 TCenter.exe 中。其他信息: 无法封送类型 commconf 的字段 cliport:该类型无法作为结构字段进行封送处理。
const int CLINUM_MAX =30;
[StructLayout(LayoutKind.Explicit)]
public struct commconf
{
//[MarshalAs(UnmanagedType.I2)]
[FieldOffset(0)]public short total;
//[MarshalAs(UnmanagedType.I2)]
[FieldOffset(2)]public short level;
//[MarshalAs(UnmanagedType.I2)]
[FieldOffset(4)]public ushort tcp_port;
//[MarshalAs(UnmanagedType.I2)]
[FieldOffset(6)]public short cli_num;
//[MarshalAs(UnmanagedType.I4)]
[FieldOffset(8)]public int BW ;
//[MarshalAs(UnmanagedType.I4)]
[FieldOffset(12)]public int BL;
//[MarshalAs(UnmanagedType.I2)]
[FieldOffset(16)]ushort [] cliport;
//[MarshalAs(UnmanagedType.I1)]
[FieldOffset(76)]byte [,] cli;
public commconf(int intParam)
{
total=0;
level=0;
tcp_port=0;
cli_num=0;
BW=0;
BL=0;
cliport=new ushort[CLINUM_MAX];
cli=new byte[CLINUM_MAX,20];
}
};
还是同样的错误,用IntPtr也试验了,但是方法可能不对,MSDN也没有例子,只有一个概述
//[MarshalAs(UnmanagedType.I2)]
[FieldOffset(16)]IntPtr [] cliport;
//[MarshalAs(UnmanagedType.I1)]
[FieldOffset(76)]IntPtr [,] cli;
错误是:
未处理的“System.Runtime.InteropServices.COMException”类型的异常出现在 TCenter.exe 中。其他信息: 格式太旧或是类型库无效。[MarshalAs(UnmanagedType.I2)]这个属性加不加好像是没有什么效果
(备注,此DLL的函数在VC中可以正确调用)