How to call a .Net DLL form a VBScript

By | May 6, 2010

You might be wondering how do you call a .Net DLL from a VB Script, well look no further as I will explain to you steps by step how do you achieve on doing this.

You might be wondering why do you want to do this?

Why not just do everything on .Net or VBScript?

Well there might be some instances this will be useful, like modifying a start-up script for GPO which is already in VBScript but you want to extend it safely (compiled codes) or just reusing available DLL’s that are out there in your organization saving you time to redevelop the same thing again. There are a lot of reasons that you want to do this but this is why I am doing it, unless someone else suggest of a better way of doing it anyways I am writing this so that if anyone needed this reference its just here.

Ok lets start with what you need. Definitely you need to develop or use a existing DLL, for this example we will develop from scratch, you also need the VBScript that you want to edit or create and that’s it.

Step 1: Your DLL. Fire up Visual Studio you can develop either in C#, VB or any language you want, this sample will be C#. You need to create a Class Library Project.

using System;
using System.Runtime.InteropServices;
namespace MyDLL
{
 [ComVisible(true)]
 public class Operations
 {
 [ComVisible(true)]
 public string getValue1(string sParameter)
 {
 switch (sParameter)
 {
 case "a":
 return "A was chosen";

 case "b":
 return "B was chosen";

 case "c":
 return "C was chosen";

 default:
 return "Other";
 }
 }
 public string getValue2()
 {
 return "From VBS String Function";
 }
 }
}

On the code above applying the ComVisibleAttribute Class controls accessibility of an individual managed type or member, or of all types within an assembly, to COM. You can apply this attribute to classes, structures, interfaces, delegates, enumerations, fields, methods, assemblies or properties. By default it is set to true but for in case you want to hide the individual type you can just set it to false (I just showed it for reference purposes, as you notice the getValue2 does not that attribute but still is still visible).

Build the project.

Step 2: Register your assembly / DLL

Now after creating this Class Library Project you have to configure it so that when you compile it will register on the system assembly. There are two ways of registering an assembly first is via command prompt. If you have .NET Framework 2.0 installed, regasm would be in following path at

C:windowsmicrosoft.netFrameworkv2.0.50727regasm.exe

And use the following for registering and unregistering an assembly

register assembly manually

The one on top

regasm /codebase c:YourClass.dll

registers it and the one on the bottom

regasm /u c:YourClass.dll

unregisters it. Now what that the /codebase option stand for?
You need to use the /codebase switch when you don’t have your assembly in GAC because that will add absolute path for your assembly in registry so that COM client can find it.

Another option is Directly to Visual Studio, this is the easy way and it’s as easy as ticking a check box in the project properties

register assembly

Now you have your DLL ready.

Step 3: Create your VBScript and here is my sample

dim myObj
Dim myClass
Set myObj = CreateObject("MyDLL.Operations")
MsgBox myObj.getValue1("a”)
MsgBox myObj.getValue2()

Now run your VBScript and that’s it.

Recommended

6 thoughts on “How to call a .Net DLL form a VBScript

  1. Pingback: Podsumowanie ciekawych materiałów z pracy | Wiadomości o technologiach IT

    1. yfy-

      You have to run your script on 32 bit command line.
      Click start, click run then type %windir%\SysWoW64\cmd.exe and enter.
      Command line should be opened in SysWoW64.
      cscript

      Reply
  2. hone for business

    Oh my goodness! Amazing article dude! Thank you, However I
    am encountering difficulties with your RSS. I don’t know why I am unable to join it. Is there anybody getting the same RSS problems? Anyone who knows the answer will you kindly respond? Thanks!!

    Reply
  3. Rony

    I have the same problem getting the message: “ActiveX component can’t create object”

    Reply
  4. Andrew Henson

    Hello, Can someone show me how I can use the CreateObject command in vbscript that will load a dll that has a windows form and then display the form to the user.

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.