Click here to Skip to main content
11,565,672 members (43,623 online)
Click here to Skip to main content

Tagged as

How to call a .NET DLL from a VBScript

, 12 Oct 2010 CPOL 67.1K 34
Rate this:
Please Sign up or sign in to vote.
You might be wondering how you can call a .NET DLL from a VB Script. Well, look no further, as I will explain how to achieve this in a step by step manner.

You might be wondering how you can call a .NET DLL from a VB Script. Well, look no further, as I will explain how to achieve this in a step by step manner.

You might be wondering why you want to do this?

Why not just do everything in .NET or VBScript?

Well there might be some instances where 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 reuse available DLLs 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 suggests a better way of doing it. Anyways I am writing this so that if anyone needs this reference, it's just here.

Ok let's start with what you need. Definitely you need to develop or use an existing DLL. For this example, we will develop it 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";
 }
 }
}

In the code above, apply 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 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 have that attribute, but still it is visible).

Build the project.

Now after creating this Class Library Project, you have to configure it so that when you compile it will register on the systems assembly. There are two ways of doing that; one is to a command prompt. If you have .NET Framework 2.0 installed, regasm would be in the following path at:

C:\windows\microsoft.net\Framework\v2.0.50727\regasm.exe

And use the following for registering and unregistering an assembly:

Register assembly manually

The one on top registers it and the one at the bottom unregisters it. Now what does 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 an 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 2: 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.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Raymund Macaalay
Technical Lead
New Zealand New Zealand
http://nz.linkedin.com/in/macaalay
http://macaalay.com/

You may also be interested in...

Comments and Discussions

 
QuestionCalling .Net Dll (Default Property) from VBScript Pin
Vinoth_KT5-Dec-13 0:59
memberVinoth_KT5-Dec-13 0:59 
Questionref. How to call a .NET DLL from a VBScript Pin
Member 1001963830-Apr-13 0:19
memberMember 1001963830-Apr-13 0:19 
QuestionHow to call a VBScript's function from a .NET DLL? Pin
Member 97380557-Jan-13 21:58
memberMember 97380557-Jan-13 21:58 
QuestionHow to call a .NET DLL from a VBScript Pin
Member 25560031-Oct-12 2:52
memberMember 25560031-Oct-12 2:52 
AnswerRe: How to call a .NET DLL from a VBScript Pin
TL1000S4-Oct-12 3:28
memberTL1000S4-Oct-12 3:28 
Questionactivex component can't create object Pin
Member 928428820-Jul-12 9:25
memberMember 928428820-Jul-12 9:25 
AnswerRe: activex component can't create object Pin
Member 945127417-Mar-14 6:10
memberMember 945127417-Mar-14 6:10 
AnswerRe: Consider 32 vs. 64 bit Pin
WernfriedD12-May-15 8:57
memberWernfriedD12-May-15 8:57 
GeneralGood Article Pin
Irwan Hassan19-Oct-10 19:45
memberIrwan Hassan19-Oct-10 19:45 
GeneralRe: Good Article Pin
Raymund Macaalay19-Oct-10 22:53
memberRaymund Macaalay19-Oct-10 22:53 
GeneralNice One Pin
Anurag Gandhi18-Oct-10 20:51
memberAnurag Gandhi18-Oct-10 20:51 
GeneralImproovements for your code Pin
Reinhard Ostermeier13-Oct-10 5:11
memberReinhard Ostermeier13-Oct-10 5:11 
GeneralRe: Improovements for your code Pin
Raymund Macaalay13-Oct-10 9:18
memberRaymund Macaalay13-Oct-10 9:18 
GeneralIntellisense Pin
Johan197416-Jul-10 0:22
memberJohan197416-Jul-10 0:22 
Good article,

Only thing is in VBScript i can not see the methods after the point.
How can i activate intellisense or is this not possible in vbscript.

Thanks!

Johan
GeneralRe: Intellisense Pin
Raymund Macaalay17-Jul-10 21:50
memberRaymund Macaalay17-Jul-10 21:50 
Questionother types? Pin
peterbacsi10-May-10 22:24
memberpeterbacsi10-May-10 22:24 
AnswerRe: other types? Pin
Raymund Macaalay10-May-10 23:06
memberRaymund Macaalay10-May-10 23:06 
GeneralThanks a lot Pin
Irwan Hassan10-May-10 17:29
memberIrwan Hassan10-May-10 17:29 
GeneralNice! Pin
Matt McKinney6-May-10 7:29
memberMatt McKinney6-May-10 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 12 Oct 2010
Article Copyright 2010 by Raymund Macaalay
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid