在c#中调用dll中方法,目的是要得到struct中的数据
来源:互联网 发布:网络推广都有哪些网站 编辑:程序博客网 时间:2024/05/17 02:19
dll中函数签名如下:
[System.Runtime.InteropServices.DllImport("XXXXX.dll")]
public static extern char Test(byte[] ImBuf, int nr, int nc, out int nResult, LPRRecogRlt[] fwOcrRlt, int XXX);
其中LPRRecogRlt数据结构如下:
public struct LPRRecogRlt
......{
public int ChLen ;
public char[] Code;
public int[] Score;
public int Type;
public int Color;
public int XLeft;
public int XRight;
public int YTop;
public int YBottom;
}
我的调用如下:
LPRRecogRlt[] fwOcrRlt = new LPRRecogRlt[5];
//初始化一些数据,不重要
for (int t = 0; t < fwOcrRlt.Length; t++)
......{
fwOcrRlt[t].ChLen = 0;
fwOcrRlt[t].Type = 0;
fwOcrRlt[t].Color = 1;
fwOcrRlt[t].XLeft = 10;
fwOcrRlt[t].XRight = 10;
fwOcrRlt[t].YBottom = 20;
fwOcrRlt[t].YTop = 20;
fwOcrRlt[t].Code = new char[12];
fwOcrRlt[t].Score = new int[12];
}
//******************************
char result = Test(data, test.Height / 2, test.Width, out nresult, fwOcrRlt, 1);
运行是有如下问题:
未处理的异常: System.ArgumentException: 参数不正确。 (异常来自 HRESULT:0x800700
57 (E_INVALIDARG))
在 jpegtest.DLLMethod.FenWei_Car_Recog(Byte[] ImBuf, Int32 nr, Int32 nc, Int3
2& nResult, LPRRecogRlt[] fwOcrRlt, Int32 CarIndex)
在 jpegtest.testCarRecognition.testRec() 位置 D:codeStationManagementSystem
jpegtest estCarRecognition.cs:行号 39
在 jpegtest.testCarRecognition.Main() 位置 D:codeStationManagementSystemjp
egtest estCarRecognition.cs:行号 66
请按任意键继续. . .
[System.Runtime.InteropServices.DllImport("XXXXX.dll")]
public static extern char Test(byte[] ImBuf, int nr, int nc, out int nResult, LPRRecogRlt[] fwOcrRlt, int XXX);
其中LPRRecogRlt数据结构如下:
public struct LPRRecogRlt
......{
public int ChLen ;
public char[] Code;
public int[] Score;
public int Type;
public int Color;
public int XLeft;
public int XRight;
public int YTop;
public int YBottom;
}
我的调用如下:
LPRRecogRlt[] fwOcrRlt = new LPRRecogRlt[5];
//初始化一些数据,不重要
for (int t = 0; t < fwOcrRlt.Length; t++)
......{
fwOcrRlt[t].ChLen = 0;
fwOcrRlt[t].Type = 0;
fwOcrRlt[t].Color = 1;
fwOcrRlt[t].XLeft = 10;
fwOcrRlt[t].XRight = 10;
fwOcrRlt[t].YBottom = 20;
fwOcrRlt[t].YTop = 20;
fwOcrRlt[t].Code = new char[12];
fwOcrRlt[t].Score = new int[12];
}
//******************************
char result = Test(data, test.Height / 2, test.Width, out nresult, fwOcrRlt, 1);
运行是有如下问题:
未处理的异常: System.ArgumentException: 参数不正确。 (异常来自 HRESULT:0x800700
57 (E_INVALIDARG))
在 jpegtest.DLLMethod.FenWei_Car_Recog(Byte[] ImBuf, Int32 nr, Int32 nc, Int3
2& nResult, LPRRecogRlt[] fwOcrRlt, Int32 CarIndex)
在 jpegtest.testCarRecognition.testRec() 位置 D:codeStationManagementSystem
jpegtest estCarRecognition.cs:行号 39
在 jpegtest.testCarRecognition.Main() 位置 D:codeStationManagementSystemjp
egtest estCarRecognition.cs:行号 66
请按任意键继续. . .
问题出在一定要在引用dll方法的时候
方法签名的参数前面要加[in out]
也就是说如下:
public static extern char Test(byte[] ImBuf, int nr, int nc, out int nResult, [In, Out] LPRRecogRlt[] fwOcrRlt, int CarIndex);
数据结构如下:
[StructLayout(LayoutKind.Sequential)]
public struct LPRRecogRlt
...{
[MarshalAs(UnmanagedType.I4,SizeConst=1)]
public int ChLen ;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public char[] Code;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public int[] Score;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int Type;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int Color;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int XLeft;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int XRight;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int YTop;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int YBottom;
}
调用如下
char result = DLLMethod.Test(data, test.Height / 2, test.Width, out nresult, fwOcrRlt, 1);
然后就OK了
方法签名的参数前面要加[in out]
也就是说如下:
public static extern char Test(byte[] ImBuf, int nr, int nc, out int nResult, [In, Out] LPRRecogRlt[] fwOcrRlt, int CarIndex);
数据结构如下:
[StructLayout(LayoutKind.Sequential)]
public struct LPRRecogRlt
...{
[MarshalAs(UnmanagedType.I4,SizeConst=1)]
public int ChLen ;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public char[] Code;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public int[] Score;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int Type;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int Color;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int XLeft;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int XRight;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int YTop;
[MarshalAs(UnmanagedType.I4, SizeConst = 1)]
public int YBottom;
}
调用如下
char result = DLLMethod.Test(data, test.Height / 2, test.Width, out nresult, fwOcrRlt, 1);
然后就OK了
- 在c#中调用dll中方法,目的是要得到struct中的数据
- 在C#中调用DLL
- C#中dll调用方法
- 在C#中调用C++Dll函数的方法
- C#中 dll文件放在子目录中的方法
- 在C#中如何调用dll文件
- 在C#中如何调用dll文件
- 在C#中如何调用dll文件
- C++打包DLL在C#中调用
- C#中调用DLL
- C# 中调用DLL
- C#中调用dll
- C#中调用DLL
- C# 中调用DLL
- C#中调用DLL
- c#中调用DLL
- C# 中调用dll
- 在C++中调用DLL中的函数
- 求两个数互质算法
- 组件聚合
- iframe 刷新
- 配置WebSphere portal的三个重要文件
- 百度酝酿进军品牌广告领域 加强商业模式探索
- 在c#中调用dll中方法,目的是要得到struct中的数据
- 使用 jQuery 简化 Ajax 开发
- VC中自定义消息实现
- Tcl script 笔记
- 用JavaScript创建超酷时钟背景特效
- js正则-验证常用的表单对象
- C,实现求集合的并集和交集
- 如何备份 Team Foundation Server
- AJAX在PHP中的简单使用