运行带有参数的控制台应用程序? [英] Run a console application with arguments?

查看:118
本文介绍了运行带有参数的控制台应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我的vb程序有严重问题.
这个想法是:在vb.net中运行一个外部控制台应用程序并将参数传递给它.
但是问题是我的代码没有传递参数.

这是我的代码

Hi guys,
I have serious problem with my vb program.
The idea is : run an external console application in vb.net and pass arguments to it.
But the problem is my code didn''t pass arguments.

this is my code

Dim _AppPath As String = Application.StartupPath & "/test3.exe"
Dim _AppArguments As String = "3"
Dim _Process As New Process
_Process.StartInfo.FileName = _AppPath
_Process.StartInfo.Arguments = _AppArguments
_Process.StartInfo.UseShellExecute = False
_Process.StartInfo.CreateNoWindow = True
_Process.StartInfo.RedirectStandardOutput = True
_Process.Start()
_Process.WaitForExit()
Dim Result As String
Result = _Process.StandardOutput.ReadToEnd()
MsgBox(Result)



如您所见,控制台应用程序读取了一个数字并打印出来.

我的vb.net程序尝试运行该控制台程序并传递3(作为参数),然后获取输出并将其打印在消息框中.
但是,当程序运行时,输出为:-858993460

为什么会这样

这是控制台应用程序代码:



As you see the console application read a number and print it.

My vb.net program try to run that console program and pass 3 (as an argument) and then take the output and print it in messagebox.
But when the program run the output is: -858993460

Why this happend

this is the console application code:

#include "iostream.h"

int main(int argc, char* argv[])
{
	int x;
	cin>>x;
	cout<<x<<endl;
	return 0;
}


希望您有答案...


I hope you have the answer......

推荐答案

语句的顺序不正确.您需要开始该过程并开始阅读StandardOutput.现在,您已阅读尝试在控制台应用程序已终止后阅读它.试试这个:
The order of your statements isn''t correct. You need to start the process and start reading the StandardOutput. You now read try reading it after the console application is already terminated. Try this:
// ...
_Process.Start()
Dim Result As String
Result = _Process.StandardOutput.ReadToEnd()
_Process.WaitForExit()



祝您好运!



Good luck!


您的控制台程序正在尝试通过从stdin读取而不是使用命令行提供的参数来获取其参数,因此也就不足为奇了.产生垃圾输出.命令行上提供的参数是通过argv[]数组访问的,程序名称由argv[0]提供,因此第一个数据参数是argv[1].
Your console program is trying to get its parameter(s) by reading from stdin instead of using the parameters provided on the command line, so it is no wonder that it produces garbage output. The parameters provided on the command line are accessed via the argv[] array, with the program name being provided by argv[0], so the first data parameter is argv[1].


这篇关于运行带有参数的控制台应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