在VBScript中访问自定义.NET DLL

Don*_* V. 6 .net dll vbscript dllregistration

我在.NET中编写了一个DLL,我想在VBScript中访问它.我不想将它添加到汇编目录中.

有没有办法指出DLL并创建它的实例?

foo*_*hat 12

我自己必须这样做,我的发现是:

使类型对COM可见:

  1. 确保您的类是公共的,非静态的,并且具有公共默认构造函数,即不是参数.
  2. 确保您的方法是公共的,非静态的.
  3. 确保在程序集上具有以下设置 - 通常在AssemblyInfo.cs中

    [assembly: ComVisible(true)]
    
    Run Code Online (Sandbox Code Playgroud)
  4. 构建DLL后,从SDK命令行运行:

    regasm yourdll.dll
    
    Run Code Online (Sandbox Code Playgroud)

    这应该回应:

    类型已成功注册

    如果你得到

    RegAsm:警告RA0000:没有注册任何类型

    那么你需要设置ComVisible或没有公共的非静态类型.

来自PowerShell

$a = New-Object -comobject Your.Utils.Logging
$a.WriteError2("Application", "hello",1,1)
Run Code Online (Sandbox Code Playgroud)

从vbs

Set logger = CreateObject("Your.Utils.Logging")
logger.WriteError2 "Application", "hello from vbs",1,1 
Run Code Online (Sandbox Code Playgroud)


Dsc*_*duc 6

huseyint的回答是钱,但是,我想补充一点.这是我用于这个问题的一些示例代码,也许它可以加速你...

// 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")
Run Code Online (Sandbox Code Playgroud)

我在dll中包含了一个IsAlive方法......

Public Function IsAlive() As Boolean
    Return True
End Function
Run Code Online (Sandbox Code Playgroud)

...并且可以使用以下语法检查它是否正确注册:

//check if dll is available to your code
msgbox "cbUtil is alive: " & cbUtil.IsAlive
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人......


hus*_*int 5

您可以通过指定参数向regasm实用程序注册该.NET dll /codebase.不鼓励此参数与未签名的程序集一起使用,但是当您无法将程序集放入GAC时它会起作用.

regasm your.dll /codebase
Run Code Online (Sandbox Code Playgroud)

请注意,在此操作之后,您不应更改.dll的路径,因为它将此路径插入Windows注册表.