在 VBScript 中访问自定义 .NET DLL

发布于 2024-07-10 01:24:57 字数 94 浏览 12 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抚笙 2024-07-17 01:25:06

不直接。 您将需要一个 COM 可调用包装器来连接您从 COM(以及 VBScript)调用的任何 .NET 库。 因此,您应该直接为 DLL 创建 CCW,也可以为代理 DLL 创建 CCW,该代理 DLL 提供加载 .NET DLL 的通用方法,并为您提供调用组件上的实际方法并返回结果的方法。 真的一点也不干净。 所以,总的来说,答案是否定的。

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.

维持三分热 2024-07-17 01:25:04

如果有人需要调试/单步调试仅从 VBScript 调用的 .Net dll:

  1. 在 .Net dll 项目调试设置屏幕上,通过浏览到 wscript.exe 程序(位于 \

  2. 在“命令行参数”中,设置 VBScript 文件的文件名和路径位置 (C:\Test\myTest.vbs)。 确保 vbs 文件和 dll 文件位于同一位置。

  3. 最后,在.Net项目DLL源代码中设置断点并点击“开始调试”

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"

过度放纵 2024-07-17 01:25:03

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

regasm your.dll /codebase

请注意,执行此操作后不应更改 .dll 的路径,因为它会将此路径插入到 Windows 注册表中。

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.

兲鉂ぱ嘚淚 2024-07-17 01:25:02

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")

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

Public Function IsAlive() As Boolean
    Return True
End Function

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

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

希望这有帮助某人...

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...

ぇ气 2024-07-17 01:25:01

我只需要自己做这件事,我的发现是:

使类型对 COM 可见:

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

    [程序集:ComVisible(true)] 
      
  4. 构建 DLL 后,从 SDK 命令行运行:

    regasm yourdll.dll 
      

    这应该响应:

    <块引用>

    类型注册成功

    如果你得到

    <块引用>

    RegAsm:警告 RA0000:未注册任何类型

    那么您需要设置 ComVisible 或没有公共非静态类型。

来自 PowerShell

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

来自 vbs

Set logger = CreateObject("Your.Utils.Logging")
logger.WriteError2 "Application", "hello from vbs",1,1 

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文