通过的Process.Start(启动)应用是没有得到论据 [英] Application started by Process.Start() isn't getting arguments
问题描述
使用C#,我试图命令行参数传递到使用的Process.Start()的新工艺:
Using C#, I am trying to pass command-line arguments to a new process using Process.Start():
string path = @"C:\Demo\Demo.exe";
string arguments = "one two three";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = path,
Arguments = arguments
};
var process = Process.Start(startInfo);
我的C程序DEMO.EXE只是回声命令行参数:
My C application Demo.exe just echos the command line arguments:
int main( int argc, char *argv[] )
{
int count=0;
// Display each command-line argument.
printf( "\nCommand-line arguments:\n" );
for( count = 0; count < argc; count++ )
printf( " argv[%d] %s\n", count, argv[count] );
while(1);
}
如果我开始从我的cmd.exe程序,我得到合理的输出:
If I start my application from cmd.exe, I get reasonable output:
Command-line arguments:
argv[0] Demo.exe
argv[1] one
argv[2] two
argv[3] three
当我使用C#应用程序中,我得到的唯一的事情是在的argv [0] path参数:
When I use the C# application, the only thing I get is the path argument at argv[0]:
Command-line arguments:
argv[0] C:
任务管理器显示启动DEMO.EXE的每个方法的命令行参数:
Task Manager shows command line arguments for each method of starting Demo.exe:
为什么是不是我的C应用程序从C#应用程序接收到命令行参数?
Why isn't my C application receiving the command-line arguments from the C# application?
修改
@hvd建议我用GetCommandLine ()。下面是该代码和结果:
Edit @hvd suggested I use GetCommandLine(). Here is the code and result of that:
char* ar = GetCommandLine();
printf( "\nGetCommandLine arguments:\n" );
printf(" %s", ar);
输出:
Output:
GetCommandLine arguments:
"C:
是否有可能,在C应用程序是接受ARGS作为一个字符串,但在路径中的第\会忽略一切
Is it possible that the C app is receiving the args as one string, but ignores everything after the first \ in the path?
编辑:我增加了一个回答以下,这是一个解决办法,但我不知道我的问题的原因。
I've added an answer below. It is a workaround, but I'm not sure the cause of my issue.
推荐答案
我能够重现您的问题,我没有获得C,所以我在Visual Studio中使用C ++ 2013年看来,使用C#的的StartInfo 传递的参数为的Unicode 字符,所以第一个字节不为零,而第2个字节可能导致因为这表示字符串终止字符显示的第一个字符0位。当我用的printf它没有工作,我不得不用_tprintf,看看是过去了。和printf不的Unicode 。不仅printf的不处理它,填充当你的C程序的的argv 不会转化的的Unicode 以字符串使用手柄1字节字符。虽然TCHAR(宽字符)和tprintf在C ++中不一样,C#本身。
I was able to reproduce your issue. I didn't have access to C, so I used C++ in Visual Studio 2013. It appears that C# using StartInfo passes the arguments as Unicode characters, so the first byte is non-zero, while the 2nd byte is likely 0 bits resulting in displaying only the first character since that indicates the string termination character. When I used printf it did not work, I had to use _tprintf to see what is passed. And printf does not handle Unicode. Not only does printf not handle it, your C program when populating argv will not translate Unicode to a string using 1 byte characters. While TCHAR (wide char) and tprintf in C++ does, as does C# natively.
所以,当你做它的其他方式,使用的的cmd.exe 调用/ C DEMO.EXE一二三 CMD并没有传递字符串作为的Unicode 。这是我的假设,因为我得到的结果。
So, when you did it the other way, using "cmd.exe" to call "/C Demo.exe one two three" cmd was not passing the string as Unicode. That's my hypothesis, given the results I am getting.
在计算器上相关的问题。
Related Question on StackOverflow
- Does Process.StartInfo.Arguments support a UTF-8 string?
这是正确显示参数的C ++代码的(tprintf)并正确<强>(printf的)
The C++ code that displayed the Arguments correctly (tprintf) and incorrectly (printf)
#include "stdafx.h"
#include "string.h"
int _tmain(int argc, _TCHAR* argv[])
{
int count=0;
// Display each command-line argument.
printf( "\nCommand-line arguments:\n" );
for( count = 0; count < argc; count++ )
//Correct. This statement worked, displaying the arguments
//_tprintf( _T(" argv[%d] %s\n"), count, argv[count] );
//Incorrect. Displayed only the first character of each argument
//printf( " argv[%d] %s\n", count, argv[count] );
getchar();
return 0;
}
}
这是C#代码调用它
namespace ProcessPassArguments
{
class Program
{
static void Main(string[] args)
{
string path = @"C:\Temp\Demo.exe";
string arguments = "one two three";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = path,
Arguments = arguments
};
var process = Process.Start(startInfo);
}
}
}
有关信息仅供参考,C#调用C#也工作了。再次怀疑的原因是,C#是传递参数给你的C程序作为的Unicode 字符。
For informational purposes only, C# calling the C# also worked. Again the suspected cause is that C# is passing the arguments to your C program as Unicode Characters.
C#代码,它作为程序调用的目标。
The C# code that works as the target programmed called.
namespace Demo
{
class Program
{
static void Main(string[] args)
{
int i = 0;
foreach (string arg in args)
{
i++;
Console.WriteLine("Argument {0}: {1}", i, arg);
}
Console.ReadLine();
}
}
}
这篇关于通过的Process.Start(启动)应用是没有得到论据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!