Creating a DLL (Dynamic Link Library) file in Delphi allows you to encapsulate reusable code and share it across multiple applications. Here’s a step-by-step guide on how to create a DLL in Delphi and use it in your Delphi programming projects:
Step 1: Creating the DLL Project
- Launch Delphi and go to “File” -> “New” -> “Other” -> “Delphi Projects” -> “DLL” to create a new DLL project.
- Choose a name and location for your project, and click “OK” to create it.
Step 2: Writing the DLL Code
- In the Code Editor window, you’ll see a unit file with some default code. Replace it with your own code or write the functionality you want to encapsulate in the DLL.
- Make sure to export the functions or procedures you want to use in other applications by adding the “exports” directive before each procedure or function declaration. Example:
library MyDLL;
uses
System.SysUtils,
System.Classes;
{$R *.res}
function AddNumbers(a, b: Integer): Integer; stdcall; export;
begin
Result := a + b;
end;
exports
AddNumbers;
begin
end.
Step 3: Building the DLL
- Go to “Project” -> “Build MyDLL” to compile and build the DLL.
- After a successful build, you should have a DLL file generated in the project’s output directory.
Step 4: Using the DLL in a Delphi Project
- Open or create a new Delphi project where you want to use the DLL.
- Go to “Project” -> “Options” -> “Directories/Conditionals” -> “Library” and add the directory path where the DLL file is located.
- Add the following code to the “uses” section of your project file to import the functions from the DLL:
uses
…, MyDLL;
Now you can call the exported functions from the DLL in your Delphi project, as if they were regular functions. Example:
procedure TForm1.Button1Click(Sender: TObject);
var
result: Integer;
begin
result := AddNumbers(2, 3);
ShowMessage(‘The result is: ‘ + IntToStr(result));
end;
How to create a DLL for a Windows service and use it in Delphi:
Step 1: Creating the DLL Project
- Launch Delphi and go to “File” -> “New” -> “Other” -> “Delphi Projects” -> “DLL” to create a new DLL project.
- Choose a name and location for your project, and click “OK” to create it.
Step 2: Writing the DLL Code
In the Code Editor window, replace the default code with the following:
library MyServiceDLL;
uses
System.SysUtils,
System.Classes,
Winapi.Windows,
Winapi.Messages;
{$R *.res}
procedure ServiceMain(NumServicesArgs: Integer; ServiceArgVectors: LPTSTR); stdcall;
var
StatusHandle: THandle;
ServiceStatus: TServiceStatus;
begin
// Set initial service status
ServiceStatus.dwServiceType := SERVICEWIN32OWN_PROCESS;
ServiceStatus.dwCurrentState := SERVICESTARTPENDING;
ServiceStatus.dwControlsAccepted := SERVICEACCEPTSTOP or SERVICEACCEPTSHUTDOWN;
ServiceStatus.dwWin32ExitCode := 0;
ServiceStatus.dwServiceSpecificExitCode := 0;
ServiceStatus.dwCheckPoint := 0;
ServiceStatus.dwWaitHint := 0;
// Register the service control handler
StatusHandle := RegisterServiceCtrlHandler(”, @ServiceHandler);
if StatusHandle = 0 then
Exit;
// Tell the service controller that the service is starting
ServiceStatus.dwCurrentState := SERVICE_RUNNING;
SetServiceStatus(StatusHandle, ServiceStatus);
// Perform service tasks here
// Tell the service controller that the service has stopped
ServiceStatus.dwCurrentState := SERVICE_STOPPED;
SetServiceStatus(StatusHandle, ServiceStatus);
end;
procedure ServiceHandler(Control: DWORD); stdcall;
begin
case Control of
SERVICECONTROLSTOP,
SERVICECONTROLSHUTDOWN:
// Perform cleanup or stop operations here
// Set the service status to stopped
ServiceStatus.dwCurrentState := SERVICE_STOPPED;
end;
SetServiceStatus(StatusHandle, ServiceStatus);
end;
exports
ServiceMain;
begin
end.
Step 3: Building the DLL
- Go to “Project” -> “Build MyServiceDLL” to compile and build the DLL.
- After a successful build, you should have a DLL file generated in the project’s output directory.
Step 4: Using the DLL in a Delphi Project
- Open or create a new Delphi project where you want to use the DLL.
- Go to “Project” -> “Options” -> “Directories/Conditionals” -> “Library” and add the directory path where the DLL file is located.
- Add the following code to the “uses” section of your project file to import the functions from the DLL:
uses
…, MyServiceDLL;
- Now you can call the exported
ServiceMain
function from the DLL in your Delphi project, as if it were a regular function.Example:procedure TForm1.Button1Click(Sender: TObject);beginServiceMain(0, nil); // Call the ServiceMain function from the DLLend;
Note that this is a simplified example that demonstrates the basic structure of a Windows service DLL. In a real-world scenario, you would typically add more functionality and handle service-specific tasks within the ServiceMain
procedure.
Remember to register and install the service using appropriate tools like sc.exe
or third-party service manager applications.
That’s it! You have created a DLL in Delphi and successfully used it in your Delphi project. Remember to distribute the DLL file along with your application if you plan to run it on other machines.
Happy Coding!