编译OK,调试时候显示的错误
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。************** 异常文本 **************
System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
   在 HZbox.HZjudge.DoRecognize(Byte[] byteSerializedCh, Int32 iByteNum, Int16 chDestCh, Int16& chWrittenCh, Double[] pEvalout, Int32 iENum, Int32[] state, Byte[] ch)
   在 HZbox.HZjudge.Judge() 位置 I:\GoogleCode\hzapp\HZcomponent.HZbox\HZjudge.cs:行号 114
dll是C++的,是写.def的方式导出的方法
原来的声明是这样的BOOL DoRecognize(
                const unsigned char* pWriteData, 
                const int iDataLen, 
                const unsigned short iDestWChar,
                unsigned short* iWrittenChar, 
                double* pEvalOut, 
                const int iENumber,
                int *state,
                char *cs);用在C#里我写成了[DllImport(@"C:\Windows\System32\RIT.dll", EntryPoint="DoRecognize")]
static extern int DoRecognize(
                byte[] byteSerializedCh,
                int iByteNum,
                short chDestCh,
                ref short chWrittenCh,
                double[] pEvalout,
                int iENum,
                int[] state,
                byte[] ch);总觉得是不是类型错了。另外还有一点不知道值不值得一说,
这个dll回读取txt文件的,一开始调用是文件读不到,所以我把dll读取的文件路径都改成了绝对路径,放在system32下。
然后就变成了这样的错误,请高手指教~

解决方案 »

  1.   

    那是不是应该显示 System.OutofMemoryException
      

  2.   

    二者都如下改:
    // 注意你的编译方式改为Unicode
    extern "C" __declspec(dllexport)
    BOOL __stdcall DoRecognize( const unsigned char* pWriteData, 
                    const int iDataLen, const unsigned short iDestWChar,
                    unsigned short* iWrittenChar, double* pEvalOut, 
                    const int iENumber, int *state, char *cs);
    // 放到你合适的目录下
    [DllImport("RIT.dll", CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Unicode)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool DoRecognize(
                    ref byte byteSerializedCh,
                    int iByteNum,
                    short chDestCh,
                    ref short chWrittenCh,
                    ref double pEvalout,
                    int iENum,
                    ref int state,
                    ref byte ch);
      

  3.   

    如果在非托管代码中越界,访问了未分配的地址空间,就是AccessViolationException。
      

  4.   


    现在要我改RID.dll用stdcall的方式不太现实,
    另外那些指针有些是数组,有些是出参,请教可以都用ref吗?
      

  5.   

    解决了,把组件的方法重载了一个,参数减少到最少,简单的传值就可以搞定。另外在C#里用short代替int,确保安全。最后成功了,谢谢大家的关注和帮助。
      

  6.   

    到底是怎么解决的,没看懂~OGR遇到的这个问题
      

  7.   

    我也遇到这个问题了,问题是DLL中的方法我没办法改,怎么解决啊~