且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否有可能从VBScript执行.NET程序集(DLL)?

更新时间:2022-11-08 19:11:17

我想你必须首先使.NET程序集COM可见通过在AssemblyInfo.cs文件的标记有ComVisible特性的属性:

I think you must first make the .NET assembly COM-visible by including the ComVisible attribute in the AssemblyInfo.cs file:

[ComVisible(true)]

请参阅本页面MSDN上:包装的大会COM

See this page on MSDN: Packaging an Assembly for COM

然后在VBScript中,您可以通过同一个访问这些组件意味着您可以访问COM组件,即使用的CreateObject或的Server.CreateObject如:

And then in VBScript you can access those components by the same means you access COM components, i.e. using CreateObject or Server.CreateObject as in:

Set testObj = CreateObject("MyNamespace.MyType")

我想GAC甚至可能是强制性的,以从VBScript访问,但我没有带这么做的身边,所以我不知道。

I think GAC might even be mandatory to access it from VBScript but I havn't done it this way around so I'm not sure.

为什么你想这样做在VBScript?为什么不只是做一个.NET应用程序,它会做什么,你的VBScript是为了做什么?由于DLL已经在.NET上,这不应该是一个问题,对不对?

Why would you want to do that in VBScript? Why not just make a .NET console application that would do what your VBScript was meant to do? Since the DLL is already on .NET, that shouldn't be a problem, right?


编辑:另一种方式做,这可能是创建一个控制台EXE而不是一个DLL(或包装了一个DLL的EXE),您可以从VBScript调用像一个正常的可执行程序和检查返回结果。这取决于许多因素,这可能是更灵活的比保持COM code

Another way to do this might be to create a console EXE instead of a DLL (or an EXE that wraps up a DLL) that you can call from VBScript like a normal executable program and examine return results. Depending on many factors, this might be more flexible than maintain COM code.