Reputation: 1472
I am New in console application .I Need to pass two command line arguments to the console application from a web application and get the returned result from console app.
here I tried in we
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim proc = New Process() With { _
.StartInfo = New ProcessStartInfo() With { _
.FileName = "C:\Users\Arun\Documents\visual studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe", _
.Arguments = TextBox1.Text & " " & TextBox2.Text, _
.UseShellExecute = False, _
.RedirectStandardOutput = True, _
.CreateNoWindow = True _
} _
}
proc.Start()
proc.WaitForExit()
Response.Write(proc.ExitCode.ToString())
End Sub
my console application code is
Public Function Main(sArgs As String()) As Integer
Return sArgs(0)
End Function
but I can't get the returned value from console app.What is the problem anyone Help?
Upvotes: 1
Views: 10100
Reputation: 27322
You can't natively return two separate arguments, you are limited A 32-bit signed integer
The only way I can think of doing this is if you have two numeric values that you can guarantee are less than 16 bits each then you could combine these into one 32bit value by bit shifting one of them.
This code should get you started:
Public Shared Function CombineValues(val1 As Int16, val2 As Int16) As Int32
Return val1 + (CInt(val2) << 16)
End Function
Public Shared Sub ExtractValues(code As Int32, ByRef val1 As Int16, ByRef val2 As Int16)
val2 = CShort(code >> 16)
val1 = CShort(code - (CInt(val2) << 16))
End Sub
Usage (console):
'in your console app combine two int16 values into one Int32 to use as the exit code
Dim exitCode As Int32 = CombineValues(100, 275)
Debug.WriteLine(exitCode) 'Output: 18022500
Usage (Calling code):
'In the calling app split the exit code back into the original values
Dim arg1 As Int16
Dim arg2 As Int16
ExtractValues(exitCode, arg1, arg2)
Debug.WriteLine(arg1.ToString + "; " + arg2.ToString) 'Output: 100; 275
Upvotes: 2
Reputation:
This is not the way in which the parameters are passed to a VB.NET console program (as you can see here).
An example:
Module Module1
Sub Main()
For Each arg As String In My.Application.CommandLineArgs
Console.WriteLine(arg)
Next
End Sub
End Module
If you generate a console-project EXE (app.exe) consisting exclusively in the code above and call it (from cmd) like this: [full_path]app 1 2
, you would get 1 2
printed in your screen.
Thus, all what you have to do is retrieving the arguments from My.Application.CommandLineArgs
.
-------- UPDATE AFTER THE EXACT REQUIREMENTS HAVE BEEN EXPLAINED BETTER
Under .Arguments
you have to put just the arguments you want to pass to the console application.
You can return more than just one integer to the calling program by relying on a simple temp file. For example:
CONSOLE PROGRAM:
Dim writer As New System.IO.StreamWriter("temp")
writer.Write("anything")
writer.Close()
CALLING PROGRAM:
Dim reader As New System.IO.StreamReader("temp")
Dim line As String
Do
line = sr.ReadLine() 'reading anything passed from the console
Loop Until line Is Nothing
reader.Close()
Try
System.IO.File.Delete("temp")
Catch ex As Exception
End Try
Upvotes: 2