1.如果dll中的函数参数含有var,则c#中要加上ref(引用);
否则,会有提示错误:“尝试读取或写入受保护的内容。这通常指示其他内存已损坏”。

解决方案 »

  1.   

    http://topic.csdn.net/u/20100609/14/b6f8e741-7fbd-4286-8478-b8b3ded50bea.html
      

  2.   

    楼主补充 
    我是调用function InceptData(const handle:THandle):integer;
     function ReadInfo(const :integer;const id:integer;d1:PChar;d2:PChar;d3:PChar;d4:PChar;d5:PChar;d6:PChar;d7:PChar;
      d8:PChar;d9:PChar;d10:PChar;d11:PChar;d12:PChar;d13:PChar;d14:PChar;d15:PChar;d16:PChar):integer;stdcall;
    这两个方法出错用C#定义如下
     //接收数据函数 =0成功;=1失败 =11 接收失败 =12 接收头错误 =5 超时错误
      [DllImport("Project2.dll")]
      private static extern int InceptData( IntPtr handle);
     function ReadInfo(const :integer;const id:integer;d1:PChar;d2:PChar;d3:PChar;d4:PChar;d5:PChar;d6:PChar;d7:PChar;
      d8:PChar;d9:PChar;d10:PChar;d11:PChar;d12:PChar;d13:PChar;d14:PChar;d15:PChar;d16:PChar):integer;stdcall;
      

  3.   

    [DllImport("Project2.dll")]
      private static extern int InceptData( IntPtr handle);
    为什么要用Private呢?
      

  4.   

    是不是c#中调用函数语句有错?
    或者你的dll没放对位置?
      

  5.   

    我调用
     [DllImport("Project2.dll")]
      private static extern int Init(int CDT);
      //释放对象 =0成功;=1失败
      [DllImport("Project2.dll")]
      private static extern int UnInit();  //主机初始化com口函数 =0成功;=1失败
      [DllImport("Project2.dll")]
      private static extern int InitCom(string comid);没有问题
    就是刚才我发的那两个函数有问题
      

  6.   


    [DllImport("Project2.dll",CharSet=CharSet.ansi)]试下行不
      

  7.   

    自己用dll建立新的project调用自己的dll看存在问题否!
    delphi中有个内存管理模块叫borlandmm.dll,在你写的函数中,使用到了string,那么就可能 use 这个单元,但这只适合delphi调用(内存共享管理单元),建议重构delphi中导出函数,去掉string,改为pchar,或者其它。
    当然你可以加入borlandmm单元然后再测试下。使用delphi进行调试dll,找到报错地址后再进行分析。顺遍请楼主把调用堆栈贴出来看看把