D7写的Dll,D2009如何调用呢?
dll 代码是:
library Code;uses
  SysUtils,AES,
  Classes;function EnCode(Str,Key:String):String;stdcall;
begin
  result:=EncryptString(Str, Key);
end;function DeCode(Str,Key:String):String;stdcall;
begin
  result:=DecryptString(Str, Key);
end; exports
  Encode, DeCode;{$R *.res}begin
end.D2009中 用以下两个调用报错:function EnCode(Str, Key:String):String;stdcall;external 'Code.dll' ;
function DeCode(Str, Key:String):String;stdcall;external 'Code.dll' ;
如何解决?
光 引用时候变成 
function EnCode(Str, Key:AnsiString):AnsiString;stdcall;external 'Code.dll' ;
function DeCode(Str, Key:AnsiString):AnsiString;stdcall;external 'Code.dll'
不行的。

解决方案 »

  1.   

    Dll中不推荐使用String型,建议采用PChar或其它的字符串类型
      

  2.   

    用pchar,即使用string类型,也要考虑宽字符的问题
      

  3.   

    那比如 dll 中
    function EnCode(Str,Key:String):String;stdcall; 
    begin 
      result:=EncryptString(Str, Key); 
    end; 因为EncryptString是aes单元的固定函数, 参数和result都是string的,没法变,dll函数应该怎么定义呢?
      

  4.   

      返回ANSISTRING,加上SHAREMM,本身你都是DELPHI調用最好用PANSICHAR來兼容。 你上面的可以用  StrPCopy   拷貝過來
      

  5.   

    你好,使用string在dll中好像會出錯,不管有沒有將sharemm這個放置在exe或是dll中都有發生錯誤的現像,請改成
    那比如 dll 中 
    function EnCode(Str,Key:pchar):pchar;stdcall; 
    一定可以的,目前我寫的dll都改成pchar了,沒有出錯過,請參考謝謝。
      

  6.   

    dll中都变成pchar了:
    function EnCode(Str,Key:pchar):pchar;stdcall;
    begin
      result:=pchar(EncryptString(Str, Key));
    end;function DeCode(Str,Key:pchar):pchar;stdcall;
    begin
      result:=PChar(DecryptString(Str, Key));
    end;
    D7 中 主程序写
    function EnCode(Str, Key:pchar):pchar;stdcall;external 'Code.dll' ;
    function DeCode(Str, Key:pchar):pchar;stdcall;external 'Code.dll' ;
    begin
      edt2.Text:=EnCode(pchar(edt1.Text), '123');
      edt3.Text:=DeCode(pchar(edt2.Text), '123');
    end;没有问题,都正确。但是主程序代码挪到d2009中编译,也能运行,不过edt2.text变成了:㄰㄰㄰㄰㄰㄰㄰㄰㄰㄰㄰㄰ ,edt3解密不对 这又是啥原因呢?
      

  7.   

    先传普通的字符试试看  再传编码的```另外  编码数据推荐以Stream方式传递以保证成功编码
      

  8.   

    根据以上几位的意见dll改成了
    library Code;uses
      SysUtils,AES,
      Classes;function EnCode(Str,Key:pansichar):pansichar;stdcall;
    begin
      result:=pansichar(EncryptString(Str, Key));
    end;function DeCode(Str,Key:pansichar):pansichar;stdcall;
    begin
      result:=pansichar(DecryptString(Str, Key));
    end;
    exports
      Encode, DeCode;{$R *.res}begin
    end.
    D2009中改成了
    function EnCode(Str, Key:PansiChar):PansiChar;stdcall;external 'Code.dll' ;
    function DeCode(Str, Key:PansiChar):PansiChar;stdcall;external 'Code.dll' ;
    此时不管加密的是什么字符,只能对第一个字节加密,如加密123,结果是对1的加密结果。
    而解密就会报错的。改成
    function EnCode(Str, Key:pwidechar):pwidechar;stdcall;external 'Code.dll' ;
    function DeCode(Str, Key:pwidechar):pwidechar;stdcall;external 'Code.dll' ;
    加解密都不报错,但也是只能对第一个字符进行加解密。
      

  9.   

    result:=pansichar(EncryptString(Str, Key)); 把這句,申請好內存後StrECopy  後返回。
      

  10.   

    Var
      vRetStr : Array[0..255-1] of AnsiChar;
      vEncryptStr : String;  vEncryptStr := 'test';
      strpcopy(vRetStr ,vEncryptStr); 再返回。