Call a function in a console app from VBScript

风格不统一 提交于 2019-12-24 02:33:25

问题


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)

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?


回答1:


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;
       }
    }
}

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)

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.

Also, you should check other stackoverflow posting such as How to call C# DLL function from VBScript.




回答2:


Yes, you will need to make the managed code library (DLL) visible to the VBScript (most likely through the GAC). Then in your VBScript, you can do something like:

dim yourObject = CreateObject("YourContainingObject");
yourObject.AddIntegers yourFirstInt, yourSecondInt


来源:https://stackoverflow.com/questions/9004352/call-a-function-in-a-console-app-from-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!