下面是我动态调用DLL的代码(测试过没问题),现在我想在不改动现有代码的情况下,加一个函数,把主调程序的S变量,传到DLL窗体上的label1.caption中.主调代码:unit Unit1;   //主调程序interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
 tshowcalendar=function(ahandle:thandle;acaption:pchar):tdatetime;stdcall;
  edllloaderror=class(exception);
  Tmainform = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  mainform: Tmainform;implementation{$R *.dfm}procedure Tmainform.Button1Click(Sender: TObject);
var
 libhandle:thandle;
 showcalendar:tshowcalendar;
 s:STRING;
begin
 libhandle:=loadlibrary('dll.dll');
 try
 if libhandle=0 then
  raise edllloaderror.Create('DLL不存在!');
  @showcalendar:=getprocaddress(libhandle,'showcalendar');
 if not (@showcalendar=nil) then
  showcalendar(application.MainForm.Handle,pchar(caption))
 else
  raiselastwin32error;
finally
 freelibrary(libhandle);
end;end;DLL窗体代码:unit dll_1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TdllForm = class(TForm)
    Label1: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Label1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  dllform: Tdllform;function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall;
implementation{$R *.dfm}
  function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall;
var
  dllform: Tdllform;
begin
  application.Handle := ahandle;
  dllform := tdllform.Create(application);
  try
    dllform.Caption := acaption; 
    dllform.ShowModal;
    Result := Now; 
  finally
    dllform.Free;
  end;
end;procedure TdllForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
end;

解决方案 »

  1.   

    定义的function showcalendar(ahandle: thandle; acaption: PChar): tdatetime; stdcall; 里面加个参数:
    function showcalendar(ahandle: thandle; acaption: PChar;S:string): tdatetime; stdcall; 如果不想改这个函数就自己再定义一个函数
    procedure SetCaption(s:string);stdcall; 
    procedure SetCaption(s:string);stdcall; 
    begin
      label1.caption:=s;
    end;
      

  2.   

    在DLL工程文件和调用程序的工程文件第一个引用ShareMem
      

  3.   

    加一个参数即可,如果使用String类型的话,则需要引用ShareMem,发布时要带Borlandmm.dll
      

  4.   

    如果不想用Delphi的内存管理,推荐试试FastMM
      

  5.   

    function showcalendar(ahandle: thandle; acaption,s: PChar): tdatetime; stdcall; 
    var 
      dllform: Tdllform; 
    begin 
      application.Handle := ahandle; 
      dllform := tdllform.Create(application); 
      dllform.label1.caption:=s;
      try 
        dllform.Caption := acaption; 
        dllform.ShowModal; 
        Result := Now; 
      finally 
        dllform.Free; 
      end; 
    end; 
    DLL窗体代码这样写不知对不对,另外下面是主调不知如何改.请教了..........
    type 
    tshowcalendar=function(ahandle:thandle;acaption,s:pchar):tdatetime;stdcall; procedure Tmainform.Button1Click(Sender: TObject); 
    var 
    libhandle:thandle; 
    showcalendar:tshowcalendar; 
    s:STRING; 
    begin 
    libhandle:=loadlibrary('dll.dll'); 
    try 
    if libhandle=0 then 
      raise edllloaderror.Create('DLL不存在!'); 
      @showcalendar:=getprocaddress(libhandle,'showcalendar'); 
    if not (@showcalendar=nil) then 
      showcalendar(application.MainForm.Handle,pchar(caption)) 
    else 
      raiselastwin32error; 
    finally 
    freelibrary(libhandle); 
    end; 
      

  6.   

    听说尽量不要用STRING,用PCHAR要好些是吧.
      

  7.   

    对,尽量不要用string和数组参数,那样的话,其他语言调用可能会出问题
      

  8.   

    1:但在我在主调这边代码还是不知用何写呀.
    2:DLL窗体代码这边改的不知对不对呀.
    function showcalendar(ahandle: thandle; acaption,s: PChar): tdatetime; stdcall; 
    var 
      dllform: Tdllform; 
    begin 
      application.Handle := ahandle; 
      dllform := tdllform.Create(application); 
      dllform.label1.caption:=s; 
      try 
        dllform.Caption := acaption; 
        dllform.ShowModal; 
        Result := Now; 
      finally 
        dllform.Free; 
      end; 
    end; 
      

  9.   

    这个话题我研究过,不一定非要PChar的。
    可以用Variant
    可惜CSDN把这篇帖子删了。
    否则给你链接。
    你可以试一下Variant
    这个参数不仅让你可以传String,还可以操作更多的类型。
    而且也不用ShareMM
      

  10.   

    1:但在我在主调这边代码还是不知用何写呀. 
    2:DLL窗体代码这边改的不知对不对呀.function showcalendar(ahandle: thandle; acaption,s: PChar): tdatetime; stdcall; 
    var 
      dllform: Tdllform; 
    begin 
      application.Handle := ahandle; 
      dllform := tdllform.Create(application); 
      dllform.label1.caption:=s; 
      try 
        dllform.Caption := acaption; 
        dllform.ShowModal; 
        Result := Now; 
      finally 
        dllform.Free; 
      end; 
    end;