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
public class TestClass
public bool ValidateEmail(string mailAddress)
var addr = new System.Net.Mail.MailAddress(mailAddress);
return true;
return false;


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

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"
msgbox "Invalid Mail"
end if


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


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”]



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.


Raymund Macaalay‘s excellent article on CodeProject

Dirk Strauss
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.

Similar Articles

  • Pingback: Interesting .NET Links - November 20 , 2012 | TechBlog()

  • shingu

    Thank you!

    • Dirk Strauss

      You are welcome 🙂

  • disqus_00YDCZxqDV

    If you’re in 64 bit windows, you have to use the 32 bit version of CSCRIPT/WSCRIPT. You can start those by doing Start>Run and typing


    Then do CSCRIPT from there. Otherwise you get “activeX Component can’t create object”

  • RV

    Hi Dirk

    I have gone through this post and find its working but when I referenced other library into VBScriptDotnetDLL project and called the method then getting error “VBScripttDotnetDLL error ‘80070002’

    Could not load file or assembly
    ‘HtmlSanitizationLibrary, Version=, Culture=neutral,
    PublicKeyToken=d127efab8a9c114f’ or one of its dependencies. The system cannot
    find the file specified.”

    Please help me to know the solution of this.

    • Hello RV. Try going through the signing process again after you have referenced your other library. If that doesn’t work, perhaps the library you’re referencing is the issue here and also need to be COM visible and signed?

  • RV

    Thanks Dirk for your quick response.

    For solve this problem I have put my referencing library into GAC and its working. The solution you have proposed will try and let you know the out come.

    Thanks Again for this post.

  • Andy Osvath

    Awesome! Very straight forward and easy. You saved me a lot of time. Thanks.

  • AlexB

    have to use a given .NET 2.0 assembly.
    vBScript is the only thing I am alowwed to use in the given production environment (No Developer Tool allowed to install)
    So I wonder how I am able to figure out if the given assambly has all the properties which are basic to succseed in using it.
    Registration is all done, launched from Sysow64 but still getting Runtime-Errormessage
    Any hints
    Thanks for your help in advance
    take care

    • Hi Alex. What is the error message that you are receiving?