I would like some info on creating DLL's in Delphi, Please?
It would be appreciated if it included simple examples such as basic mathematical functions in the DLL and using it in some Form.
Hi,
Creating a new dll is as simple as creating new Application.
To create a new dll. start your delphi if it isn't working. Select File | New and from the New tab select DLL.
Delphi will create new project without any units and pay attention to keyword Library which tells the compiler that this should be compiled as library.
Just read the comments for use of Sharemem.
Now it depends on you whether you can add already completed units in which you've implemented your functions or procedures or add new units for that purpose.
But here as i gonna show you only function i'll not add any unit, i'll just declare function here in the same dpr.
just simple example do like this
function Multiply(a,b:Integer):Integer;
begin
Result := a * b;
end;
exports
Multiply;
begin
//
end;
compile your dll in which location you want. And bingo you've made your first dll.
now i gonna show you how to use the function which we've implemented in dll i.e. simple multiply function.
Create Application in delphi and drop one button and two edit boxes.
Note:
To load dynamic link library in your applicatoin there are two possible ways (upto my knowledge) one is static linking and one is dynamic linking. here i gonna show you how to use dynamic loading of dlls.
Write the following in your button onClick event.
Var
iHandle : Integer;
ptrFunc : Function(a,b:Integer):Integer;
begin
//Replace Path\DllName.dll with the path and name of your dll
iHandle := LoadLibrary('Path\DllName.dll'); //Load libray
try
if iHandle > 0 then
begin
ptrFunc := GetProcAddress(iHandle,'Multiply');
if @ptrFunc <> nil then
ShowMessage(IntToStr(ptrFunc(StrToIntDef (Edit1.Text,0),StrToIntDef(Edit2.Text,0)));
end;
finally
FreeLibrary(iHandle); //Unload library
end;
Here i've considered the name of Editbox to Edit1 and Edit2 for a and b. Change your name to any u like and dont forget to change here.
After entering values into editboxes click on the button and a messagebox displaying the mulitplication of two values.
begin
end.
---------------------------------------
ok, the main part you need to know is that you cannot directly relate to
components on your form linking to your app in the uses clause... you have to
create a virtual Button or component until you can link it to the destination
app.
procedure AddItemToListBox(MyListBox : TListBox);
begin
MyListBox.Items.Add('hello world');
end;
exports
AddItemToListBox;
end.
---------------------------------------
You need the StdCtrls in the uses clause because this is where the components
resides in its source file... if you want to have a virtual DBGrid or something
like that or a component which doesnt reside in the StdCtrls then you should
find the right source file which does... these files can be found in the
\delphi\source\vcl\ directories...
<Note> You must always remember to add your created procedure to the exports
list... this is so your application can call upon your proc() from within the
DLL otherwise it will stay hidden.
Now you have that DLL you can start on your app that will call the procedure
from your DLL
1) Start a new app
2) Add a listbox
3) At the part where is lists something like this :
basically to summarize that... it is a standard call because, well... i dont
actually know... but it is, so believe me and it is external because it is
calling the DLL File... you have to place the DLL in your app's search path in
your project options -> Directories...
Next thing is to make the call, make an TForm1OnCreate procedure from your
object inspector and in there put this...
---------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
begin
AddItemToListBox(ListBox1);
end;
---------------------------------------
Run the app now and as long as you have the DLL where you app can find it...
i.e. in the same directory or in the search path and you have all the names
correct then it will add the item "hello world" to your listbox from your DLL...
thats basically giving you a simplistic intro to DLLs, i think the rest is self-explanitory...
if you need anymore help concerning this or anything else (providing that i can
help you with it) email me
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.