从VBScript调用控制台应用程序中的函数 [英] Call a function in a console app from VBScript
问题描述
我有一个控制台应用程序myapp.exe。在应用程序中有一个函数,我们称它为:
I have a console app, myapp.exe. Within the app is a function, let's call it:
public static int AddIntegers(int a, int b)
是否可以使此函数在外部可见,以便VBscript可以调用它?我是否必须将函数移到DLL中,或者可以将其保留在EXE中并使其可见?如果是,怎么办?
Is it possible to make this function visible externally so that a VBscript could call it? Do I have to move the function into a DLL or can I leave it in the EXE and make it visible? If so, how?
推荐答案
理想地,您应该制作一个DLL并在需要公开的功能上设置Com Visible。
Idealistically, you should be making a DLL and set Com Visible on the functions you need to expose.
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调用它:
After you've compiled your DLL you need to register it with regasm.exe so that you can call it from VBScript:
Dim myObj
Set myObj = CreateObject("MyDLL.Operations")
Dim sum
sum = myObj.AddIntegers(3, 5)
此回复基于CodeProject发布的如何从VBScript调用.NET DLL ,作者是Raymund Macaalay。我建议您阅读它。
This reply is based on the CodeProject posting How to call a .NET DLL from a VBScript by Raymund Macaalay. I recommend you read it.
此外,您还应该检查其他stackoverflow发布信息,例如如何从VBScript调用C#DLL函数。
Also, you should check other stackoverflow posting such as How to call C# DLL function from VBScript.
这篇关于从VBScript调用控制台应用程序中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!