C# 在 VBScript 中使用 DLL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9839704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using DLLs in VBScript
提问by steventnorris
I've compiled C# code into a DLL, but have little experience with them. My C# code contains a class HelloWorldwith a static method Print(). I'd like to use this DLL in VBScript to call the method Print(). I know this is base, but I'm using this as a test for a larger scale project that will be compiled to DLL in the end. What's the declare look like for that and how would the method call look?
我已经将 C# 代码编译成一个 DLL,但对它们的经验很少。我的 C# 代码包含一个HelloWorld带有静态方法的类Print()。我想在 VBScript 中使用这个 DLL 来调用方法Print()。我知道这是基础,但我将其用作对最终将被编译为 DLL 的更大规模项目的测试。声明看起来像什么,方法调用看起来如何?
采纳答案by Nilpo
If your dll is registered with the system, use CreateObjectwith it's ProgID.
如果您的 dll 已在系统中注册,请使用CreateObject它的 ProgID。
Set myObject = CreateObject("MyReallyCoolObject.HelloWorld")
myObject.Print
If your object is not registered on the system, use GetObjectwith a path to the file containing your object. Make sure your object exposes the proper interface. (The second parameter is optional. Here you can provide a class name if your object exposes more than one.)
如果您的对象未在系统上注册,请使用GetObject包含您的对象的文件的路径。确保您的对象公开了正确的接口。(第二个参数是可选的。如果您的对象公开了多个,您可以在这里提供一个类名。)
Set myObject = GetObject("C:\some\path\helloworld.dll", "appname.HelloWorld")
myObject.Print
回答by Cheran Shunmugavel
I think you might be looking for Registration-Free COM. This SO answer regarding the Microsoft.Windows.ActCtxshould help specifically for VBScript.
我想您可能正在寻找免注册 COM。这个关于Microsoft.Windows.ActCtx 的SO 答案应该特别适用于 VBScript。
Keep in mind that COM doesn't support static methods, so you'll have to make your Print method into an instance method.
请记住,COM 不支持静态方法,因此您必须将 Print 方法转换为实例方法。

