Search Tools Links Login

DLL Tutorial - MyFirstDLL


Learn how to not only create a DLL file, but also add it to another project, and call its functions, subs, and properties. This tutorial is step-by-step and very easy to understand. I made this because I noticed that all the other how-to DLL examples on PSC were pretty poor.

Original Author: Rob Loach

Code


DLL Tutorial: MyFirstDLL


Author: Rob Loach


[if !supportEmptyParas] [endif]


[if !supportEmptyParas] [endif]


In this tutorial you will learn:


[if !supportLists]?style='font:7.0pt "Times New Roman"'>    
[endif]How to make a DLL file.


[if !supportLists]?style='font:7.0pt "Times New Roman"'>    
[endif]How to call the DLL from a different project.


[if !supportLists]?style='font:7.0pt "Times New Roman"'>    
[endif]How to make a class with properties, subs, and
functions.


[if !supportEmptyParas] [endif]


Welcome


Welcome to my tutorial,
MyFirstDLL. If you read through this
tutorial, and do all the coding, it will take you about 3-10 minutes for you to
fully understand how the DLL system in VB works. If you want to do it more quickly, all the important
information is bolded
. The goal of
this tutorial is to explain step-by-step how to create and use a DLL file.


[if !supportEmptyParas] [endif]


What is a DLL?


A DLL is a file that you
can have your application use. A
programmer can use the functions in a DLL file, but the code itself cannot be
accessed
. This allows you to make
various things such as game engines.
You can then distribute the engine to the public without actually giving
out the code. DLLs are very useful
because it allows you to hold a large amount of code in only one file.


[if !supportEmptyParas] [endif]


So how do I make a DLL file?


To make a DLL, follow
these simple steps:


[if !supportLists]1)style='font:7.0pt "Times New Roman"'>  
[endif]Open
Microsoft Visual Basic.


[if !supportLists]2)style='font:7.0pt "Times New Roman"'>  
[endif]Goto
File style='mso-char-type:symbol;mso-symbol-font-family:Wingdings'>?
New Project.


[if !supportLists]3)style='font:7.0pt "Times New Roman"'>  
[endif]Start
an ActiveX DLL.


[if !supportLists]4)style='font:7.0pt "Times New Roman"'>  
[endif]This
new window is your DLL. You currently
only have one object in it, a class.
Now it is time to put in the code that you want your DLL to use.style="mso-spacerun: yes"> In this case, just add in the following
code
.


[if !supportEmptyParas] [endif]







'=====================


'Title:   MyFirstDLL


'Purpose:  Holds a text string and when the DisplayMsg


'      sub is called, it displays a message box
of


'      the string.


'      This is just an example showing how a
class


'      file works.
Now you can type CLASS and . and


'      a list of properties and subs will
appear.


'Author:  Rob Loach


'=====================


[if
!supportEmptyParas] [endif]


'Variables


'========= style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier
New"'>


Private
style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier
New"'>p_Text
As Stringstyle='color:blue'>


[if
!supportEmptyParas] [endif]


[if
!supportEmptyParas] [endif]


'Properties


'========== style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier
New"'>


Public Property Get style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"'>
Text() As Stringstyle='color:blue'>


  Text =
p_Text


End Property style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:blue'>


Public Property Let style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"'>
Text(ByVal i_Text
As String)


  p_Text
= i_Text


End Property style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:blue'>


[if
!supportEmptyParas] [endif]


[if
!supportEmptyParas] [endif]


'Functions and Subs


'================== style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier
New"'>


Public Sub style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:blue'> DisplayMsg()


  MsgBox
p_Text, vbOKOnly, "DLL Function Called"


End Sub



[if !supportEmptyParas] [endif]


[if !supportEmptyParas] [endif]


[if !supportEmptyParas] [endif]


This is your class file inside
your new DLL
. All it will do is
hold a string called p_Text. Calling
the property named Text can change the string.
When the DisplayMsg sub is called, it will make a messagebox saying the
Text string.


[if
!supportEmptyParas] [endif]


You could put any code you want
into this class. This is just an example
that we will be using. You can put
anything you want into the DLL
file (Forms, Classes, Modules, etc).


[if
!supportEmptyParas] [endif]


[if
!supportEmptyParas] [endif]


[if !supportLists]5)style='font:7.0pt "Times New Roman"'>  
[endif]Now
you have to rename your class file.
For this example, name it clsMyFirstDLL.


[if !supportLists]6)style='font:7.0pt "Times New Roman"'>  
[endif]You
can then rename your DLL to MyFirstDLL (or anything you want).style="mso-spacerun: yes"> Click on the ActiveX Icon in the top left of
the project window.  Next, in the
properties window, change the Name property to MyFirstDLL.


[if !supportLists]7)style='font:7.0pt "Times New Roman"'>  
[endif]Now,
once your done making your DLL, your going to have to save it as an actual
file. Goto File style='font-family:Wingdings;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New
Roman";mso-char-type:symbol;mso-symbol-font-family:
Wingdings'>style='mso-char-type:symbol;mso-symbol-font-family:Wingdings'>?
Make MyFirstDLL.dll
Ǫ Save it
wherever you want. Just take note of
where you put it.


[if !supportEmptyParas] [endif]


[if !supportEmptyParas] [endif]


