c#调用c++.dll中函数参数问题,急求!!!泪奔~~~~~

k7ta 2008-01-07 04:25:02
在c#中调用dll中方法,目的是要得到struct数组中的数据
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:\code\StationManagementSystem
\jpegtest\testCarRecognition.cs:行号 39
在 jpegtest.testCarRecognition.Main() 位置 D:\code\StationManagementSystem\jp
egtest\testCarRecognition.cs:行号 66
请按任意键继续. . .

求教各位大侠~~~,泪奔中







...全文
230 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
k7ta 2008-01-08
  • 打赏
  • 举报
回复
我已经解决问题了
问题出在一定要在引用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了
zmaini1420 2008-01-07
  • 打赏
  • 举报
回复
UP!~~
k7ta 2008-01-07
  • 打赏
  • 举报
回复
恩,根据你给的数据类型对照,改了如下
public struct LPRRecogRlt
{

public Int32 ChLen ;
public Int16[] Code;
public Int32[] Score;
public Int32 Type;
public Int32 Color;
public Int32 XLeft;
public Int32 XRight;
public Int32 YTop;
public Int32 YBottom;
}
运行后如下问题:

未处理的异常: 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:\code\StationManagementSystem
\jpegtest\testCarRecognition.cs:行号 39
在 jpegtest.testCarRecognition.Main() 位置 D:\code\StationManagementSystem\jp
egtest\testCarRecognition.cs:行号 66
请按任意键继续. . .



changjiangzhibin 2008-01-07
  • 打赏
  • 举报
回复
BOOL=System.Int32
BOOLEAN=System.Int32
BYTE=System.UInt16
CHAR=System.Int16
COLORREF=System.UInt32
DWORD=System.UInt32
DWORD32=System.UInt32
DWORD64=System.UInt64
FLOAT=System.Float
HACCEL=System.IntPtr
HANDLE=System.IntPtr
HBITMAP=System.IntPtr
HBRUSH=System.IntPtr
HCONV=System.IntPtr
HCONVLIST=System.IntPtr
HCURSOR=System.IntPtr
HDC=System.IntPtr
HDDEDATA=System.IntPtr
HDESK=System.IntPtr
HDROP=System.IntPtr
HDWP=System.IntPtr
HENHMETAFILE=System.IntPtr
HFILE=System.IntPtr
HFONT=System.IntPtr
HGDIOBJ=System.IntPtr
HGLOBAL=System.IntPtr
HHOOK=System.IntPtr
HICON=System.IntPtr
HIMAGELIST=System.IntPtr
HIMC=System.IntPtr
HINSTANCE=System.IntPtr
HKEY=System.IntPtr
HLOCAL=System.IntPtr
HMENU=System.IntPtr
HMETAFILE=System.IntPtr
HMODULE=System.IntPtr
HMONITOR=System.IntPtr
HPALETTE=System.IntPtr
HPEN=System.IntPtr
HRGN=System.IntPtr
HRSRC=System.IntPtr
HSZ=System.IntPtr
HWINSTA=System.IntPtr
HWND=System.IntPtr
INT=System.Int32
INT32=System.Int32
INT64=System.Int64
LONG=System.Int32
LONG32=System.Int32
LONG64=System.Int64
LONGLONG=System.Int64
LPARAM=System.IntPtr
LPBOOL=System.Int16[]
LPBYTE=System.UInt16[]
LPCOLORREF=System.UInt32[]
LPCSTR=System.String
LPCTSTR=System.String
LPCVOID=System.UInt32
LPCWSTR=System.String
LPDWORD=System.UInt32[]
LPHANDLE=System.UInt32
LPINT=System.Int32[]
LPLONG=System.Int32[]
LPSTR=System.String
LPTSTR=System.String
LPVOID=System.UInt32
LPWORD=System.Int32[]
LPWSTR=System.String
LRESULT=System.IntPtr
PBOOL=System.Int16[]
PBOOLEAN=System.Int16[]
PBYTE=System.UInt16[]
PCHAR=System.Char[]
PCSTR=System.String
PCTSTR=System.String
PCWCH=System.UInt32
PCWSTR=System.UInt32
PDWORD=System.Int32[]
PFLOAT=System.Float[]
PHANDLE=System.UInt32
PHKEY=System.UInt32
PINT=System.Int32[]
PLCID=System.UInt32
PLONG=System.Int32[]
PLUID=System.UInt32
PSHORT=System.Int16[]
PSTR=System.String
PTBYTE=System.Char[]
PTCHAR=System.Char[]
PTSTR=System.String
PUCHAR=System.Char[]
PUINT=System.UInt32[]
PULONG=System.UInt32[]
PUSHORT=System.UInt16[]
PVOID=System.UInt32
PWCHAR=System.Char[]
PWORD=System.Int16[]
PWSTR=System.String
REGSAM=System.UInt32
SC_HANDLE=System.IntPtr
SC_LOCK=System.IntPtr
SHORT=System.Int16
SIZE_T=System.UInt32
SSIZE_=System.UInt32
TBYTE=System.Char
TCHAR=System.Char
UCHAR=System.Byte
UINT=System.UInt32
UINT32=System.UInt32
UINT64=System.UInt64
ULONG=System.UInt32
ULONG32=System.UInt32
ULONG64=System.UInt64
ULONGLONG=System.UInt64
USHORT=System.UInt16
WORD=System.UInt16
WPARAM=System.IntPtr
changjiangzhibin 2008-01-07
  • 打赏
  • 举报
回复
数据类型未转换好

111,092

社区成员

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

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

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