Creating and Using Dynamic Link Libraries

Use a DLL to Reuse Code in Different Programs and Languages

© Mark Alexander Bain

Aug 25, 2009
Creating and Using Dynamic Link Libraries, Mark Alexander Bain
A Visual Basic Programmer can create a Dynamic Link Library that allows them not just to reuse code across applications, but to reuse code across programming languages.

Any good programmer will try to reuse their code as much as possible. Just how this is done will depend on the programming language being used. Some programming languages use modules, some will use packages and others will use libraries.

They can normally only be used by applications written in the same language as the original code. However, the Visual Basic programmer can produce libraries that can be used by any .NET framework language. They can do this by creating Dynamic Link Libraries (or DLLs) with Microsoft Visual Basic 2008 Express Edition.

Creating a DLL with Microsoft Visual Basic 2008 Express Edition

Microsoft Visual Basic 2008 Express Edition is freely available from the Microsoft web site. It does require registration within 30 days of installation, but apart from that the programmer is free to use the application as they wish. In this case it's to create a custom DLL (as shown in figure 1 at the bottom of this article).

The DLL itself consists of a class that can be used in any .NET framework based application. In this example the class will be able to:

  • return the monthly payment due on a mortgage for a given loan amount, interest rate and repayment period
  • return the loan available for a set monthly payment

The formulae used are discussed fully in A Simple Home Mortgage Calculator, and the class will look like:

Public Class Mortgage
Function monthly_payment(ByVal amount As Double, _
ByVal interest As Double, _
ByVal years As Integer) As Double
Dim payments As Integer
Dim percentage As Double
payments = years * 12 'Total number of months
percentage = interest / 100 / 12 'Montly interest rate
monthly_payment = _
amount * (percentage * (1 + percentage) ^ payments) / _
((1 + percentage) ^ payments - 1)
End Function
Function Loan(ByVal mp As Double, _
ByVal interest As Double, _
ByVal years As Integer) As Double
Dim payments As Integer
Dim percentage As Double
payments = years * 12 'Total number of months
percentage = interest / 100 / 12 'Montly interest rate
Loan = _
mp / ((percentage * (1 + percentage) ^ payments) / _
((1 + percentage) ^ payments - 1))
End Function
End Class

Once the programmer has built the DLL then it will be available for any applications that they build, regardless of the .NET framework language used.

Using the Custom DLL in a C# Application

The DLL contains the mortgage calculation functionality. That functionality has been written in Visual Basic it can be added as a reference and then used, for example, in a C# application.

using System;
using Finance;

A new object can then be created from the mortgage class:

namespace mortgage
{
class Program
{
public static void Main(string[] args)
{
Mortgage mortgage = new Finance.Mortgage();

And the the Visual Basic functions can be accessed from the C# code (as shown in figure 2):

Console.Write("Enter Loan amount: ");
double loan = Convert.ToDouble( Console.ReadLine());
Console.Write("Enter Interest Rate: ");
double interest = Convert.ToDouble( Console.ReadLine());
Console.Write("Enter Loan Period: ");
int years = Convert.ToInt16( Console.ReadLine());
Console.WriteLine(mortgage.monthly_payment(loan, interest, years));
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}

And the DLL can even be accessed from the command line.

Using the Custom DLL in a PowerShell Application

The DLL can also be used as part of a PowerShell script (see How to Write a PowerShell Script). The process is very simple. First the DLL must be loaded:

[Reflection.Assembly]::LoadFile("C:\dlls\\Finance.dll")

A mortgage object is then created:

$Mortgage = new-object Finance.Mortgage

And the functions can be used in the script.

Write-Host $Mortgage.monthly_payment(100000, 5, 25)

This can be seen in operation in figure 3 and shows just how easy it is for a Visual Basic programmer to create custom DLL that can be used in any .NET framework application.


The copyright of the article Creating and Using Dynamic Link Libraries in Computer Programming is owned by Mark Alexander Bain. Permission to republish Creating and Using Dynamic Link Libraries in print or online must be granted by the author in writing.


Creating and Using Dynamic Link Libraries, Mark Alexander Bain
Figure 1: Creating a DLL, Mark Alexander Bain
Figure 2: Using the DLL, Mark Alexander Bain
Figure 3: Using the DLL with PowerShell, Mark Alexander Bain
 


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo