5,392
社区成员
发帖
与我相关
我的任务
分享
library mydll;
{ 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}
//此处也试过把dll里面窗体showform函数放到这里来,问题依旧
//正常情况下这边应该还有一些dll卸载机制以及Dll的Application.handle还原机制,和我发现的问题没有什么太大关系,暂时没有加上去
exports
showform;
begin
end.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure showform(ff:THandle);stdcall;
implementation
{$R *.dfm}
procedure showform(ff: THandle);stdcall;
var
A: THandle;
begin
a := Application.Handle; //默认这个A值竟然是exe的application.handle
Application.Handle := ff; //传递进来的ff竟然不等于exe的application.handle,很无奈,颠覆了我学到的Dll的知识,但是又发现网上竟然没有类似的问题
Form1:=TForm1.Create(Application);
Form1.Show;
end;
end.
unit Unitmain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Math;
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
showform: procedure(ff: THandle);
hnd: THandle;
begin
hnd := LoadLibrary(PChar('mydll.dll'));
if hnd>0 then
begin
@showform := GetProcAddress(hnd,PChar('showform'));
if @showform <> nil then
showform(Application.Handle);
end;
end;
end.