简体   繁体   English

从 C# 使用参数在 C++ 上运行 .exe

[英]Run .exe on C++ from C# with arguments

I have one application on C++ and one on C# (for GUI).我在 C++ 上有一个应用程序,在 C# 上有一个应用程序(用于 GUI)。

I run exe - file (C++) from C# in new process and pass parameters to the command line.我在新进程中从 C# 运行 exe - file (C++) 并将参数传递给命令行。

Code C#:代码 C#:

System.Diagnostics.Process Proc = new System.Diagnostics.Process();
Proc.StartInfo.FileName = "main.exe";
Proc.StartInfo.Arguments = "p";
Proc.Start();

Code C++:代码 C++:

if (char(argv[1]) == 'p') 
     {
            program.SetUniform("cur_color", make_float3(0.5f, 0.5f, 0.0f));
            std::cout << "This is true" << std::endl;
     }
else {
            std::cout << "This is false    " << argv[1] << std::endl;
            program.SetUniform("cur_color", make_float3(0.9f, 0.9f, 0.9f));
     }

But I got "This is false p" as a result.但结果我得到了“这是假 p”。

I don't know why.我不知道为什么。 Can you help me?你能帮助我吗? Could there be a problem with the type of argument being passed?传递的参数类型是否有问题? Thanks!谢谢!

You cant do comparing this way, because arguments in C++ are delivered as char** where argv[0] argv[1] etc. are arrays created from arguments separated by space.您不能以这种方式进行比较,因为 C++ 中的参数以char**形式提供,其中argv[0] argv[1]等是由空格分隔的参数创建的数组。

Example:例子:

main.exe foo bar

argv[0] == ['f','o','o']
argv[1] == ['b','a','r']

Documentation: HERE文档:这里

So you need to iterate through the characters for comparison or make std::vector<std:string> from argv所以你需要遍历字符进行比较或从argv 生成std::vector<std:string>

Example:例子:

C# Program Calling C++ Exe
  main.exe p foo bar
C++ Program Getting Parameters and comparing them
  argv[1][0] == 'p'

Simpler way to compare, but not faster in non complex solutions:更简单的比较方法,但在非复杂解决方案中不会更快:

std::vector<std::string> vectorOfArguments(argv + 1, argv + argc);

Then you can just browse through vector然后你可以浏览矢量

for(auto & argument : vectorOfArguments){
   //Some Action
}

Hope it helped希望有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM