Access a custom .NET DLL in VBScript
Asked Answered
A

5

8

I wrote a DLL in .NET and I want to access it in VBScript. I don't want to add it to the assembly directory.

Is there a way to point too the DLL and create an instance of it?

Ardent answered 5/12, 2008 at 21:20 Comment(0)
J
6

You can register that .NET dll with regasm utility by specifying /codebase parameter. This parameter is not encouraged to use with unsigned assemblies but it works when you can not put your assembly into GAC.

regasm your.dll /codebase

Please note that you should not change your .dll's path after this operation since it inserts this path into the Windows registry.

Jeffjeffcoat answered 5/12, 2008 at 21:38 Comment(1)
How do you unregister once you do it?Ardent
T
13

I just had to do this myself, my findings were:

Making types visible to COM:

  1. Ensure your class is public, non-static and has a public default constructor i.e. not arguments.
  2. Ensure your method is public, non-static.
  3. Ensure you have the following set on your assembly - typically in AssemblyInfo.cs

    [assembly: ComVisible(true)]
    
  4. After building your DLL, from SDK command line run:

    regasm yourdll.dll
    

    This should respond:

    Types registered successfully

    If you get

    RegAsm: warning RA0000: No types were registered

    then you need to set ComVisible or have no public, non-static types.

From PowerShell

$a = New-Object -comobject Your.Utils.Logging
$a.WriteError2("Application", "hello",1,1)

From vbs

Set logger = CreateObject("Your.Utils.Logging")
logger.WriteError2 "Application", "hello from vbs",1,1 
Trifoliate answered 19/5, 2009 at 11:14 Comment(4)
Great answer, you included each step. Thanks a lot.Assemble
Thanks, this help a bunch. Step 3 was the one I was missing!Keniakenilworth
@Trifoliate .. Is this Server.CreateObject or CreateObjectTeilo
It doesn't work for me. Powershell says the class is not registered. Other articles mention a strong name to add to the DLL, which I also tried, for the same result.Biafra
N
7

huseyint's answer was on the money, however, I wanted to add a little to it. Here is some sample code I used for this very issue, perhaps it can speed you along...

// bind a variabe to WScript.Shell
Set WshShell = CreateObject("WScript.Shell")

// define the path to the regasm.exe file
RegAsmPath = "c:\Windows\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe"

// register the dll
WshShell.run "cmd /c " & RegAsmPath & " c:\temp\cbsecurity.dll /codebase /nologo /s", 0, True

// bind a variable to the dll
Set cbUtil = CreateObject("CBSecurity.Utilities")

I had included an IsAlive method in the dll...

Public Function IsAlive() As Boolean
    Return True
End Function

...and could check that it registered correctly using the syntax:

//check if dll is available to your code
msgbox "cbUtil is alive: " & cbUtil.IsAlive

Hope this helps someone...

Natala answered 7/2, 2009 at 16:37 Comment(0)
J
6

You can register that .NET dll with regasm utility by specifying /codebase parameter. This parameter is not encouraged to use with unsigned assemblies but it works when you can not put your assembly into GAC.

regasm your.dll /codebase

Please note that you should not change your .dll's path after this operation since it inserts this path into the Windows registry.

Jeffjeffcoat answered 5/12, 2008 at 21:38 Comment(1)
How do you unregister once you do it?Ardent
A
4

In case someone needs to debug/step-into the .Net dll that's called from VBScript only:

  1. On the .Net dll project debug setup screen, set the "start external program" by browsing to the wscript.exe program (located in C:\WINDOWS\system32\wscript.exe).

  2. On the "Command Line Arguments", set the file name and path location of the VBScript file (C:\Test\myTest.vbs). Make sure the vbs file and dll file are in the same location.

  3. Finally, in the .Net project DLL source code just set the break point and hit the "start debug"

Antichrist answered 23/8, 2010 at 20:45 Comment(0)
M
1

Not directly. You'll need a COM Callable Wrapper to any .NET library you'll calling from COM (and hence, VBScript). Therefore, you should either directly create a CCW to the DLL or you can create a CCW for a proxy DLL which provides generic methods to load a .NET DLL and provide methods for you that call the actual methods on the component and return the result. It's really not clean at all. So, in general, the answer is no.

Mondrian answered 5/12, 2008 at 21:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.