dll文件内容如下 uses SysUtils, Classes, forms, Unit1 in 'Unit1.pas' {fm_main};{$R *.res}function ShowMainForm(AHandle: THandle;ACaption:PChar): Longint; stdcall; begin Application.Handle := AHandle; fm_Main := Tfm_Main.Create(nil); fm_Main.Caption:=ACaption Result := Longint(fm_Main); fm_Main.Show; end;exports ShowMainForm; end.主调窗体如下: var Form1: TForm1;implementation function ShowMainForm(AHandle: THandle;ACaption:PChar;bCaption:PChar): Longint; stdcall; external 'about.dll'; {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); begin ShowMainForm(application.handle,pchar(edit1.text),pchar(edit2.text));end;
library Project1;{ 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, Unit1 in 'Unit1.pas' {Form1};{$R *.RES}function Returnstr:string;stdcall; begin with TForm1.Create(nil) do try showmodal; Result := trim(edit1.text); finally free; end; end;exports Returnstr;begin end. 这是dll的代码。 procedure TForm1.Button1Click(Sender: TObject); var DllHandle : THandle=0; Rd : function : String; stdcall; begin DllHandle := LoadLibrary('mydll.dll'); if DllHandle > 0 then @Rd := GetProcAddress(DllHandle,'ShowMainForm') else begin DllHandle := 0; @Rd := nil; end; if (DllHandle > 0) and (@Rd <> nil) then edit1.text := Rd else edit1.text := ''; end; 这是调用的代码。你看看。
用setfocus;
第二个问题:
发送消息吧
uses
SysUtils,
Classes,
forms,
Unit1 in 'Unit1.pas' {fm_main};{$R *.res}function ShowMainForm(AHandle: THandle;ACaption:PChar): Longint; stdcall;
begin
Application.Handle := AHandle;
fm_Main := Tfm_Main.Create(nil);
fm_Main.Caption:=ACaption
Result := Longint(fm_Main);
fm_Main.Show;
end;exports
ShowMainForm;
end.主调窗体如下:
var
Form1: TForm1;implementation
function ShowMainForm(AHandle: THandle;ACaption:PChar;bCaption:PChar): Longint; stdcall; external 'about.dll';
{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMainForm(application.handle,pchar(edit1.text),pchar(edit2.text));end;
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,
Unit1 in 'Unit1.pas' {Form1};{$R *.RES}function Returnstr:string;stdcall;
begin
with TForm1.Create(nil) do
try
showmodal;
Result := trim(edit1.text);
finally
free;
end;
end;exports
Returnstr;begin
end.
这是dll的代码。
procedure TForm1.Button1Click(Sender: TObject);
var
DllHandle : THandle=0;
Rd : function : String; stdcall;
begin
DllHandle := LoadLibrary('mydll.dll');
if DllHandle > 0 then
@Rd := GetProcAddress(DllHandle,'ShowMainForm')
else
begin
DllHandle := 0;
@Rd := nil;
end; if (DllHandle > 0) and (@Rd <> nil) then
edit1.text := Rd
else
edit1.text := '';
end;
这是调用的代码。你看看。