Click here to Skip to main content
11,575,124 members (66,930 online)
Click here to Skip to main content

Create a DLL without Visual Studio

, 16 Jul 2007 28.1K 313 16
Rate this:
Please Sign up or sign in to vote.
An article on creating your own DLLs for use in your .NET projects

Introduction

This article is aimed primarily at users creating ASP.NET web pages without Visual Studio -- perhaps using only Notepad and having their code-behind inline on the same page -- but who want to create a DLL with some global (public) functions that they can reference from any page. It is also, perhaps, a simple introduction to what DLLs are and how to use them. This article deals with VB.NET, but the same principles apply for other .NET languages.

There is no reason why such a DLL cannot be incorporated into a Visual Studio project too of course, but anyone using that has more direct methods of creating DLLs. Once created, they are treated like any other DLL, i.e. they should be placed in the application's /bin folder and referenced in the usual manner. This is a good start towards building up and keeping a library of useful functions that can be used in all your applications. Furthermore, being pre-compiled, functions within the DLL should execute faster than having them simply included in your page's inline code.

Using the code

The ZIP file contains a small application, vbc.exe, that is in fact probably already on your computer. The one in the ZIP file is in windows/Microsoft.NET/Framework/v1.1.4322. DLLs so created can also be used in higher versions of the framework, subject to whatever compatibility issues exist between frameworks. This is to say, they'll usually work just fine! However, a .NET 2.0 version exists in the corresponding folder for that framework, so if you particularly want to target that instead, find and use it in place of the one in the ZIP file.

I found it helpful to create a new folder in a more accessible location, /inetpub/vbc/, and copied the file there. This is a command-line executable, so to run it you need to open a Command Prompt window and navigate to this folder. To ease this task, the ZIP file also contains a sample batch (BAT) file that will take you straight there. You may need to amend this to suit your needs; it's simple enough to work out. So, the steps are:

  1. Create a new folder in inetpub called "vbc"
  2. Copy the file vbc.exe into it
  3. Open a Command Prompt window and note the default folder it opens in
  4. Copy the file vbc.bat into that folder, having amended it if necessary

Before we can actually make use of all this, we need to create the source file for our DLL! We will create a small DLL containing a public function that will return a string greeting a name passed in when called. Open a new blank text file and save it to the same vbc folder as above with a .vb extension, say myfirstdll.vb. Then add the following:

Imports System
Imports System.Data

Namespace MyNameSpace1
  
    Public Class MyClass1

        Public Function Hello(ByVal strName As String) As String
            If strName.Trim = "" Then 
               Return "Hello! Who the @!*# are you?"
            Else
               Return "Hello " & strName & "!"
            End If
        End Function
        
    End Class
        
End Namespace

In fact, the imports are not necessary in this example, but are there as an example of how to import classes, which you will need to do for more realistic examples than this. You can, of course, add as many functions as you wish. Now open your command prompt window and type in vbc if you have copied and configured the batch file. Otherwise, navigate manually to the vbc folder. Next, type and enter:

vbc.exe /t:library /out:myfirstdll.dll /r:system.dll 
    /r:system.data.dll myfirstdll.vb

Notes about this:

  1. vbc.exe /t:library - always use
  2. /out:myfirstdll.dll - /out: followed by the name of the DLL you want to create
  3. /r:system.dll /r:system.data.dll - list of DLLs imported preceded by /r:
  4. myfirstdll.vb - your source file

Note point 3: knowing which classes to import and which DLLs are needed here is a matter of experience. Common useful ones might be:

  • System.Data
  • System.Data.SqlClient
  • System.XML
  • System.Text

These can be referenced in your vbc call accordingly. If you may want to include Data.SqlClient in your list of imports to make your code easier, you should only reference /r:system.data.dll in your vbc call. There is no system.data.sqlclient.dll. Take a look in the windows/Microsoft.NET/Framework/v1.1.4322 folder where the original vbc.exe file resides; you can reference any DLL in there. You should now see myfirstdll.dll in your vbc folder. Copy this over to your application's /bin folder and it is ready for use. Making a call to the Hello function is now a simple matter of referencing the DLL in your ASPX page. For example:

<%@ import Namespace="MyNameSpace1" %>

<script runat="server">
    Private Sub Page_Load(ByVal sender As System.Object, 
        ByVal e As System.EventArgs) 
        Dim myFunction As New MyNameSpace1.MyClass1
        litHello.Text = myFunction.Hello("World")           
    End Sub
</script>

<html>
    <body>
        <asp:Literal runat="server" id="litHello" />
    </body>
</html>

The output of this page will be a simple Hello World! message. That's it! Happy coding. -Fred

History

  • 16 July, 2007 -- Original version posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Fred_Smith
Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionGetting Frustrated Pin
Member 108630983-Jun-14 21:23
memberMember 108630983-Jun-14 21:23 
QuestionI'm having problems with the above... Pin
Brian Kessler11-Sep-09 4:31
memberBrian Kessler11-Sep-09 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 16 Jul 2007
Article Copyright 2007 by Fred_Smith
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid