Tag Archives: dll

Create a Visual Basic Dynamic-Link Library(dll) from the command line.

28 Apr

A Dynamic-Link Library is a non executable program made of one or more files, which contains classes, procedures, and/or other resources that other programs can use.  Because it is not an executable, it doesn’t need an entry point or the main() procedure  and it usually ends with .dll

Well, you can read more about Dynamic link libraries(dlls) on msdn or wikipedia.

In this post I’ll show you how to create a VB.Net DLL from the command line.

First you need to set up your system to use the vbc compiler. If you do not know how to do this, please read my previous post here.  So from now on I’ll assume you have set up your compiler and we are ready to go.

Steps:

  • Open notepad,  create a class, call it Person and save it as a VB file (Person.vb) . For this post we’ll create a simple Person class.  See the code for the class at the end of the post below.

>>CodeListing No.1 – The Person Class

  • Create a new folder in your main drive (in my case C:\) and call it PersonLibrary.  Save the above class in this folder.
  • Click Start > Run > type cmd  and click OK to open the command prompt.
  • Change to the PersonLibrary directory as shown in the image below.

changedirectory

How to Create a Dynamic-Link Library from the command line.

  1. There are two options. The first one is you create a library that has the same name as the source file.
  • To create a Dll that has the same name as the source file, at the command prompt type the following command.

vbc /target:library Person.vb

  • The command is vbc /target:library nameOfSourceFile.vb notice the space after vbc and the other space after library. Make sure you include them.
  • Browse to the PersonLibrary folder and you’ll see that the Person.dll dynamic-link library has been created. See the image below.

personlibrary1

2.  The other option is that you can provide your own name for the library.

  • To create a Dll that has your own desired name, at the command prompt type the following command.

vbc /target:library /out:PersonLibrary.dll Person.vb

  • The command is vbc /target:library /out:DesiredNameOfLibrary.dll nameOfSourceFile.vb
  • Now browse to the PersonLibrary folder and you’ll see the newly created dll with our custom name PersonLibrary.dll, as well as the old one, Person.dll

personlibrary2

And thats all there is to it. You are done and you have your libraries and you can distribute them now or use them from an application.

How to use the Library.

Before you use the library you simply have to import it into your project and add it as a reference using the command line when compiling.

  • Open Notepad and type in the following code
Imports System
Imports PersonLibrary

Module MainModule
    Sub Main()
        Dim p As New Person("Mike F", "Masao")
        p.Addresses(0) = "6032, Olacity, Arusha"
        p.Addresses(1) = "Tanzania"
        p.Citizenship = "Tanzanian"
        p.BirthDate = #8/21/1982#

        Console.WriteLine("Full Name: " & p.FullName("Mr."))
        Console.WriteLine("Address1: " & p.Addresses(0))
        Console.WriteLine("Address2: " & p.Addresses(1))
        Console.WriteLine("Citizenship: " & p.Citizenship)
        Console.WriteLine("Age: " & p.Age)

        Console.Read()
    End Sub

End Module
  • Save the file as MainProgram.vb under our PersonLibrary folder.
  • Now go back to the command prompt and issue the following command, which will add the library as a reference to the project and then compile the MainProgram code to an executable.

vbc /reference:PersonLibrary.dll MainProgram.vb

  • The command is vbc /reference:NameOfLibrary.dll NameOfMainProgram.vb
  • After the above steps, browse again to the PersonLibrary folder and you’ll see your libraries and your MainProgram.exe executable file as seen in the image below.

aftercompiling

  • Now double click the MainProgram.exe file and you should see the following output on your screen.

output

And we are done.

So now you can create your own libraries by using just notepad and the command prompt.

Hope this was helpful. Till next time, yours truly.

CodeListing No.1  – The Person Class

Public Class Person
    'Fields
    Public FirstName As String
    Public LastName As String
    Public m_BirthDate As Date
    'You can define up to 4 addresses for this person
    Public m_Addresses(3) As String
    Public Citizenship As String = ""

    Sub New(ByVal firstname As String, ByVal lastname As String)
        Me.FirstName = firstname
        Me.LastName = lastname
    End Sub
    Public Property BirthDate() As Date
        Get
            Return m_BirthDate
        End Get
        Set(ByVal value As Date)
            m_BirthDate = value
        End Set
    End Property

    Function FullName(Optional ByVal title As String = "") As String
        FullName = ""
        'use the title if provided
        If title <> "" Then FullName = title & " "
        'append first and last names
        FullName &= FirstName & " " & LastName
    End Function

    'Age is a ReadOnly property
    Public ReadOnly Property Age() As Integer
        Get
            Return Year(Now) - Year(m_BirthDate)
        End Get
    End Property

    Public Property Addresses(ByVal index As Integer) As String
        Get
            If index < 0 Or index > UBound(m_Addresses) Then
                Throw New IndexOutOfRangeException("Invalid address Index")
            End If
            Return m_Addresses(index)
        End Get
        Set(ByVal value As String)
            If index < 0 Or index > UBound(m_Addresses) Then
                Throw New IndexOutOfRangeException("Invalid address Index")
            End If
            m_Addresses(index) = value
        End Set
    End Property

End Class
Follow

Get every new post delivered to your Inbox.