c#调用C++DLL时的类型转换问题

icejd 2009-10-08 10:25:17
c++
/* 返回车牌信息 */
typedef struct _VCA_PLATE_INFO_
{
VCA_RECOGNIZE_RESULT resultFlag; /* 识别结果标志,
图像识别: =0:识别失败
1:图像识别成功
视频识别: =0:识别失败
2:视频更优结果,替代上一个识别记录
3:视频新的车辆识别结果 */

VCA_PLATE_TYPE plateClass; /* 车牌类型 */
VCA_PLATE_COLOR plateColor; /* 车牌颜色 */
VCA_RECT_NORMALIZE plateRect; /* 车牌位置,使用归一化,小数点后3位 */
unsigned int reserved[4]; /* 保留 */
unsigned int licenseLen; /* 车牌长度 */
char *pLicense; /* 车牌号码 */
char *pBelieve; /* 各个识别字符的置信度,如检测到车牌"浙A12345",
置信度为10,20,30,40,50,60,70,则表示"浙"字正确的可能性只有10%,
"A"字的正确的可能性是20%
*/

转换为c#如下
    public struct VCA_PLATE_INFO
{
/// <summary>
/// 识别结果标志
/// </summary>
public VCA_RECOGNIZE_RESULT resultFlag;
/// <summary>
/// 车牌类型
/// </summary>
public VCA_PLATE_TYPE plateClass;
/// <summary>
/// 车牌颜色
/// </summary>
public VCA_PLATE_COLOR plateColor;
/// <summary>
/// 车牌位置
/// </summary>
public VCA_RECT_NORMALIZE plateRect;
public uint[] reserved;
///// <summary>
///// 车牌长度
///// </summary>
public uint licenseLen;
/// <summary>
/// 车牌号码
/// </summary>
public StringBuilder pLicense;
/// <summary>
/// 各个识别字符的置信度,如检测到车牌"浙A12345置信度为10,20,30,40,50,60,70,则表示"浙"字正确的可能性只有10%,
///"A"字的正确的可能性是20%*/
/// </summary>
public StringBuilder pBelieve;
}

在运行时却提示
无法封送处理类型为“plateTest.VCA_PLATE_RESULT”的字段“pPlateInfo”: 该字段的类型定义具有布局信息,但具有无效的托管/非托管类型组合或是不可封送的
问题应该是出在这里
public StringBuilder pLicense;
原C++是 char*类型的,查了好多资料,提示说输入型的char*对应string,而输出要对应StringBuilder
我使用string直接提示内存写入出错,请各位帮忙
...全文
358 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
cacagege 2009-11-15
  • 打赏
  • 举报
回复
学习。。
qldsrx 2009-11-15
  • 打赏
  • 举报
回复
这里需要指定长度,C++中明明是长度为4的数组,方法#13楼已给出。
public uint[] reserved;
蒋晟 2009-11-15
  • 打赏
  • 举报
回复
http://blogs.msdn.com/jaredpar/archive/2005/07/11/437584.aspx
fengling2001 2009-11-14
  • 打赏
  • 举报
回复

public struct VCA_PLATE_RESULT
{
public uint plateNum; /* 车牌个数 */
public VCA_PLATE_INFO pPlateInfo; /* 车牌信息结构 */
[MarshalAs(UnmanagedType.ByValArray,SizeConst = 4)]
public uint[] reserved; /* 保留 */
public VCA_PICTURE_INFO pictureInfo; /* 返回图片信息结构 */
}
zdhook 2009-11-14
  • 打赏
  • 举报
回复
[MarshalAs(UnmanagedType.LPWStr)] string pLicense

这个返回的值以空缺会有\0\0之类,你可以用pLicense.TrimEnd('\0');去掉. 祝你好运
Thr21ough 2009-11-14
  • 打赏
  • 举报
回复
学习~
shuiping150 2009-11-14
  • 打赏
  • 举报
回复
指针的都用IntPtr就应该可以了
wartim 2009-10-08
  • 打赏
  • 举报
回复
[StructLayout(LayoutKind.Sequential)]
public struct VCA_PLATE_RESULT
{
[MarshalAs(UnmanagedType.SysUInt)]
public uint plateNum; /* 车牌个数 */
...
}


都指定下看看
icejd 2009-10-08
  • 打赏
  • 举报
回复
是传出的结构体信息
ms44 2009-10-08
  • 打赏
  • 举报
回复
你是需要传入还是传出的?
我记得在C ++中的Strcut在传入的时候C#这边是要用Class,而不是Strcut.
否则会出错。
虽然MSDN 上说是要一个Strcut.
你修改试试。
icejd 2009-10-08
  • 打赏
  • 举报
回复
    /// <summary>
/// 车牌检测结果
/// </summary>
public struct VCA_PLATE_RESULT
{
public uint plateNum; /* 车牌个数 */
public VCA_PLATE_INFO pPlateInfo; /* 车牌信息结构 */
public uint[] reserved; /* 保留 */
public VCA_PICTURE_INFO pictureInfo; /* 返回图片信息结构 */
}


/* 车牌检测结果 */
typedef struct _VCA_PLATE_RESULT_
{
unsigned int plateNum; /* 车牌个数 */
VCA_PLATE_INFO *pPlateInfo; /* 车牌信息结构 */
unsigned int reserved[4]; /* 保留 */
VCA_PICTURE_INFO pictureInfo; /* 返回图片信息结构 */
}VCA_PLATE_RESULT;
lzsh0622 2009-10-08
  • 打赏
  • 举报
回复
传代码
出错信息中 plateTest.VCA_PLATE_RESULT , pPlateInfo 在你传上来的代码中没出现。
icejd 2009-10-08
  • 打赏
  • 举报
回复
还是提示
无法封送处理类型为“plateTest.VCA_PLATE_RESULT”的字段“pPlateInfo”: 该字段的类型定义具有布局信息,但具有无效的托管/非托管类型组合或是不可封送
shalen520 2009-10-08
  • 打赏
  • 举报
回复
应用 [StructLayout(LayoutKind.Sequential)] 到struct试试
icejd 2009-10-08
  • 打赏
  • 举报
回复
还是不管用,可愁死我了

110,536

社区成员

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

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

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