VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



DLL Tutorial MyFirstDLL

by Rob Loach (2 Submissions)
Category: Data Structures
Compatability: Visual Basic 5.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (3 Votes)

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 VBC were pretty poor.

Rate DLL Tutorial MyFirstDLL


DLL Tutorial: MyFirstDLL


Author: Rob Loach


[if !supportEmptyParas] [endif]


[if !supportEmptyParas] [endif]


In this tutorial you will learn:


lfo4;
tab-stops:list 54.0pt'>[if !supportLists]·style='font:7.0pt "Times New Roman"'>    
[endif]How to make a DLL file.


lfo5;
tab-stops:list 54.0pt'>[if !supportLists]·style='font:7.0pt "Times New Roman"'>    
[endif]How to call the DLL from a different project.


lfo5;
tab-stops:list 54.0pt'>[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.yes"> 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:


-18.0pt;mso-list:l2 level1 lfo2;tab-stops:list 36.0pt'>[if !supportLists]1)style='font:7.0pt "Times New Roman"'>   
[endif]Open
Microsoft Visual Basic. 


-18.0pt;mso-list:l2 level1 lfo2;tab-stops:list 36.0pt'>[if !supportLists]2)style='font:7.0pt "Times New Roman"'>   
[endif]Goto
File 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'>à
New Project.


-18.0pt;mso-list:l2 level1 lfo2;tab-stops:list 36.0pt'>[if !supportLists]3)style='font:7.0pt "Times New Roman"'>   
[endif]Start
an ActiveX DLL.


-18.0pt;mso-list:l2 level1 lfo2;tab-stops:list 36.0pt'>[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]


 #E6E6E6;border-collapse:collapse;border:none;mso-border-alt:solid windowtext .5pt;
 mso-padding-alt:0cm 5.4pt 0cm 5.4pt'>
 
  padding:0cm 5.4pt 0cm 5.4pt'>
  font-family:"Courier 
New";color:green'>'=====================


  font-family:"Courier New";color:green'>'Title: yes">  yes"> MyFirstDLL


  font-family:"Courier New";color:green'>'Purpose: yes">  Holds a text string and when the DisplayMsg


  font-family:"Courier New";color:green'>' yes">      sub is called, it displays a message box 
of


  font-family:"Courier New";color:green'>' yes">      the string.


  font-family:"Courier New";color:green'>' yes">      This is just an example showing how a 
class


  font-family:"Courier New";color:green'>' yes">      file works. 
 Now you can type CLASS and . and


  font-family:"Courier New";color:green'>' yes">      a list of properties and subs will 
appear.


  font-family:"Courier New";color:green'>'Author: yes">  Rob Loach


  font-family:"Courier 
New";color:green'>'=====================


  font-family:"Courier New";color:green'>[if 
!supportEmptyParas] [endif]


  font-family:"Courier New";color:green'>'Variables


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


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


  font-family:"Courier New"'>[if 
!supportEmptyParas] [endif]


  font-family:"Courier New"'>[if 
!supportEmptyParas] [endif]


  font-family:"Courier New";color:green'>'Properties


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


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


  font-family:"Courier New"'>  Text =
 p_Text


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


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


  font-family:"Courier New"'>  p_Text
 = i_Text


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


  font-family:"Courier New"'>[if 
!supportEmptyParas] [endif]


  font-family:"Courier New"'>[if 
!supportEmptyParas] [endif]


  font-family:"Courier New";color:green'>'Functions and Subs


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


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


  font-family:"Courier New"'>  MsgBox
 p_Text, vbOKOnly, "DLL Function Called"


  font-family:"Courier New";color:navy'>End Sub "Courier New";color:navy'>


 
 

[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]


-18.0pt;mso-list:l2 level1 lfo2;tab-stops:list 36.0pt'>[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.


-18.0pt;mso-list:l2 level1 lfo2;tab-stops:list 36.0pt'>[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.


-18.0pt;mso-list:l2 level1 lfo2;tab-stops:list 36.0pt'>[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.


-18.0pt;mso-list:l0 level1 lfo3;tab-stops:list 36.0pt'>[if !supportLists]1)style='font:7.0pt "Times New Roman"'>   
[endif]Start
Microsoft Visual Basic.


-18.0pt;mso-list:l0 level1 lfo3;tab-stops:list 36.0pt'>[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.


-18.0pt;mso-list:l0 level1 lfo3;tab-stops:list 36.0pt'>[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.yes"> Your project has now successfully loaded the DLL information,
functions, and properties into memory.


-18.0pt;mso-list:l0 level1 lfo3;tab-stops:list 36.0pt'>[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
.yes"> These are going to be used in this example.


