Accessing a Custom .NET DLL in VBScript

I have written a DLL in .NET and I want to access it in VBScript. I do not want to add it to the assembly directory.

Is there a way to specify a DLL and instantiate it?

+6
dll vbscript
source share
5 answers

You can register this DLL.NET with the regasm utility by specifying the /codebase parameter. This option is not recommended for unsigned assemblies, but it works when you cannot put your assembly in the GAC.

 regasm your.dll /codebase 

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

+5
source share

I just had to do it myself, my conclusions were:

Creating types visible to COM:

  • Make sure your class is public, non-static and has an open default constructor, i.e. not arguments.
  • Make sure your method is public, non-static.
  • Make sure the following set is installed on your assembly: usually in AssemblyInfo.cs

     [assembly: ComVisible(true)] 
  • After creating your DLL from the SDK command line, do:

     regasm yourdll.dll 

    This should answer:

    Registered Types Successfully

    If you get

    RegAsm: warning RA0000: not a single type was registered

    then you need to install ComVisible or not have 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 
+12
source share

The answer to my vote was for money, but I wanted to add a little to it. Here is an example of the code that I used for this very problem, maybe it can speed you up ...

 // 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 have included the IsAlive method in the dll ...

 Public Function IsAlive() As Boolean Return True End Function 

... and can verify that it is correctly registered using the syntax:

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

Hope this helps someone ...

+6
source share

In case someone has to debug / embed a .Net dll that is called only from VBScript:

  • On the debug setup screen of the .Net dll project, install โ€œrun external programโ€ by looking at the wscript.exe program (located in C: \ WINDOWS \ system32 \ wscript.exe).

  • In the "Command Line Arguments", specify the file name and path to the VBScript file (C: \ Test \ myTest.vbs). Make sure the vbs and dll files are in the same place.

  • Finally, in the source code of the .Net project DLL, just set a breakpoint and click "start debugging"

+4
source share

Not directly. You will need a COM-Callable Wrapper for any .NET library that you call from COM (and therefore VBScript). Therefore, you must either create CCW for the DLL, or create CCW for the proxy DLL, which provides common methods for loading the .NET DLL and provides methods for you that call the actual methods of the component and return the result. It really is not clean. So, in general, the answer is no.

+1
source share

All Articles