Use the window packaged in the DLL file
Xu Changyou
Article Summary: Write software often use the DLL file, this article uses the window packaged in the DLL file to say how DLL is in the DLL, how to call the package in the DLL, and MDI-Child load in the DLL and use
First, open Delphi to create a DLL project in the package window in the DLL, save it as usedll, generate code
library usedll; {Important note about DLL memory management: ShareMem must be thefirst unit in your library's USES clause AND your project's (selectProject-View Source) USES clause if your DLL exports any procedures orfunctions that pass strings as parameters or function results Thisapplies to. all strings passed to and from your DLL -. even those thatare nested in records and classes ShareMem is the interface unit tothe BORLNDMM.DLL shared memory manager, which must be deployed alongwith your DLL to avoid using BORLNDMM.DLL, pass string informationusing. Pchar or shortstring parameters; {$ r * .res} beginend.
Create a form, add a Label and Button, set the following:
object Form1: TForm1Left = 192Top = 133Width = 334Height = 221Caption = 'DLL' # 20013 # 20351 # 29992 # 31383 # 20307Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name = 'MS Sans Serif'Font .Style = [] OldCreateOrder = FalsePixelsPerInch = 96TextHeight = 13object Label1: TLabelLeft = 104Top = 80Width = 80Height = 13Caption = 'DLL' # 20013 # 20351 # 29992 # 31383 # 20307endobject Button1: TButtonLeft = 120Top = 152Width = 75Height = 25Caption = # 30830 # 23450taborder = 0onclick = Button1Clickendendend
Add a process: procedure loadform; export; procedure loadform; beginform1: = tform1.create (application); tryform1.showmodal; fin orfulform1.free; end;
All complete code:
library usedll; usesSysUtils, Classes, Form_Unit in 'Form_Unit.pas' {Form1}; {$ R * .res} exportsLoadForm index 1; begin end.unit Form_Unit; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; typeTForm1 = class (TForm) Label1: TLabel; Button1: TButton; procedure Button1Click (Sender: TObject); private {Private declarations} public {public declarations} end; varForm1: TForm1; ApplicationName: String; procedure LoadForm (Handle: THandle; AppName: ShortString); export; implementation {$ R * .dfm} procedure LoadForm (Handle: THandle; AppName: ShortString); beginApplication.Handle: = Handle; ApplicationName: = AppName; Form1: = TForm1.Create (Application); Tryform1.ShowModal; Finallyform1.free; end; end; procedure tForm1.button1click (sender: TOBJECT); beginself.close; end; end;
After compiling, generate the usedll.dll file, and it is completed to this DLL file.
Second, call the window in the package in DLL
Create a new project, add a button, the form layout is as follows:
object Form1: TForm1Left = 192Top = 133Width = 336Height = 222Caption = 'Form1'Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name =' MS Sans Serif'Font.Style = [] OldCreateOrder = FalsePixelsPerInch = 96Textheight = 13object Button1: TButtonLeft = 128top = 88width = 75height = 25caption = # 25171 # 24320 # 31383 # 20307taborder = 0onclick = button1clickendendend
The complete code is as follows:
unit Use_Unit; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; typeTForm1 = class (TForm) Button1: TButton; procedure Button1Click (Sender: TObject); private {Private declarations} public {Public declarations} end; varForm1: TForm1; procedure LoadForm; external 'usedll.dll' index 1; implementation {$ R * .dfm} procedure TForm1.Button1Click (Sender: TObject);. beginLoadForm; end; end three, MDI-Child in DLL is loaded and used if MDI-Child is loaded and used in the DLL, let's talk about the window from the package in the DLL file below. Create a new DLL project and save it as mdidll, then create a new form, FormStyle set fsMDIChild, as follows: object Form1: TForm1Left = 192Top = 133Width = 344Height = 234Caption = 'MDI'Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont. Height = -11font.name = 'ms Sans Serif'Font.style = [] FormStyle = falsePosition = podfaultvisible = trueonclose = formclosepixelsperinch = 96TextHeight = 13nd = 96TextHeight = 13nd
code show as below:
unit mdi_Unit; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; typeTForm1 = class (TForm) procedure FormClose (Sender: TObject; var Action: TCloseAction); private {Private declarations} public {Public declarations } MyParentForm: TForm; MyParentApplication: TApplication; end; varDllApplication: TApplication; implementation {$ R * .dfm} procedure TForm1.FormClose (Sender: TObject; var Action: TCloseAction); beginAction: = caFree; end; end.
library mdidll; usesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, mdi_Unit in 'mdi_Unit.pas' {Form1}; procedure LoadChild (ParentApplication: TApplication; ParentForm: TForm); export; stdcall; varForm1: TForm1; DllProc: Pointer; beginApplication: = parentApplication; Form1: = TForm1.Create (ParentForm); Form1.MyParentForm: = ParentForm; Form1.MyParentApplication: = parentApplication; Form1.Show; end; procedure DLLUnloadProc (Reason: Integer); register; beginif Reason = DLL_PROCESS_DETACH THEN Application: = DLLApplication; end; {$ r * .res} exportsloadChild; begindllapplication: = Application; dllproc: = @dllunloadProc; end. After compiling, generate mdidll.dll files. The MDI-Child window in the DLL is as follows:
Create a new project, the main window to set the following FormStyle fsMDIForm: object Form1: TForm1Left = 192Top = 133Width = 544Height = 375Caption = 'Form1'Color = clBtnFaceFont.Charset = DEFAULT_CHARSETFont.Color = clWindowTextFont.Height = -11Font.Name =' MS sans Serif'Font.Style = [] FormStyle = fsMDIFormMenu = MainMenu1OldCreateOrder = FalsePixelsPerInch = 96TextHeight = 13object MainMenu1: TMainMenuLeft = 72Top = 136object N1: TMenuItemCaption = # 26032 # 24314 '(& N)' OnClick = N1Clickendendend Code:
unit usemdi_Unit; interface usesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Menus; typeTForm1 = class (TForm) MainMenu1: TMainMenu; N1: TMenuItem; procedure N1Click (Sender: TObject); private {Private declarations } public {public declarations} end; T_ProvaChild = procedure (parentApplication: TApplication; ParentForm: TForm); stdcall; varForm1: TForm1; implementation {$ R * .dfm} procedure TForm1.N1Click (Sender: TObject); varDllHandle: THandle; ProcAddr : FarProc; ProvaChild: T_ProvaChild; begin DllHandle: = LoadLibrary ( 'mdidll'); ProcAddr: = GetProcAddress (DllHandle, 'LoadChild'); if ProcAddr <> nil thenbeginProvaChild: = ProcAddr; ProvaChild (Application, Self); end; end End. Conclusion: You should use Delphi to call the window in the DLL file. If you don't understand, please contact me (Home: http://yousoft.home.chinaren.com, email: YOUFT @ chinaren.com)