Quick Search for:  in language:    
DLL,article,will,show,code,Dynamic,Link,Libra
   Code/Articles ╗ |  Newest/Best ╗ |  Community ╗ |  Jobs ╗ |  Other ╗ |  Goto ╗ | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
Delphi Stats

 Code: 209,911. lines
 Jobs: 14. postings

 How to support the site

 
Sponsored by:

 
You are in:
 

Does your code think in ink?
Login





Latest Code Ticker for Delphi.
Record From Microphone to Wave File
By Una-Rat on 11/25


List Manager
By james hill on 11/17


Click here to see a screenshot of this code!Skin app
By Jonathan Curry on 11/15

(Screen Shot)

Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!

Affiliate Sites



 
 
   

How to code a DLL and Unit Interface

Print
Email
 

Submitted on: 6/18/2002 2:33:58 PM
By: ShadowMaster  
Level: Advanced
User Rating: By 1 Users
Compatibility:Delphi 5

Users have accessed this article 5543 times.
 
(About the author)
 
     This article will show you how to code a DLL (Dynamic Link Library) and call its method and/or functions from the unit interface.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
1) You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
First lets start a new delphi project. For the purpose of this exercise,
we will select the DLL Wizard.

2. Save the project as whatever you want, but remember the name as you
will need this for later use when you $define the library for conditional compilation.

3. add a unit to the DLL project, this is your DLL interface unit, name it something like myDLLInt.pas

4. No you are ready to code your DLL and interface.

Let start with 2 simple add/subtract functions that can be called from the DLL via the interface.
Here is the source to the actual DLL that we will use:

library adder;
uses SysUtils,Classes,adderInt;
{$R *.res}

function addNumbers(num1:integer;num2:integer):integer;stdcall;
begin
Result := num1+num2;
end;

function subNumbers(num1:integer;num2:integer):integer;stdcall;
begin
Result:=num1-num2;
end;

exports
addNumbers,subNumbers;
end.

Notice that my units uses has the adderInt specified as I named my interface unit adderInt.pas.
These are the actual functions that will be called.

NEXT: We must write an interface that will allow the calling of our DLL
functions form other applications namely C/C++ apps and of course Delphi apps.
The source code for your interface will look like this:

unit adderInt;
interface
{$IFNDEF ADDER}
function addNumbers(num1:integer;num2:integer):integer;stdcall;
function subNumbers(num1:integer;num2:integer):integer;stdcall;
{$ENDIF}
implementation
{$IFNDEF ADDER}
function addNumbers; external 'ADDER.DLL' name 'addNumbers';
function subNumbers; external 'ADDER.DLL' name 'subNumbers';
{$ENDIF}
end.

Thats it! Compile the project and now you should have a DLL called yourname.dll
To use this DLL you must do 3 things:

1. Place the yourfile.dll into the same directory as your EXE or Project you are writing.
2. Add the interface unit to your current project. eg mylibInt.pas
3. Add the interface to your uses in your project. eg. uses
sysutils,mydllint
there ya go, you just coded a DLL
Email me at support@cttech.net with any questions.


Other 6 submission(s) by this author

 

 
Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
Reason:
 
Your Vote!

What do you think of this article(in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments
1/15/2003 8:05:50 AM:
Please, give me a little concept about client/server and more database
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
5/2/2003 9:01:51 AM:
I have created the DLL. Howw do you call it from other projects?
Keep the Planet clean! If this comment was disrespectful, please report it:
Reason:

 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.

NOTICE: The author of this article has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular article, please click here.
 
Name:
Comment:

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | Delphi Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyrightę 1997 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.  Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.