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
regasm /codebase C:\Temp\VBScriptDotNetDLL.dll
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.
If you look in the Registry under
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.
Latest posts by Dirk Strauss (see all)
- The Daily Six Pack: November 5, 2015 - 5 November, 2015 00:10:45
- The Daily Six Pack: November 4, 2015 - 4 November, 2015 00:10:43
- Visual Studio Enterprise 2015 Reviewed - 3 November, 2015 21:49:36
- The Daily Six Pack: November 3, 2015 - 3 November, 2015 00:10:29
- The Daily Six Pack: November 2, 2015 - 2 November, 2015 00:30:40