delphi编写的xxx.dll导出函数定义如下:
function DataToStr(
    const aType:Word;
    Const aCode:Word;
    const Data; //传入数据
    Datalen:integer;//传入数据长度
    out Re;//传出数据
    const ReLen:word//传出数据长度
):integer ;
--------------
C#定义:
        [DllImport(@"xxx.dll" , CharSet=CharSet.Ansi)]
        public static extern int DataToStr(UInt16 aType, UInt16 aCode, byte[] Data, int len, ref byte[] re, UInt16 reLen);-----
C#中调用时候出现异常
System.AccessViolationException
-----
问题:
1.请问上述C#定义有无错误
2.出现异常是为什么
3.怎么解决
----
附:msdn对AccessViolationException 的解释
http://msdn2.microsoft.com/zh-cn/library/system.accessviolationexception.aspx

解决方案 »

  1.   

    你的Data以及Re在Delphi中是如何用,什么类型的
      

  2.   

    都是string
    ---
    function DataToStr(const aType:Word;Const aCode:Word;const data:string): string;
    var
      len ,relen: integer;
    begin
      Result:='';
      if length(Data)>0 then
      begin
        if Assigned(FDataToStrFun) then
        begin
          len:=1024;
          SetLength(Result,len);
          FillChar(Result[1],len,0);//FDataToStrFun是从dll中导出的地址
          relen:=FDataToStrFun(aType,aCode,Data[1],length(Data),Result[1],len);
          if relen<=0 then relen:=0 ;
          if relen<len then
            SetLength(Result,len);
        end
        else
          Result:=BinaryToHex(data[1],length(Data));
      end;
    end;
    -------
    我用icesword查了一下,程序运行的时候,xxx.dll没有在内存中
      

  3.   

    我知道xxx.dll的源代码,但是这代码是自动产生的,我不能改任何代码
    --
    多谢
      

  4.   

    如何导用?
     这个是delphi写的,不是c#写的
      

  5.   

    Have a try!
    [DllImport(@"xxx.dll" , CharSet=CharSet.Ansi)]
    public static extern int DataToStr(UInt16 aType, UInt16 aCode, [In]StringBuilder Data, int len, [Out]StringBuilder re, UInt16 reLen);
      

  6.   

    [DllImport(@"xxx.dll" , CharSet=CharSet.Ansi)]
    public static extern int DataToStr(UInt16 aType, UInt16 aCode, [In]StringBuilder Data, int len, [Out]StringBuilder re, UInt16 reLen);
    ------谢谢
    这个不行,一样的结果
    ------
    我怀疑是xxx.dll没有load
    --用icesword查了一下,程序运行的时候,xxx.dll没有在内存中
    不知道有什么方法可以检测
      

  7.   

    都有 这个我比较注意的
    --
    我看过一些资料这里好像只能使用byte[]
      

  8.   

    byte[] data = new byte[lh.DataLength];
    byte[] msg = new byte[lh.MessageLength];
    if (lh.DataLength > 0)
    {
        fs.Read(data, 0, data.Length);
        byte[] re = new byte[data.Length * 2];
        //StringBuilder strData = new StringBuilder(System.Text.Encoding.ASCII.GetString(data, 0, data.Length));
        //StringBuilder strRe = new StringBuilder(strData.Length * 2);
        //DataToStr(lh.Type, lh.Code, strData, strData.Length,  strRe, (UInt16)strRe.Length);
        DataToStr(lh.Type, lh.Code, data, data.Length,ref  re, (UInt16)re.Length);
    }
      

  9.   

    按说StringBuilder就行了,如果你要用byte[],可以试试如下的方式
    [DllImport(@"xxx.dll" , CharSet=CharSet.Ansi)]
    public static extern int DataToStr(UInt16 aType, UInt16 aCode, 
    [MarshalAs(UnmanagedType.LPTStr)] byte[] Data, int len, [Out,MarshalAs(UnmanagedType.LPTStr)] byte[] re, UInt16 reLen);
      

  10.   

    几种方法都试过,还是出现AccessViolationExceptio
    内存也有xxx.dll
      

  11.   

    你在delphi的函数声明后面加一个stdcall。delphi默认才用register(fastcall)调用约定
      

  12.   

    另外Delphi的字符串只适用于Delphi。不要和其他其他语言的字符串混用。跨语言使用c风格字符串
      

  13.   

    我想过dll调用方式的问题,不过最有可能是 delphi字符串的问题
    谢谢你的提醒