On 20 November, 2012 12:30:49,
by Dirk Strauss
Recently I have had to call a .NET Dll from a VBScript. Why would I need to do this I hear you ask? Well sometimes it is easier to do certain things in .NET or you might have previously written functionality in a .NET DLL that you now need to duplicate in the VBScript. That was what I was faced with. I didn’t want to have to re-write code, and I make no excuses for being lazy. So I decided to try and find a way to call a .NET DLL from a VBScript.
This turned out to be much easier than I had initially thought. In order to illustrate this, fire up Visual Studio and create a new Class Library Project.
I will be using Visual Studio 2012 on Windows 8 Pro (64 bit). The class will just be a simple email validation class that accepts an email address and returns true or false to indicate if it is a valid email address or not. Add the following code to your new class:
public class TestClass
public bool ValidateEmail(string mailAddress)
var addr = new System.Net.Mail.MailAddress(mailAddress);
You will notice a few things about this class.
It is defined as COM Visible
The Program ID is defined as Test.DLL
A GUID is specified for this DLL
Before you build the class, head on over to PROJECT (If you haven’t switched off Caps in VS 2012) and click on the Properties right at the bottom. Under the Signing Tab, check the “Sign the Assembly” check box.
Select <New> under “Choose a strong name key” and give your Key file a logical name.
Once you have completed entering the settings, click on ok. Your Solution Explorer should look as follows:
Go ahead and Build your class and navigate to the Output folder. Copy the DLL to the C:\Temp folder. (That is just my preference, but you can put the DLL in a more suitable file location.) From the Command Prompt (you might need to run this as Administrator) navigate to your .NET Framework folder (mine was C:\Windows\Microsoft.NET\Framework64\v4.0.30319) and type in the following command:
The next step is to create a VBScript file. To do this quickly, open Notepad or your favourite text editor (I prefer SciTE) and type the following:
Dim mObj, blnValid
set mObj = CreateObject("Test.DLL")
blnValid = mObj.ValidateEmail("email@example.com")
if(blnValid = true) then
msgbox "Valid Mail"
msgbox "Invalid Mail"
For the purposes of this VBScript, just Hard Code the email address. Save the file as Test.vbs in the C:\Temp folder where you placed your DLL. Take note that the VBScript calls the Test.DLL defined in the ProgId attribute in the C# code above. Now, from the command prompt, type in the following to test your VBScript:
You should see a message box pop up:
If you had to change the email address in the VBScript file to an invalid address, the DLL will return false. You can see from the example above that the .NET DLL was successfully called from the VBScript and that the email address was successfully validated. But what was that CodeBase option when we registered the DLL? Well, seeing as the DLL isn’t going into the GAC, we need to sign it in Visual Studio and because it is COM visible, we need to uniquely identify it to the Operating System so that it can be called from code like VBScript.
You will see that an entry has been added for your DLL. This is how my Registry Entries look:
Under the ProgId node, you can see the attribute value I added in C#.
Being able to call .NET Code straight from your VBScript opens up many doors for you as a developer. You can re-use components you wrote in .NET and know that they will work. You can expand the capabilities of VBScript (for example) by augmenting it with the power of .NET. This can also cut down on messy and unnecessary VBScript.
Please let me know your thoughts in the comments below.
Dirk is a Software Developer and Microsoft MVP from South Africa. He loves all things Technology and is slightly addicted to Twitter and Jimi Hendrix. Apart from writing code, he also enjoys writing human readable articles. "I love sharing knowledge and connecting with people from around the world. It's the diversity that makes life so beautiful." Dirk feels very strongly that pizza is simply not complete without Tabasco, that you can never have too much garlic, and that cooking the perfect steak is an art he has yet to master.