c#调用c++dll问题,急!!

ffff41 2012-05-04 10:56:14
c++调用声明
struct _dh_dev_base_info
{
int dwManuID;
unsigned short int nDeviceClassID;
unsigned short int nDeviceID;
ULARGE_INTEGER GUID;
};
typedef struct _dh_dev_base_info dh_dev_base_info_t;

struct _dhir_device
{
dh_dev_base_info_t base_info;
char cpi_layer[64];
char device[64];

int port;
char addr[32];
int flags;
};
typedef struct _dhir_device dhir_device_t;

int __stdcall enumerate_devices(
OUT dhir_device_t *device,
OUT int *count
);

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
对应的c#声明
public class dhirCam
{
public struct _dh_dev_base_info
{
public UInt32 dwManuID;
public UInt32 nDeviceClassID;
public UInt32 nDeviceID;
public UInt32 GUID;
}

[StructLayout(LayoutKind.Sequential)]
public struct _dhir_device
{
public _dh_dev_base_info base_info;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 64)]
public string cpi_layer;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 64)]
public string device;
public UInt32 port;
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 32)]
public string addr;
public UInt32 flags;
}


[DllImport("cam.dll", EntryPoint = "enumerate_devices", CharSet = CharSet.Ansi)]
public static extern int enumerate_devices([MarshalAs(UnmanagedType.LPArray)] _dhir_device[] device, ref int count);
}
------------------------------------------------------------------------
c#调用
int count = 0;
int result = 0;
dhirCam._dhir_device[] device=new dhirCam._dhir_device[16];
result=dhirCam.enumerate_devices( device ,ref count);
if (result == 0)
{
MessageBox.Show("0"+count.ToString()+" "+device[0].device);
}
else {
MessageBox.Show("1" + count.ToString() + " " + device[0].device);
}

返回后 count有值,但是device 都是空
...全文
406 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
gflp123 2012-08-24
  • 打赏
  • 举报
回复
楼主 我的程序也是运行一小会自动退出软件,但能正常显示,自动退出怎样解决的
ffff41 2012-05-07
  • 打赏
  • 举报
回复
运行一段时间现在会出现:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
ffff41 2012-05-07
  • 打赏
  • 举报
回复
tv_sec和tv_usec改为Int32依然运行一小会自动退出软件。
public static Int32 StreamCallback(ref dhirCam._dhir_data_buffer indata)
{
//不对数据做任何处理,直接返回


return 0;
}

ffff41 2012-05-06
  • 打赏
  • 举报
回复
public IntPtr data;
public IntPtr ptr_user;
这两个类型上有错吗?
ffff41 2012-05-06
  • 打赏
  • 举报
回复
可以了,改成这种形式[Out]就可以了,谢谢 foreachif !!
但是又碰到另一个问题了。

/*
struct _dhir_rect
{
int32_t x;
int32_t y;
int32_t width;
int32_t height;
};*/
[StructLayout(LayoutKind.Sequential)]
public struct _dhir_rect
{
public Int32 x;
public Int32 y;
public Int32 width;
public Int32 height;
}

/*
struct _dhir_format
{
char identifier[128];
dhir_rect_t size;
dhir_rect_t min_size;
dhir_rect_t max_size;
int32_t bpp;
u_int32_t fourcc;
u_int32_t flags;

int32_t buffer_size;

};
*/
[StructLayout(LayoutKind.Sequential)]
public struct _dhir_format
{
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 128)]
public string identifier;
public _dhir_rect size;
public _dhir_rect min_size;
public _dhir_rect max_size;
public Int32 bpp;
public UInt32 fourcc;
public UInt32 flags;
public Int32 buffer_size;
}
/*
struct timeval
{
long tv_sec;
long tv_usec;
};
*/
[StructLayout(LayoutKind.Sequential)]
public struct timeval
{
public Int64 tv_sec;
public Int64 tv_usec;
}
/*
struct _dhir_data_buffer
{
dhir_format_t format;

int32_t frame_number;
int32_t process_finish;
struct timeval fill_time;
unsigned char *data;
void *ptr_user;
u_int32_t reserved[8];
};
*/
[StructLayout(LayoutKind.Sequential)]
public struct _dhir_data_buffer
{
public _dhir_format format;
public Int32 frame_number;
public Int32 process_finish;
public timeval fill_time;
public IntPtr data;
public IntPtr ptr_user;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public UInt32[] reserved;
}

/*
int __stdcall dhir_start_capture(IN dhir_handle_t handle,IN dhir_callback_func_t callback_func,void *user_data);
*/
[DllImport("dhircam.dll", EntryPoint = "dhir_start_capture", CharSet = CharSet.Ansi)]
public static extern int dhir_start_capture(IntPtr handle, dhir_callback_func_t callback_func, IntPtr user_data);

--------------------------------------------------------
c++
/* user callback function */
typedef dhir_status_t (*dhir_callback_func_t)( dhir_data_buffer_t *buffer);

c#
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate Int32 dhir_callback_func_t(ref dhirCam._dhir_data_buffer indata);

static dhir_callback_func_t myStreamCallback = new dhir_callback_func_t(StreamCallback);


public static Int32 StreamCallback(ref dhirCam._dhir_data_buffer indata)
{



return 0;
}




程序可运行,但是运行一小会,自动莫名退出。查不出问题。
WAN 2012-05-06
  • 打赏
  • 举报
回复
对于类似的情形,有一种通行的做法:count不仅用于被调者输出实际数量,还用于调用者输入提供的缓冲区数量,因此,改为:int count = 16;
WAN 2012-05-06
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 的回复:]
public IntPtr data;
public IntPtr ptr_user;
这两个类型上有错吗?
[/Quote]
可以这样声明。所有指针类型都可以在C#中简单地声明为IntPtr,其关键在于调用时赋予其正确的含义,或者说让它指向一个正确的地址(通常是一个已分配的且固定的缓冲区地址)。
就已贴出的代码来看,有一个问题,C++的long类型一般和int一样,只是32位的,并非64位的,因此tv_sec和tv_usec应为Int32
WAN 2012-05-05
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ffff41 的回复:]
还是不行。
[/Quote]
怎么个不行法呢?你原来贴的代码按我6#进行更改后,C#调用应已没问题。注意是[Out]特性,而非out关键字
ffff41 2012-05-05
  • 打赏
  • 举报
回复
还是不行。
test2050 2012-05-04
  • 打赏
  • 举报
回复
result=dhirCam.enumerate_devices( device ,ref count);
device不需要 ref吗,猜得哈哈。
熙风 2012-05-04
  • 打赏
  • 举报
回复
count有值那调用dll就没问题啊,,device有问题
ffff41 2012-05-04
  • 打赏
  • 举报
回复
楼上说细点。。。
qq598235031 2012-05-04
  • 打赏
  • 举报
回复
参照 dotnet 环境下使用COM组件
ffff41 2012-05-04
  • 打赏
  • 举报
回复
我试下,试完后回复。
WAN 2012-05-04
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]
数据类型对应有问题
unsigned short int uint16
int int32
ULARGE_INTEGER 这个应该是 UInt64 吧

你再检查检查其他的吧
[/Quote]
++

还有,
1、_dh_dev_base_info声明时也需[StructLayout(LayoutKind.Sequential)]
2、public static extern int enumerate_devices([Out]_dhir_device[] device, ref int count);
这里MarshalAs不是必需的,但[Out]是必需的
bdmh 2012-05-04
  • 打赏
  • 举报
回复
数据类型对应有问题
unsigned short int uint16
int int32
ULARGE_INTEGER 这个应该是 UInt64 吧

你再检查检查其他的吧
ffff41 2012-05-04
  • 打赏
  • 举报
回复
自己感觉是在device定义上有问题。
ffff41 2012-05-04
  • 打赏
  • 举报
回复
之前有加ref,会出现这种错误。
Common sources of this bug include user marshaling errors for COM-interop

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