article

DLL Tutorial - MyFirstDLL

Email
Submitted on: 2/4/2015 2:02:00 PM
By: Rob Loach (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: VB 5.0, VB 6.0
Views: 972
 
     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.

 
				

DLL Tutorial: MyFirstDLL

Author: Rob Loach

[if !supportEmptyParas] [endif]

[if !supportEmptyParas] [endif]

In this tutorial you will learn:

[if !supportLists] [endif]How to make a DLL file.

[if !supportLists] [endif]How to call the DLL from a different project.

[if !supportLists] [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) [endif]Open Microsoft Visual Basic.

[if !supportLists]2) [endif]Goto File New Project.

[if !supportLists]3) [endif]Start an ActiveX DLL.

[if !supportLists]4) [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. 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

'=========

Private p_Text As String

[if !supportEmptyParas] [endif]

[if !supportEmptyParas] [endif]

'Properties

'==========

Public Property Get Text() As String

Text = p_Text

End Property

Public Property Let Text(ByVal i_Text As String)

p_Text = i_Text

End Property

[if !supportEmptyParas] [endif]

[if !supportEmptyParas] [endif]

'Functions and Subs

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

Public Sub 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) [endif]Now you have to rename your class file. For this example, name it clsMyFirstDLL.

[if !supportLists]6) [endif]You can then rename your DLL to MyFirstDLL (or anything you want). 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) [endif]Now, once your done making your DLL, your going to have to save it as an actual file. Goto File 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 Ive 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) [endif]Start Microsoft Visual Basic.

[if !supportLists]2) [endif]Start a Standard EXE. This will be the new project that will use the DLL.

[if !supportLists]3) [endif] Goto Project 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. It will then add the DLL to the list. Now click on OK. Your project has now successfully loaded the DLL information, functions, and properties into memory.

[if !supportLists]4) [endif]Now is the time to use the DLL and see how the class works within the DLL. 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) [endif] 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 MyFirstDLL As New clsMyFirstDLL

[if !supportEmptyParas] [endif]

Private Sub Command1_Click() '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

End Sub

[if !supportEmptyParas] [endif]

Private Sub Command2_Click()

'Call the sub DisplayMsg in the DLL.

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

MyFirstDLL.DisplayMsg

End Sub

[if !supportEmptyParas] [endif]

Private Sub Form_Load()

'Initialize the Objects

Form1.Caption = "MyFirstDLL"

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

Command1.Caption = "Set Text"

Command2.Caption = "Display Text"

End Sub

[if !supportEmptyParas] [endif]

[if !supportEmptyParas] [endif]

[if !supportEmptyParas] [endif]

[if !supportLists]6) [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] [endif]To make a DLL, use ActiveX DLL project.

[if !supportLists] [endif]Project References

[if !supportLists] [endif]Keep your DLLs in the same directory as the project.

[if !supportLists] [endif]Name everything to keep organization.

[if !supportLists] [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]


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate 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

6/28/2016 11:18:12 AMDavid

This method of instruction by text in a box, is so unworthy of the good code on the site. have had to copy and reformat this, before I could read it and understand it.

What does this mean - [if
!supportEmptyParas] [endif] there are so many, I just removed them. and all the other paragraph with this rubbish in front.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.