You are here
Home > Programming > Calling a .NET DLL From VBScript

Calling a .NET DLL From VBScript

Fallback Image

Visual Studio 2012 New Class Library ProjectRecently 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.

  1. It is defined as COM Visible
  2. The Program ID is defined as Test.DLL
  3. 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.
Visual Studio 2012 Sign Assembly
Select <New> under “Choose a strong name key” and give your Key file a logical name.
Visual Studio 2012 Create Strong Name Key
Once you have completed entering the settings, click on ok. Your Solution Explorer should look as follows:
Visual Studio 2012 Solution Explorer
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:WindowsMicrosoft.NETFramework64v4.0.30319) and type in the following command:
[sourcecode language=”text”]
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:

Test VBScript File

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:

Windows 8 RegEdit

Windows 8 RegEdit

Under the ProgId node, you can see the attribute value I added in C#.

Windows 8 RegEdit

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

Dirk Strauss
Dirk is a Software Developer from South Africa. He loves all things Technology and is slightly addicted to Jimi Hendrix. Apart from writing code, he also enjoys authoring books and 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 almost mastered.
https://dirkstrauss.com

Similar Articles

Top