在dll中有一窗体,如何在程序里掉用(50分)

  • 主题发起人 主题发起人 yangpeng0622
  • 开始时间 开始时间
Y

yangpeng0622

Unregistered / Unconfirmed
GUEST, unregistred user!
在dll中有一窗体,如何在程序里掉用
 
看看教程吧,小子·
 
看看
http://www.delphibbs.com/keylife/iblog_show.asp?xid=4126
 
library CalendarLib;

uses
ShareMem,
SysUtils,
Classes,
DLLFrm in 'DLLFrm.pas' {DLLForm};

exports
ShowCalendar;

begin
end.
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
unit DLLFrm;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Grids, Calendar;

type

TDLLForm = class(TForm)
calDllCalendar: TCalendar;
procedure calDllCalendarDblClick(Sender: TObject);
end;

{ Declare the export function }
function ShowCalendar(AHandle: THandle
ACaption: String): TDateTime
StdCall;

implementation
{$R *.DFM}

function ShowCalendar(AHandle: THandle
ACaption: String): TDateTime;
var
DLLForm: TDllForm;
begin
// Copy application handle to DLL's TApplication object
Application.Handle := AHandle;
DLLForm := TDLLForm.Create(Application)

try
DLLForm.Caption := ACaption;
DLLForm.ShowModal;
Result := DLLForm.calDLLCalendar.CalendarDate
// Pass the date back in Result
finally
DLLForm.Free;
end;
end;

procedure TDLLForm.calDllCalendarDblClick(Sender: TObject);
begin
Close;
end;
、、、、、、、、、、、、、、、、、、、、、、、、、以上为DLL工程及DLL单元
下面是调用工程及单元(详细见delphi5开发人员指南)
program CalendarTest;

uses
Forms,
MainFfm in 'MainFfm.pas' {MainForm};

{$R *.RES}

begin
Application.CreateForm(TMainForm, MainForm);
Application.Run;
end.
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
unit MainFfm;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;

type
{ First, define a procedural data type, this should reflect the
procedure that is exported from the DLL. }
TShowCalendar = function (AHandle: THandle
ACaption: String): TDateTime
StdCall;

{ Create a new exception class to reflect a failed DLL load }
EDLLLoadError = class(Exception);

TMainForm = class(TForm)
lblDate: TLabel;
btnGetCalendar: TButton;
procedure btnGetCalendarClick(Sender: TObject);
end;

var
MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.btnGetCalendarClick(Sender: TObject);
var
LibHandle : THandle;
ShowCalendar: TShowCalendar;
begin

{ Attempt to load the DLL }
LibHandle := LoadLibrary('CALENDARLIB.DLL');
try
{ If the load failed, LibHandle will be zero.
If this occurs, raise an exception. }
if LibHandle = 0 then
raise EDLLLoadError.Create('Unable to Load DLL');
{ If the code makes it here, the DLL loaded successfully, now obtain
the link to the DLL's exported function so that it can be called. }
@ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');
{ If the function is imported successfully, then set lblDate.Caption to reflect
the returned date from the function. Otherwise, show the return raise
an exception. }
if not (@ShowCalendar = nil) then
lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))
else
RaiseLastWin32Error

finally
FreeLibrary(LibHandle)
// Unload the DLL.
end;
end;

end.
 
老兄就等着4K~12K不等的内存泄漏吧!
 
app2001,的方法很好
 
大家帮我看一下
Dll 工程
library ypdll;
uses
SysUtils,
Classes,
Unit_dll in 'Unit_dll.pas' {Frm_dll};
exports
BeforeCreate,
AfterFree,
Getmsg;
{$R *.res}

begin

end.
---------------------------
Dll 里一个form
unit Unit_dll;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB;

type
TFrm_dll = class(TForm)
ADOConnection1: TADOConnection;
ASPDll: TADOStoredProc;
private
{ Private declarations }

public

{ Public declarations }
end;
Function Getmsg(Atd_id:string):pchar
stdcall ;
procedure BeforeCreate
stdcall;
procedure AfterFree
stdcall;
var
Frm_dll: TFrm_dll;

implementation

{$R *.dfm}

{ TFrm_dll }



{ TFrm_dll }

function Getmsg(Atd_id: string): pchar
stdcall;
var
str:string;
begin

Frm_dll.ASPDll.Close;
Frm_dll.ASPDll.Parameters.ParamValues['@td_id']:=strtoint(Atd_id);
Frm_dll.ASPDll.Parameters.ParamValues['@td_name']:='' ;
Frm_dll.ASPDll.Parameters.ParamValues['@td_birthday']:=date() ;
Frm_dll.ASPDll.Parameters.ParamValues['@td_memeo']:='' ;
Frm_dll.ASPDll.Parameters.ParamValues['@td_type']:='' ;
Frm_dll.ASPDll.ExecProc;
str:= Frm_dll.ASPDll.Parameters.ParamValues['@td_name']+'|'+datetostr(Frm_dll.ASPDll.Parameters.ParamValues['@td_birthday'])
+ '|'+Frm_dll.ASPDll.Parameters.ParamValues['@td_memeo']+'|'+Frm_dll.ASPDll.Parameters.ParamValues['@td_type'];
result:=pchar(str);

end;

procedure BeforeCreate
stdcall;
var
Frm_dll:Tform;
begin
if assigned(Frm_dll) then exit;
Frm_dll:= TFrm_dll.Create(application) ;
Frm_dll.Hide;
end;

procedure AfterFree
stdcall;
begin
if assigned(Frm_dll) then freeandnil(Frm_dll);
end;
end.
------------------------------------------------
exe 工程
program Id;

uses
Forms,
Unit_Id in 'Unit_Id.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
------------------------------------------------------
exe form

unit Unit_Id;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject
var Action: TCloseAction);
private
{ Private declarations }

public

{ Public declarations }
end;

var
Form1: TForm1;

implementation
Function Getmsg(Atd_id:string):pchar
stdcall
external 'ypdll.dll';
procedure BeforeCreate
stdcall
external 'ypdll.dll';
procedure AfterFree
stdcall
external 'ypdll.dll';
{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
BeforeCreate;
Memo1.Clear;
Memo1.SetSelTextBuf(Getmsg(trim(Edit1.Text)));
end;

procedure TForm1.FormClose(Sender: TObject
var Action: TCloseAction);
begin
AfterFree;
end;

end.
-----------------------------------------
调函数时出错,提示:尚未调用Coinitialize

 
在DLL中需要先调用Coinitialize
 
在哪里调用
谢谢
 
呵呵,他们讲的很清楚拉
我就不说什么路过
 
library PBasicData;

{ 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,
Forms,
Unit_BasicData in 'Unit_BasicData.pas' {Form_DllBasicData};

{$R *.res}
exports
ShowBasicData;
begin
end.
//
unit Unit_BasicData;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DBCtrls, DB, ExtCtrls, ADODB, StdCtrls;

type
TForm_DllBasicData = class(TForm)
Panel1: TPanel;
Label1: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form_DllBasicData: TForm_DllBasicData;
function ShowBasicData(AHandle:THandle):Boolean;StdCall;

implementation

{$R *.dfm}
function ShowBasicData(AHandle:THandle):Boolean;
var
ShowDllBasic:TForm_DllBasicData;
begin
AHandle:=Application.Handle;
ShowDllBasic:=TForm_DllBasicData.Create(Application);
ShowDllBasic.ShowModal;
ShowDllBasic.Free;
end;
end.


 
多人接受答案了。
 
后退
顶部