在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 
请按任意键继续.   .   . 

问题出在一定要在引用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了
 
原创粉丝点击