11

I have my script on server, so I do not have UI interaction available and have to use DLL instead of console application.

How to call a function in C# DLL from VBScript?

How do I make my DLL to be COMVisible? Do I have to register it?

bluish
  • 26,356
  • 27
  • 122
  • 180
abatishchev
  • 98,240
  • 88
  • 296
  • 433

2 Answers2

22

You need to mark your assembly as COM visible by setting the COMVisibleAttribute to true (either at assembly level or at class level if you want to expose only a single type).

Next you register it with:

regasm /codebase MyAssembly.dll

and finally call it from VBScript:

dim myObj
Set myObj = CreateObject("MyNamespace.MyObject")
bluish
  • 26,356
  • 27
  • 122
  • 180
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • See also [Exposing .NET Components to COM](http://www.codeproject.com/Articles/3511/Exposing-NET-Components-to-COM?fid=14076&df=90&mpp=25&noise=3&prof=False&sort=Position&view=Quick&spc=Relaxed&fr=26#csharp) – bluish Oct 22 '12 at 13:46
  • Is it possible to use `Dim myObj As MyNamespace.MyObject` in VBScript as you can in VBA so you don't need `CreateObject`? Is there an explicit reference you can make? – cheezsteak May 26 '16 at 15:52
2

Yes you will need to set the ComVisible attribute to true and then register the assembly using regasm or regsvcs along with tlbexp. Then you can use Server.CreateObject and sail through.

bluish
  • 26,356
  • 27
  • 122
  • 180
danish
  • 5,550
  • 2
  • 25
  • 28