1、Delphi 包的应用英文Dynamic packages in Delphi - More articles by this author Rating: Ratings: 73 Rate itAbstract: A paper on how to create and use dynamic packages in Delphi. By Vino Rodrigues. Any discussion of advanced package use in Delphi must raise the question: Why use packages at all?Design-time pa
2、ckages simplify the tasks of distributing and installing custom components. Runtime packages, which are optional, offer several advantages over conventional programming. By compiling reused code into a runtime library, you can share it among applications. For example, all of your applications - incl
3、uding Delphi itself - can access standard components through packages. Since the applications dont have separate copies of the component library bound into their executables, the executables are much smaller-saving both system resources and hard disk storage. Moreover, packages allow faster compilat
4、ion because only code unique to the application is compiled with each build.Packages are even better when they are used dynamically. Packages offer a modular library approach to developing applications. At time those modules may become an optional entity of your application. Take for example an acco
5、unting system with an optional HR module. For some installations you will need to install just the base application - for others you will install both the base application and the HR module. This level of modularization can be easily achieved by just including the optional package to the install bas
6、e. In the past this was usually achieved with dynamically loaded DLLs, but with Delphis package technology it is easy to make your modular classes part and parcel of your application. Classes created from packages become application-owned and thus can interact with your application classes.Runtime p
7、ackages and your applicationMany developers think that Delphi packages are a place to put component - but a package can (and should) also be used to modularize an application.To show how to use packages to modularize your application we will create an example: 1. Create a new Delphi application with
8、 2 forms: Form1 and Form2. 2. Remove Form2 from the auto-created form list in the Project | Options | Forms menu dialog. 3. Drop a button on Form1 and add the following code to the OnClick event handler: 4. with TForm2.Create(Application) do5. begin6. ShowModal;7. Free;end;8. Remember to add Unit2 t
9、o Unit1s uses clause. 9. Save and run the project.We have created a simple application that shows a form with a button that shows another form when it is clicked.But what if we wanted to create Form2 in a reusable module?The answer is - PACKAGES!To create a package for Form2 we will: 1. Open the pro
10、ject manager (View | Project Manager) 2. Right-click on the Project Group and select Add New Project. 3. Select Package from the New items list. 4. You should now see the Package editor. 5. Select the Contains item and press the Add button. 6. Now use the Browse. button to select Unit2.pas. 7. The p
11、ackage should now contain the unit Unit2.pas. 8. Now save and compile the package.The package is now complete. You should have a file called package1.bpl in your Projects BPL directory. (The BPL is the Borland Package Library; the DCP is the Delphi Compiled Package - sort of like the DCU of a PAS fi
12、le.)Thats all that need to be done to the package. We now need to compile the original application with the package option switched on. 1. Select the project Project1.exe from the Project Manager by double-clicking on it. 2. Right-click and select Options. (You can also select Project | Options. fro
13、m the menu. 3. Select the Packages tab. 4. Check the Build with runtime packages check box. 5. Edit the edit-box in the Runtime packages section to read: Vcl50;Package1 and OK the options. 6. NOTE: Do not remove Unit2 from the application. 7. Save and run the application.The application will run and
14、 behave just like before - the difference can be seen in the file size. Project1.exe is now only 14K as apposed to the original 293K. If you use a resource explorer to view the contents of the EXE and the BPL you will find that both the DFM and the code for Form2 now reside in the package.Delphi ach
15、ieves this by statically linking in the package at compile time. (Thats why you shouldnt remove the unit from the EXE project.)Just think of what can be achieved by doing this: One could create data modules in packages and quickly modify their source and only distribute the new package when our data
16、-access rules have changed, like when we move from BDE based connectivity to ADO. Or, we could create a from that shows a this option is not available in this version message in one package, and then a similarly named form that has functionality in a same-named package. We will then have a Pro and E
17、nterprise version of our product without much effort.Dynamic load and unload of packagesStatically linked DLLs and BPLs work fine in most cases, but what if we decide not to deploy the BPL? We would get a The dynamic link library Package1.bpl could not be found in the specified path. error and our a
18、pplication would stop functioning. Or what if, in our modular application, we wanted to have numerous plug-in like modules?We need a way to dynamically link to the BPL at runtime.With DLLs this is a simple process of using the LoadLibrary function.function LoadLibrary( lpLibFileName: PChar): HMODULE
19、; stdcall;Once the DLL is loaded we can call exported functions and procedures within the DLL by using the GetProcAddress function.function GetProcAddress(hModule: HMODULE; lpProcName: LPCSTR): FARPROC; stdcall;We finally unload the dll by using the FreeLibrary function.function FreeLibrary(hLibModu
20、le: HMODULE): BOOL; stdcall;In this example we will dynamically load Microsofts HtmlHelp library:function TForm1.ApplicationEvents1Help( Command: Word; Data: Integer; var CallHelp: Boolean): Boolean;type TFNHtmlHelpA = function(hwndCaller: HWND; pszFile: PAnsiChar; uCommand: UINT; dwData: DWORD): HW
21、ND; stdcall;var HelpModule: HModule; HtmlHelp: TFNHtmlHelpA;begin Result := False; HelpModule := LoadLibrary(HHCTRL.OCX); if HelpModule 0 then begin HtmlHelp := GetProcAddress(HelpModule, HtmlHelpA); if HtmlHelp nil then Result := HtmlHelp(Application.Handle, PChar(Application.HelpFile), Command, Da
22、ta) 0; FreeLibrary(HelpModule); end; CallHelp := False;end;Dynamically loaded BPLsBPLs are just as simple. Well almost.We dynamically load the package by using the LoadPackage function.function LoadPackage(const Name: string): HMODULE;We create TPersistentClass of the class we wish to instantiate by
23、 using the GetClass function.function GetClass(const AClassName: string): TPersistentClass;Instantiate an object of the loaded class and use it.And when we are done, unload the package using the UnloadPackage procedure.procedure UnloadPackage(Module: HMODULE);Let us go back to our example and make a
24、 few changes:1. Select Project1.exe from the project manager. 2. Right-click and select Options. 3. Select the Packages tab. 4. Remove Package1 from the Runtime packages edit-box section and OK the options. 5. On Delphis toolbar, click on the Remove file from project button. 6. Select Unit2 | Form2
25、from the list and then OK. 7. Now go to the Unit1.pas source and remove Unit2 from its uses clause. (These steps are required to remove any link to Unit2 and the package we wish to load dynamically.) 8. Go to the source of Button1s OnClick event. 9. Add two variables of type HModule and TPersistentC
26、lass. 10. var11. PackageModule: HModule; AClass: TPersistentClass;12. Load the package Package1 by using the LoadPackage function. PackageModule := LoadPackage(Package1.bpl);13. Check that the Package Module is not 0 (zero). 14. Create a persistent class using the GetClass function, passing it the n
27、ame of the form within the package as its parameter: AClass := GetClass(TForm2);15. If the persistent class is not nil, create and use an instance of the class just a before. 16. with TComponentClass(AClass).Create(Application)17. as TCustomForm do18. begin19. ShowModal;20. Free; end;21. Finally, un
28、load the package using the UnloadPackage procedure: UnloadPackage(PackageModule);22. Save the project.Here is the complete listing of the OnClick event:procedure TForm1.Button1Click(Sender: TObject);var PackageModule: HModule; AClass: TPersistentClass;begin PackageModule := LoadPackage(Package1.bpl)
29、; if PackageModule 0 then begin AClass := GetClass(TForm2); if AClass nil then with TComponentClass(AClass).Create(Application) as TCustomForm do begin ShowModal; Free; end; UnloadPackage(PackageModule); end;end;Unfortunately thats not the end of it.The problem is that the GetClass function requires
30、 the class to be registered before the function can find it. Usually form classes and component classes that are referenced in a form declaration (instance variables) are automatically registered when the form is loaded. But the form isnt loaded yet. So where should we register the class? The answer
31、: in the package. Each unit in the package is initialized when the package is loaded and finalized when the package is unloaded.Lets return to our example and make a few changes:1. Double-click on Package1.bpl in the project manager; this will activate the package editor. 2. Click on the + symbol next to Unit2 in the Contains section. This will expand the unit tree. 3. Double-click on Unit2.pas to activate the units source code. 4. Scroll down to the end of the file and add an initialization section.
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1