通过控制台调用VB6应用程序中的子程序或函数



我想制作一个简单的应用程序,其中有1个方法和2个参数该应用程序可以通过控制台/CMD启动,如下所示:

name_of_app.exe name_of_method param1 param2

示例:我有一个名为myApp.exe的应用程序,它有一个类似的方法

  Module Module1
    Sub Main()
        Console.WriteLine("Hello World!")
        Dim x As Integer, y As Integer
        Dim total As Integer
        x = Console.ReadLine()
        y = Console.ReadLine()
        total = plus(x, y)
        Console.WriteLine("result: " & total)
        Console.ReadLine()
    End Sub
    Private Function plus(ByVal x As Integer, ByVal y As Integer) As Integer
        Return x + y
    End Function
End Module

所以在控制台/cmd中,我只调用类似的函数

myApp.exe plus 3 2

我怎样才能做到这一点?

Private Sub Form_Load()
    Dim strCommand As String
    Dim s() As String
    Dim spliter As String
    Dim returnValue As Variant
    strCommand = Command
    Do While InStr(1, strCommand, "  ", vbTextCompare) > 0
        strCommand = Replace(strCommand, "  ", " ")
    Loop
    spliter = " "
    s = Split(strCommand, spliter)
    returnValue = CallByName(Me, s(0), VbMethod, Val(s(1)), Val(s(2)))
    MsgBox returnValue
End Sub
Public Function Plus(ByVal Param1 As Variant, ByVal Param2 As Variant)
    Plus = Param1 + Param2
End Function

最新更新