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:
[sourcecode language=”csharp”]
namespace VBScriptDotNetDLL
{
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ProgId("Test.DLL")]
[System.Runtime.InteropServices.Guid("df2dac4d-ba8a-4ecc-b76e-958c1bc32f1f")]
public class TestClass
{
public bool ValidateEmail(string mailAddress)
{
try
{
var addr = new System.Net.Mail.MailAddress(mailAddress);
return true;
}
catch
{
return false;
}
}
}
}
[/sourcecode]
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:TempVBScriptDotNetDLL.dll
[/sourcecode]
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:
[sourcecode language=”vb”]
Dim mObj, blnValid
set mObj = CreateObject("Test.DLL")
blnValid = mObj.ValidateEmail("[email protected]")
if(blnValid = true) then
msgbox "Valid Mail"
else
msgbox "Invalid Mail"
end if
[/sourcecode]
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:
[sourcecode language=”vb”]
wscript test.vbs
[/sourcecode]
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
[sourcecode language=”text”]
ComputerHKEY_LOCAL_MACHINESOFTWAREClassesCLSID{Your GUID}
[/sourcecode]
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.
References:
Raymund Macaalay‘s excellent article on CodeProject