我在C#里调用dephi写的一个加密用的dll,可能是类型转换不太对的原因,总是出异常System.AccessViolationException。
dephi 里的声明
function Encode(FromFile,ToFile,Key,RemindStr:String;isShowProgress:Boolean): Integer; stdcall; export;C#
[DllImport("Encrypt.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
private static extern int Encode(
    [MarshalAs(UnmanagedType.LPStr)]string FromFile,
    [MarshalAs(UnmanagedType.LPStr)]string ToFile,
    [MarshalAs(UnmanagedType.AnsiBStr)]string Key,
    [MarshalAs(UnmanagedType.AnsiBStr)]string RemindStr,
    bool isShowProgress);请熟悉delphi的大虾帮帮忙,谢了先!

解决方案 »

  1.   

    String必须转化为PCHAR来进行。
      

  2.   

    function Encode(FromFile,ToFile,Key,RemindStr:String;isShowProgress:Boolean): Integer; stdcall; export; 
    String要转换为PChar类型才可以接收.net的StringBuilder类型;其它的都不用变;
      

  3.   

    谢谢gxj760998、lianshaohua!
    dephi代码是客户那边的,能不改delphi代码,通过修c#代码解决吗?
      

  4.   

    不改那边也可以,那么你用再Delphi封一层或用C++封一层,然后改为C#能调用的接口就行了,
      

  5.   

    dumpbin /exports a.dllhttp://topic.csdn.net/u/20090324/12/90f88eba-5194-4b70-ad72-c4f57282a63b.html