我有一个控制台应用程序myapp.exe。在app中是一个函数,我们称之为:
public static int AddIntegers(int a, int b)
是否可以在外部看到此函数,以便VBscript可以调用它?我是否必须将该函数移动到DLL中,还是可以将其保留在EXE中并使其可见?如果是这样,怎么样?
答案 0 :(得分:3)
理想情况下,您应该创建一个DLL并在需要公开的函数上设置Com Visible。
using System;
using System.Runtime.InteropServices;
namespace MyDLL
{
[ComVisible(true)]
public class Operations
{
[ComVisible(true)]
public int AddIntegers(int a, int b)
{
return a + b;
}
}
}
在您编译DLL之后,需要使用regasm.exe注册它,以便可以从VBScript中调用它:
Dim myObj
Set myObj = CreateObject("MyDLL.Operations")
Dim sum
sum = myObj.AddIntegers(3, 5)
此回复基于Raymund Macaalay发布的CodeProject How to call a .NET DLL from a VBScript。我建议你阅读它。
此外,您应该检查其他stackoverflow发布,例如How to call C# DLL function from VBScript。
答案 1 :(得分:2)
是的,您需要使托管代码库(DLL)对VBScript可见(最有可能通过GAC)。然后在您的VBScript中,您可以执行以下操作:
dim yourObject = CreateObject("YourContainingObject");
yourObject.AddIntegers yourFirstInt, yourSecondInt