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
[if !supportEmptyParas] [endif] [if !supportEmptyParas] [endif] In this tutorial you will learn: [if !supportLists]·style='font:7.0pt "Times New Roman"'> [if !supportLists]·style='font:7.0pt "Times New Roman"'> [if !supportLists]·style='font:7.0pt "Times New Roman"'> [if !supportEmptyParas] [endif] Welcome to my tutorial, [if !supportEmptyParas] [endif] A DLL is a file that you [if !supportEmptyParas] [endif] To make a DLL, follow [if !supportLists]1)style='font:7.0pt "Times New Roman"'> [if !supportLists]2)style='font:7.0pt "Times New Roman"'> [if !supportLists]3)style='font:7.0pt "Times New Roman"'> [if !supportLists]4)style='font:7.0pt "Times New Roman"'> [if !supportEmptyParas] [endif] '===================== 'Title: MyFirstDLL 'Purpose: Holds a text string and when the DisplayMsg ' sub is called, it displays a message box ' the string. ' This is just an example showing how a ' file works. ' a list of properties and subs will 'Author: Rob Loach '===================== [if 'Variables '========= style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier Private [if [if 'Properties '========== style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier Public Property Get style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"'> Text = End Property style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"; Public Property Let style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"'> p_Text End Property style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"; [if [if 'Functions and Subs '================== style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier Public Sub style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"; MsgBox End Sub [if !supportEmptyParas] [endif] [if !supportEmptyParas] [endif] [if !supportEmptyParas] [endif] This is your class file inside [if You could put any code you want [if [if [if !supportLists]5)style='font:7.0pt "Times New Roman"'> [if !supportLists]6)style='font:7.0pt "Times New Roman"'> [if !supportLists]7)style='font:7.0pt "Times New Roman"'> [if !supportEmptyParas] [endif] [if !supportEmptyParas] [endif] [if !supportEmptyParas] [endif] [if !supportEmptyParas] [endif] [if !supportEmptyParas] [endif] You can call the DLL a Once you have a DLL file, [if !supportLists]1)style='font:7.0pt "Times New Roman"'> [if !supportLists]2)style='font:7.0pt "Times New Roman"'> [if !supportLists]3)style='font:7.0pt "Times New Roman"'> [if !supportLists]4)style='font:7.0pt "Times New Roman"'> [if !supportLists]5)style='font:7.0pt "Times New Roman"'> [if [if '===================================== 'Title: MyFirstDLL Application Use 'Purpose: An application that uses the DLL that was just ' It requires a form (Form1) ' two commands (Command1 and Command2) ' a text box (Text1). 'Author: Rob Loach '===================================== [if [if 'Variables '========= [if 'Make a variable that uses the class 'MyFirstDLL DLL file so that we can Dim style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"; [if Private Sub style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"; End Sub [if Private Sub style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"; End Sub [if Private Sub style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier New"; End Sub [if [if [if [if !supportLists]6)style='font:7.0pt "Times New Roman"'> [if [if !supportEmptyParas] [endif] [if !supportLists]style='font-family:Symbol'>· [if !supportLists]style='font-family:Symbol'>· [if !supportLists]style='font-family:Symbol'>· [if !supportLists]style='font-family:Symbol'>· [if !supportLists]style='font-family:Symbol'>· [if [if That concludes this tutorial! In it you learned how to make a DLL and use it in a different [if [if DLL Tutorial: MyFirstDLL
Author: Rob Loach
[endif]How to make a DLL file.
[endif]How to call the DLL from a different project.
[endif]How to make a class with properties, subs, and
functions.Welcome
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.What is a DLL?
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.So how do I make a DLL file?
these simple steps:
[endif]Open
Microsoft Visual Basic.
[endif]Goto
File style='mso-char-type:symbol;mso-symbol-font-family:Wingdings'>à
New Project.
[endif]Start
an ActiveX DLL.
[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.
of
class
Now you can type CLASS and . and
appear.
!supportEmptyParas] [endif]
New"'>
style='font-size:10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier
New"'>p_Text
As Stringstyle='color:blue'>
!supportEmptyParas] [endif]
!supportEmptyParas] [endif]
New"'>
Text() As Stringstyle='color:blue'>
p_Text
color:blue'>
Text(ByVal i_Text
As String)
= i_Text
color:blue'>
!supportEmptyParas] [endif]
!supportEmptyParas] [endif]
New"'>
color:blue'> DisplayMsg()
p_Text, vbOKOnly, "DLL Function Called"
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.
!supportEmptyParas] [endif]
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).
!supportEmptyParas] [endif]
!supportEmptyParas] [endif]
[endif]Now
you have to rename your class file.
For this example, name it clsMyFirstDLL.
[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.
[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.Now that I've made my DLL, how do I use it?
number of ways. In this tutorial, I
will only show you one.
and want to make use of it, do the following.
[endif]Start
Microsoft Visual Basic.
[endif]Start
a Standard EXE. This will be the new
project that will use the DLL.
[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.
[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.
[endif]style="mso-spacerun: yes"> Now view the code of the form and put in the
following code:
!supportEmptyParas] [endif]
!supportEmptyParas] [endif]
made.
!supportEmptyParas] [endif]
!supportEmptyParas] [endif]
!supportEmptyParas] [endif]
in the
make use of it.
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'>
!supportEmptyParas] [endif]
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'>
!supportEmptyParas] [endif]
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'>
!supportEmptyParas] [endif]
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'>
!supportEmptyParas] [endif]
!supportEmptyParas] [endif]
!supportEmptyParas] [endif]
[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.
!supportEmptyParas] [endif]Quick Things To Remember
[endif]To make a DLL, use ActiveX DLL project.
[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
[endif]Keep your DLLs in the same directory as the project.
[endif]Name everything to keep organization.
[endif]Make sure to use as many variables as possible in every
sub/function in your DLLs to allow diversity of programs.
!supportEmptyParas] [endif]
!supportEmptyParas] [endif]Conclusion
program. Thank you for reading MyFirstDLL
and I hope you have learned the DLL-VB concept.
!supportEmptyParas] [endif]
!supportEmptyParas] [endif]
Loading Comments ...
Comments
No comments have been added for this post.
You must be logged in to make a comment.