[if !supportEmptyParas] [endif]


[if !supportEmptyParas] [endif]


[if !supportEmptyParas] [endif]


Now that I've made my DLL, how do I use it?


You can call the DLL a
number of ways. In this tutorial, I
will only show you one.


Once you have a DLL file,
and want to make use of it, do the following.


[if !supportLists]1)style='font:7.0pt "Times New Roman"'>  
[endif]Start
Microsoft Visual Basic.


[if !supportLists]2)style='font:7.0pt "Times New Roman"'>  
[endif]Start
a Standard EXE. This will be the new
project that will use the DLL.


[if !supportLists]3)style='font:7.0pt "Times New Roman"'>  
[endif]style="mso-spacerun: yes"> Goto Project style='font-family:Wingdings;mso-ascii-font-family:"Times New Roman";
mso-hansi-font-family:"Times New
Roman";mso-char-type:symbol;mso-symbol-font-family:
Wingdings'>style='mso-char-type:symbol;mso-symbol-font-family:Wingdings'>?
References
. It will take some time
to load. This is a list of DLL files
that the application is currently using.
Click on browse and load the DLL that you just made.style="mso-spacerun: yes"> It will then add the DLL to the list.style="mso-spacerun: yes"> Now click on OK. Your project has now successfully loaded the DLL information,
functions, and properties into memory.


[if !supportLists]4)style='font:7.0pt "Times New Roman"'>  
[endif]Now
is the time to use the DLL and see how the class works within the DLL.style="mso-spacerun: yes"> Make two command buttons, one named
Command1 and the other named Command2.
Next, make a textbox and name it Text1
. These are going to be used in this example.


[if !supportLists]5)style='font:7.0pt "Times New Roman"'>  
[endif]style="mso-spacerun: yes"> Now view the code of the form and put in the
following code:


[if
!supportEmptyParas] [endif]


[if
!supportEmptyParas] [endif]







'=====================================


'Title:   MyFirstDLL Application Use


'Purpose:  An application that uses the DLL that was just
made.


'      It requires a form (Form1)


'      two commands (Command1 and Command2)


'      a text box (Text1).


'Author:  Rob Loach


'=====================================


[if
!supportEmptyParas] [endif]


[if
!supportEmptyParas] [endif]


'Variables


'=========


[if
!supportEmptyParas] [endif]


'Make a variable that uses the class
in the


'MyFirstDLL DLL file so that we can
make use of it.


Dim style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:black'>MyFirstDLL As
New
style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:green'> clsMyFirstDLL style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:green'>


[if
!supportEmptyParas] [endif]


Private Sub style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:green'> Command1_Click() style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:green'> 'Set text


  
'Set the property of MyFirstDLL to text1 text


  
'This shows how to set a property of the class/DLL.


  
MyFirstDLL.Text = Text1.Text style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:green'>


End Sub


[if
!supportEmptyParas] [endif]


Private Sub style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:green'> Command2_Click() style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:green'>


  
'Call the sub DisplayMsg in the DLL.


  
'This shows how to call a sub/function of the
class/DLL.


  
MyFirstDLL.DisplayMsg style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:green'>


End Sub


[if
!supportEmptyParas] [endif]


Private Sub style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:green'> Form_Load()


  
'Initialize the Objects


  
Form1.Caption =
"MyFirstDLL"


  
Text1.Text = "Enter text to be displayed
here..."


  
Command1.Caption = "Set Text"


  
Command2.Caption = "Display Text"
style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New";
color:green'>


End Sub



[if
!supportEmptyParas] [endif]


[if
!supportEmptyParas] [endif]


[if
!supportEmptyParas] [endif]


[if !supportLists]6)style='font:7.0pt "Times New Roman"'>  
[endif]Now
run the program and play around with it.
As you can see, when you click Set Text, it sets the property ǣText in
MyFirstDLL to whatever you typed in. When
you click Display Text, it calls the sub DisplayMsg in the DLL.


[if
!supportEmptyParas] [endif]


[if !supportEmptyParas] [endif]


Quick Things To Remember


[if !supportLists]style='font-family:Symbol'>?    
[endif]To make a DLL, use ActiveX DLL project.


[if !supportLists]style='font-family:Symbol'>?    
[endif]Project style='font-family:Wingdings;mso-ascii-font-family:
"Times New Roman";mso-hansi-font-family:"Times New Roman";mso-char-type:symbol;
mso-symbol-font-family:Wingdings'>style='mso-char-type:symbol;mso-symbol-font-family:
Wingdings'>?
References


[if !supportLists]style='font-family:Symbol'>?    
[endif]Keep your DLLs in the same directory as the project.


[if !supportLists]style='font-family:Symbol'>?    
[endif]Name everything to keep organization.


[if !supportLists]style='font-family:Symbol'>?    
[endif]Make sure to use as many variables as possible in every
sub/function in your DLLs to allow diversity of programs.


[if
!supportEmptyParas] [endif]


[if
!supportEmptyParas] [endif]


Conclusion


That concludes this tutorial! In it you learned how to make a DLL and use it in a different
program. Thank you for reading MyFirstDLL
and I hope you have learned the DLL-VB concept.


[if
!supportEmptyParas] [endif]


[if
!supportEmptyParas] [endif]



About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 149 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.