Delphi DLL Tutorial - Part 1

Contributed by [Mystic]
Written for mIRC 5.8 and Delphi 4 and 6

Just when you thought I left the scene

I decided to make this tutorial because I couldn't find any other Delphi tutorials around for making DLLs. Ill start with an introduction to DLLs, and why you would need them in mIRC.
Introduction
DLL's (Dynamic Link Libraries) are a great way to speed up processing information and getting information from windows that you cant get within mIRC. Anyone can make a DLL, it is very easy and simple no matter what people tell you. DLLs are small and very powerful. They are a set procedures and functions that other programs can call. So why use them with mIRC? Well, lets say you wanted to get the amount to Phyiscal Memory Windows has, you can make a simple function in a DLL to return it.
Lets Get Started
Im assuming you have Delphi 4 or 5, im not sure if this format is correct for older versions of Delphi, please email me if you have the correct syntax for older versions.

In Delphi, goto the File Menu and click New. Now select DLL. You will end up with something like this:
library Project1;

uses
  SysUtils,
  Classes;

{$R *.RES}

begin
end.
Select the menu Project then select Options. Select Directories and Conditionals. Set the output directory to your mIRC folder. When you compile your DLL it will now compile into your mIRC folder.

Ok, lets make a simple function that returns the value 'hello world'.
function helloworld(mWnd, aWnd: HWND; data, parms: PChar; show, nopause:boolean):integer; stdcall;
begin
  StrCopy(data,'Hello World!');
  Result := 3;
End;
You should already know how to make functions and procedures in Delphi. This should be inserted between uses and begin. You also have to add windows to the uses list. Eg:
uses
  Windows, SysUtils, Classes;
Ok, ill explain what this does. (Taken from the mIRC help file)

mWnd is the handle to the main mIRC window.
aWnd is the handle of the window in which the command is being issued, this might not be the currently active window if the command is being called by a remote script.
data is the information that you wish to send to the DLL. On return, the DLL can fill this variable with the command it wants mIRC to perform if any.
parms is filled by the DLL on return with parameters that it wants mIRC to use when performing the command that it returns in the data variable.
show is FALSE if the . prefix was specified to make the command quiet, or TRUE otherwise. nopause is TRUE if mIRC is in a critical routine and the DLL must not do anything that pauses processing in mIRC, eg. the DLL should not pop up a dialog.

The result part can be the following:
0 means that mIRC should /halt processing
1 means that mIRC should continue processing
2 means that it has filled the data variable with a command which it wants mIRC to perform, and has filled parms with the parameters to use, if any, when performing the command.
3 means that the DLL has filled the data variable with the result that $dll() as an identifier should return.

Ok. StrCopy(data,'Hello World!'); is copying the string Hello World into the var data. You cannot use data := 'Hello World';. Result is 3 which means you must use the $dll command in mIRC to get the data from the function.

We still have to add one more thing to our project before we can build it. We need to add the exports section. Below the function put:
Exports
  Helloworld;
This lets mIRC access the function helloworld.

Now goto the Project Menu and select Build Project. . In mIRC type //echo -a $dll(project1.dll,helloworld,NOT_USED). It should echo Hello World!. You do not need to use the 3rd parameter or the $dll command because we have no data to send to the function. If you had errors building, please goto the top and try it again. If this fails email me and ill try to help.

Congrats, you just made your first mIRC DLL function

Your unit file should look like this:
library Project1;

uses
  Windows, SysUtils, Classes;

function helloworld(mWnd, aWnd: HWND; data, parms: PChar; show, nopause:boolean):integer; stdcall;
begin
  StrCopy(data,'Hello World!');
  Result := 3;
End;

exports
  helloworld;

{$R *.RES}

begin
end.
Lets modify this abit so we can use parameters.
function helloworld(mWnd, aWnd: HWND; data, parms: PChar; show, nopause:boolean):integer; stdcall;
begin
  StrCopy(data,'/echo -a Hello World!');
  Result := 2;
End;
Build this and in mIRC type /dll project1.dll helloworld. This should return Hello World!. Notice that we changed result to 2. This tells mIRC to execute the value that is in data which is /echo -a.

Im sure you are going to think of a lot more functions. Ill post a more complex Delphi tutorial in a few weeks, stay tuned.

Any comments or suggestions feel free to send them to [email protected] Author [Mystic]
All content is copyright by mircscripts.org and cannot be used without permission. For more details, click here.