Create And Call a DLL In VB.Net

A DLL allows you to compile a unit of your program into a useful library that can be updated separately and shared between programs. This makes it more efficient to share code between projects, faster to load programs into memory, and can be easier to maintain. Unfortunately Visual Basic .Net 2003 Standard Edition has the DLL creation feature missing, but you can easily hack your VB project to compile a DLL. Use this feature to add a DLL to an existing Visual Basic "Solution", or create a simple VB.Net DLL project which you can then include in another Visual Studio project.

The reason I'm writing this is because I've just had to split a large project into DLLs as the main program EXE was approaching 10 Mb in size and was comprised of various distinct units, so it made sense to split the project into the main program EXE and around 10 DLLs. Each of my DLLs are built in their own VB project, and all the projects are contained in the one VB.Net Solution, so it's still easy to navigate around the whole project.

I'll show you here how to create a DLL project with VB.Net, compile it into a DLL, and include that in another VB.Net project, getting around the limitation of Visual Studio Standard Edition supposedly not supporting DLL projects. :-)

Create Your DLL

Start by creating a new Windows Application in the normal way, using File, New, Project. Give it the name you want to call your DLL, e.g. "MyDLL".

Create new Visual Basic Project

Remove the "Form1.vb" form that's automatically created for you by right-clicking it in the Solution Explorer and selecting Delete. Now "Save All" from the File menu.

Remove form from VB.Net project

Now, browse the folder you saved the project in, and open MyDLL.vbproj in notepad, or your favourite text editor. You'll see a load of XML-like stuff. We'll need to change two words in here. Near the top, change OutputType from "WinExe" to "Library". Then change StartupObject from "MyDLL.Form1" to "". (Nothing). Close and save the file.

Amend the project file with notepad

Now, go back to your project and you will see a message saying the project has been modified outside the development environment. Press "Reload". Now, add a module or class to expose the functionality you want to be able to call. For example, add a module and insert the code:

Public Module MyModule

   Public Sub show_my_message()

       MessageBox.Show("Hello World")

   End Sub

End Module

Now, build your project and you will see your new DLL sitting in the "bin" folder of your project folder. It's now ready to be used in another project.

Including Your New DLL In Another Project

Lets close that project now and start another new VB project using the "Windows Application" template again. Call it "MyProgram" or something equally meaningless.

Add a reference to your DLL by right clicking References in the Solution Explorer, and selecting "Add Reference".

Add DLL reference to VB project

Click the Projects tab, and browse for the bin folder of your new DLL project. Select the DLL, then press OK and OK again and you will see your DLL as a reference in Solution Explorer.

Add a button to the form, then view your form's code. Add Imports MyDLL to the very top, and now you are able to use any functions from your new DLL. In the button's code add:

Call show_my_message()

And now run your project. Your DLL will automatically be copied in with your program EXE file when it's compiled. Hello world!

DLL called from another program

OK, a very simple, pointless example but it illustrates how easy it is to compile your own DLLs with Visual Basic Standard Edition. It's the first step I took when splitting my huge project into DLLs.

Good luck!

Page Updated 25/03/10