有一动态库中,一函数申明如下
void* StartICDMCommand(void*,int,int, void *, int);
在C#里
[DllImport("D:\\Edm\\dll5k.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr StartICDMCommand(IntPtr device,int Address,int cmd, IntPtr Param, int size);//如果在c++里
int cmd=28;
int nRevCount=200;
StartICDMCommand(device,5,cmd,(void*)&nRevCount,sizeof(int));
这样可以成功与设备通信
我在c#里StartICDMCommand(device,5,cmd,new IntPtr(200),sizeof(int));
失败
请问要在C#里要怎样才能实现上面功能(C++)
void* StartICDMCommand(void*,int,int, void *, int);
在C#里
[DllImport("D:\\Edm\\dll5k.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr StartICDMCommand(IntPtr device,int Address,int cmd, IntPtr Param, int size);//如果在c++里
int cmd=28;
int nRevCount=200;
StartICDMCommand(device,5,cmd,(void*)&nRevCount,sizeof(int));
这样可以成功与设备通信
我在c#里StartICDMCommand(device,5,cmd,new IntPtr(200),sizeof(int));
失败
请问要在C#里要怎样才能实现上面功能(C++)
intptr 只是相当于指针,
你需要
marshal.AllocTaskMem(200)
int value
)参数value
类型:System.Int32
32 位有符号的整数中包含的指针或句柄。 上面我的回复,有些错Marshal.AllocHGlobal
int pm=200;
StartICDMCommand(device,5,cmd,new (IntPtr)pm,sizeof(int));
其实那是考勤的动态库,向考勤机发送读取命令,每次读取200行.
在C++里完全可以实现.C#老是报错...
在C#发送广播校时
StartICDMCommand(device,0,1,IntPtr.Zero,0);
都OK
声明的时候,在加个ref试试
int a = 200;
Intptr ptr;
Marshal.StructureToPtr (a,ptr,true);
void* Param
不一定是int类型的,当命令字不同,参数传递,有的是整数,有的是结构体等
装箱,拆箱
Marshal.structuretoptr
IntPtr buffer = Marshal.AllocHGlobal(size);Marshal.StructureToPtr (形参变量,buffer ,true);。。
public static extern IntPtr StartICDMCommand(IntPtr device,int Address,int cmd, ref int Param, int size);