-18.0pt;mso-list:l0 level1 lfo3;tab-stops:list 36.0pt'>[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]


 #E6E6E6;border-collapse:collapse;border:none;mso-border-alt:solid windowtext .5pt;
 mso-padding-alt:0cm 5.4pt 0cm 5.4pt'>
 
  padding:0cm 5.4pt 0cm 5.4pt'>
  font-family:"Courier 
New";color:green'>'=====================================


  font-family:"Courier New";color:green'>'Title: yes">   MyFirstDLL Application Use


  font-family:"Courier New";color:green'>'Purpose: yes">  An application that uses the DLL that was just 
made.


  font-family:"Courier New";color:green'>' yes">      It requires a form (Form1)


  font-family:"Courier New";color:green'>' yes">      two commands (Command1 and Command2)


  font-family:"Courier New";color:green'>' yes">      a text box (Text1).


  font-family:"Courier New";color:green'>'Author: yes">  Rob Loach


  font-family:"Courier 
New";color:green'>'=====================================


  font-family:"Courier New";color:green'>[if 
!supportEmptyParas] [endif]


  font-family:"Courier New";color:green'>[if 
!supportEmptyParas] [endif]


  font-family:"Courier New";color:green'>'Variables


  font-family:"Courier New";color:green'>'=========


  font-family:"Courier New";color:green'>[if 
!supportEmptyParas] [endif]


  font-family:"Courier New";color:green'>'Make a variable that uses the class
 in the


  font-family:"Courier New";color:green'>'MyFirstDLL DLL file so that we can
 make use of it.


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


  font-family:"Courier New";color:green'>[if 
!supportEmptyParas] [endif]


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


  font-family:"Courier New";color:green'>  
 'Set the property of MyFirstDLL to text1 text


  font-family:"Courier New";color:green'>  
 'This shows how to set a property of the class/DLL.


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


  font-family:"Courier New";color:navy'>End Sub 10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier 
New";color:green'>


  font-family:"Courier New";color:green'>[if 
!supportEmptyParas] [endif]


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


  font-family:"Courier New";color:green'>  
 'Call the sub DisplayMsg in the DLL.


  font-family:"Courier New";color:green'>  
 'This shows how to call a sub/function of the 
class/DLL.


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


  font-family:"Courier New";color:navy'>End Sub 10.0pt;mso-bidi-font-size:12.0pt;font-family:"Courier 
New";color:green'>


  font-family:"Courier New";color:green'>[if 
!supportEmptyParas] [endif]


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


  font-family:"Courier New";color:green'>  
 'Initialize the Objects


  font-family:"Courier New";color:green'>  
  font-family:"Courier New";color:black'>Form1.Caption = 
"MyFirstDLL"


  font-family:"Courier New";color:black'>  
 Text1.Text = "Enter text to be displayed 
here..."


  font-family:"Courier New";color:black'>  
 Command1.Caption = "Set Text"


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


  font-family:"Courier New";color:navy'>End Sub "Courier New";color:blue'>


 
 

[if 
!supportEmptyParas] [endif]


[if 
!supportEmptyParas] [endif]


[if 
!supportEmptyParas] [endif]


-18.0pt;mso-list:l0 level1 lfo3;tab-stops:list 36.0pt'>[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


-18.0pt;mso-list:l4 level1 lfo5;tab-stops:list 54.0pt'>[if !supportLists]style='font-family:Symbol'>·Roman"'>    
[endif]To make a DLL, use ActiveX DLL project.


-18.0pt;mso-list:l4 level1 lfo5;tab-stops:list 54.0pt'>[if !supportLists]style='font-family:Symbol'>·Roman"'>    
[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


-18.0pt;mso-list:l4 level1 lfo5;tab-stops:list 54.0pt'>[if !supportLists]style='font-family:Symbol'>·Roman"'>    
[endif]Keep your DLLs in the same directory as the project.


-18.0pt;mso-list:l4 level1 lfo5;tab-stops:list 54.0pt'>[if !supportLists]style='font-family:Symbol'>·Roman"'>    
[endif]Name everything to keep organization.


-18.0pt;mso-list:l4 level1 lfo5;tab-stops:list 54.0pt'>[if !supportLists]style='font-family:Symbol'>·Roman"'>    
[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!yes"> 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]



Download this snippet    Add to My Saved Code

DLL Tutorial MyFirstDLL Comments

No comments have been posted about DLL Tutorial MyFirstDLL. Why not be the first to post a comment about DLL Tutorial MyFirstDLL.

Post your comment

Subject:
Message:
0/1000 characters