sloppycode.net
Creating a native windows DLL
Making DLLs (native) in Delphi 5.
Home
›
Code snippets
›
Delphi
›
Creating a native windows DLL
The code below (from 2002!) shows you how to create a simple DLL that can then be reused (see the Using a DLL snippet in the Delphi section). Assuming you are using Delphi 5 (it may or may not differ for earlier versions of Delphi), select File>New and then DLL.
Delphi will then create some code for you, the most notable part of it, is the 'Libary' declaration at the top - this signals that the file is a DLL. You create procedures and functions that can then be used by the application using the DLL.
The procedures and functions that you want to export, have to be declared in the exports section of the code. You can export functions and procedures by index, we won't go into this though. An example of the exports keyword can be seen in the source code. You can also see that a global variable called DLLProc is setup to point to the dllUnload procedure - this is procedure is then responsible for doing any garbage collection when the DLL is unloaded.
library Project1; uses SysUtils, Forms, Windows, Classes; {$R *.RES} { The Forms unit is required in the 'uses' section above, for TForm to make any sense } procedure demoProcedure(TheForm: TForm); begin { MessageBox requires Windows to be declared in in the 'uses' section above. } MessageBox(TheForm.Handle,'Hello, this is from a DLL','Title of box',MB_OK Or MB_ICONEXCLAMATION); end; { Custom procedure for when the DLL is unloaded } procedure dllUnload(Reason : Integer); begin if Reason = DLL_PROCESS_DETACH then { Do some cleanup code here, freeing memory etc.} end; { Which procedures/functions are available to the calling application } exports demoProcedure; { Main body of the DLL } begin {DLLProc global variable needs to point the cleanup procedure} DLLProc := @dllUnload; end.
{Name}
Says:
{Date}
{Text}
› Home
› C#
› Snippets
› Articles
› Tools
› Taglines
› ASP
› Dictionary Object
› FSO
› Unix cheat sheet
› Gaming
› CSS
› Yak
› Umbraco
› About
› Contact
› Privacy
› Projects
› Search
› Sitemap
Buy on Amazon
Buy on Amazon
Buy on Amazon
Buy on Amazon