我在dll的projiect里面定义了
function CreateCommandLine(Command:DWORD; Param: PChar): PChar;stdcall;export;function CreateCommandLine(Command: DWORD; Param: PChar): PChar;stdcall;
var
  TmpStr: string;
begin
  //
end;
=====exe程序调用===========
function CreateCommandLine(Command:DWORD; Param: PChar): PChar;stdcall;external 'AnyGameCom.dll' name '_CreateCommandLine';
调用CreateCommandLine(3,pchar('2222222'));当我在dll的工程里面调试的时候,发现exe程序传递到dll的参数param是乱码啊。为什么呢?编程环境:winxp sp3 + delphi 2009 Version 12.0.3420.21218 

解决方案 »

  1.   

    直接调用不行吗?
    CreateCommandLine(3, '2222222'); 
    或者将PChar改为string类型,然后将ShareMem单元做为DLL和EXE工程的第一个use单元,程序发布时带上borlndmm.dll。
      

  2.   

    直接调用不行吗?
    CreateCommandLine(3, '2222222');
      

  3.   


    直接调用当然可以,我不是想封装到DLL里面嘛。
      

  4.   


    用这个DLL貌似有不好的地方,内存泄露?
      

  5.   

    DELPHI在创建dll工程的时候,说的很清楚了,
    To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. 为了避免使用宝兰妹妹,那么可以使用PCHAR来传递string参数嘛。为什么我的回错误啊。救命啊~~~
      

  6.   

    用Pchar传参数
    或者用sharemem
      

  7.   

    声明个变量,然后GETMEM,赋值,传送,再FREEMEM看看
      

  8.   

    是不是Freemem的太早了,你不要把PChar设成局部变量,设置成全局试试,在窗口关闭的时候再Freemem
      

  9.   

    同意用widestring类型,以前这样用过。
      

  10.   

    DELPHI 2009不知道是不是改過處理, DELPHI7裡面, 直接傳是不會有問題的, 但是你返回的PChar就有可能有問題.
    或者你需需要在DLL當中屏閉使用UNICODE, 可能DLL當中默認定編譯字符為UNICODE不定...
    像那種調用方式, 隻會存在這兩種可能的錯誤, 不會有第三種的
      

  11.   

    试了一下没有问题,可能楼主函数名字写错了代码如下
    dll 中library dllPro;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }uses
      SysUtils,
      Classes,
      Types,Dialogs;{$R *.res}function CreateCommandLine(Command: DWORD; Param: PChar): PChar;stdcall;
    var
      TmpStr: string;
    begin
        TmpStr := Param;
        ShowMessage(TmpStr);
    end;exports
      CreateCommandLine;begin
    end.
    主程序中unit TestForm;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;function CreateCommandLine(Command:DWORD; Param: PChar): PChar;stdcall;external 'dllPro.dll' name 'CreateCommandLine';  implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      CreateCommandLine(1,'aaa'); 
    end;end.
      

  12.   

    widestring类型应该也可以的,我就这么用。
      

  13.   

    调用的方法改为用变量就可以了.
    var pc1: pchar;
    begin
      pc1 := pchar('2222222');
      CreateCommandLine(3,pc1); 
    end;
    为什么要这样做呢?
    因为你的格式是传立即数,是传值的,而delphi在这里字符的处理机制是传地址的.
      

  14.   


    function CreateCommandLine(Command:DWORD; Param: PChar): PChar;stdcall;export;function CreateCommandLine(Command: DWORD; Param: PChar): PChar;stdcall;
    var
      TmpStr: string;
    begin
      TmpStr:=StrPas(Param);//----断点这里,看param就是乱码
      GetMem(Result,Length(TmpStr)+1);
      strpcopy(Result,TmpStr);
    end;
      

  15.   


    函数名不会错的。否则也调用不了啊。我用的是D2009 啊。你呢?会不会是pchar类型在D2009里面变成了Pwidechar或者pansiChar的缘故啊。
      

  16.   

    都用PAnsiChar   先。
      

  17.   


    uses 里引用sharemem即可
      

  18.   

    其实那个变成过程, 不用函数也行的dll中过程定义
       CreateCommandLine(Command: DWORD; Param: PChar; var aa :pChar);
    主程序定义aa变量,取返回值.
    var
       aa :pChar;调用  DLL中的过程
       
       CreateCommandLine(Command: DWORD; Param: PChar; var aa[0]);
      

  19.   

    打错. 
    这样...~~~~~
    CreateCommandLine(1,  'abc', aa[0]);