private void SetDataCallBack(int cameraId = 0, IntPtr pReserved = new IntPtr(), int len = 0)
申明的时候不要给默认值,在调用的时候给。 int SetDataCallBack(DataCallBack fData, int cameraId, IntPtr pReserved, int len);调用的时候 SetDataCallBack(CallbackMethod, 0, IntPtr.Zero, 0);
发现原函数的调用是这样子的: char ppRe[6] = {'e','e','e','e','e'}; printf("sizeof(ppRe) =%d\n", sizeof(ppRe)); m_CameraIndex[id] = cameraId; SetEventCallBack(CallbackEvent, cameraId, ppRe, sizeof(ppRe)); 我按照你的方法声明了:public static extern int SetEventCallBack(EventCallBack fEvent, int cameraId = 0, IntPtr pReserved = new IntPtr(), int len = 0); 我的调用是这个样子的: char[] ppRe = new char[6]; for (int i = 0; i < 5;i++ ) { ppRe[i] = 'e'; } int pLen = ppRe.Length; nRet = SDKMfcDll.SetEventCallBack(eventCallback,handle,ppRe,pLen); 却提示:参数 3: 无法从“char[]”转换为“System.IntPtr”
C#不支持参数默认值,如果要实现相同功能,
可以使用函数重载的方法模拟实现。
例如:private void ShowMess(string message)
{
Color color = Color.White;
}
private void ShowMess(string message, Color color)
{
}
int SetDataCallBack(DataCallBack fData, int cameraId, IntPtr pReserved, int len);调用的时候
SetDataCallBack(CallbackMethod, 0, IntPtr.Zero, 0);
char ppRe[6] = {'e','e','e','e','e'};
printf("sizeof(ppRe) =%d\n", sizeof(ppRe));
m_CameraIndex[id] = cameraId;
SetEventCallBack(CallbackEvent, cameraId, ppRe, sizeof(ppRe));
我按照你的方法声明了:public static extern int SetEventCallBack(EventCallBack fEvent, int cameraId = 0, IntPtr pReserved = new IntPtr(), int len = 0);
我的调用是这个样子的:
char[] ppRe = new char[6];
for (int i = 0; i < 5;i++ )
{
ppRe[i] = 'e';
}
int pLen = ppRe.Length;
nRet = SDKMfcDll.SetEventCallBack(eventCallback,handle,ppRe,pLen);
却提示:参数 3: 无法从“char[]”转换为“System.IntPtr”
char ppRe[6] = {'e','e','e','e','e'};
printf("sizeof(ppRe) =%d\n", sizeof(ppRe));
m_CameraIndex[id] = cameraId;
SetEventCallBack(CallbackEvent, cameraId, ppRe, sizeof(ppRe));
如果按照你的方法声明了,该怎么调用呢?谢谢
byte[] data = new byte[] { 0x65, 0x65, 0x65, 0x65, 0x65, 0};
IntPtr pRe = Marshal.AllocHGlobal(data.Length); // 分配6字节的非托管内存
Marshal.Copy(data, 0, pRe, data.Length);SetEventCallBack(CallbackEvent, cameraId, pRe, data.Length);Marshal.FreeHGlobal(pRe); // 用完以后别忘了释放