gpt4 book ai didi

How can i run a C# program from Qt C++ program?(如何从Qt C++程序运行C#程序?)

转载 作者:bug小助手 更新时间:2023-10-25 11:17:34 25 4
gpt4 key购买 nike



I have a program written in C# (.net framework 4.0) and i have a main program written in Qt C++, i need to run this program (C# program) from my main program, the problem is if i use QProcess to run it, nothing happens, i used waitForStarted and waitForFinished members functions of Qprocess but already timeout.

我有一个用C#(.Net Framework4.0)写的程序,我有一个用QtC++写的主程序,我需要从我的主程序运行这个程序(C#程序),问题是如果我使用QProcess来运行它,什么都不会发生,我使用了QProcess的waitForStarted和waitForFinded成员函数,但已经超时。


I tried Qprocess (start()), system().
Here is what i tried:

我尝试了Qprocess(Start())、System()。以下是我尝试过的:


QProcess p;
p.start(qApp->applicationDirPath()+"/control.exe",{"0000"},QIODevice::ReadWrite);
p.waitForFinished();

QFile fff(qApp->applicationDirPath()+"/control.csv");
if(!fff.exists())

{
/*Error control.csv not exists*/
}

control.exe extracts data from a datagridView and store it in control.csv file, but the csv file is not created while control.exe works perfectly when i run it from windows console.
How can i run it from Qt C++ program?

Contro.exe从datagridView中提取数据并将其存储在Control.csv文件中,但当我从Windows控制台运行它时,没有创建CSV文件,而Contro.exe工作得很好。如何从Qt C++程序运行它?


PS: my C# program is a console application.

PS:我的C#程序是一个控制台应用程序。


Thank for helping me.

谢谢你帮我


更多回答

Please show your code -- preferably in the form of a minimal reproducible example.

请展示您的代码--最好是以最小的可重现示例的形式。

Should be no problem with QProcess.

QProcess应该没有问题。

Connect to the other QProcess signals and check the responses. Also, how long does your C# app. take to run? QProcess::waitForFinished only waits for 30 seconds by default and you never check its return value. Does the C# app. definitely create the csv file under qApp->applicationDirPath()? Creating it under the current working directory would be more usual (probably?).

连接到其他QProcess信号并检查响应。还有,你的C#应用程序。带着跑?默认情况下,QProcess::waitForFinded只等待30秒,并且您从不检查其返回值。C#应用程序。一定要在qApp->ApplationDirPath()下创建CSV文件吗?在当前工作目录下创建它会更常见(可能是?)。

@G.M. yes csv file is created in the same folder. i have let a default value for waitForFinished (30 seconds) and always timedout.

@G.M.YES CSV文件在同一文件夹中创建。我设置了waitForFinded(30秒)的默认值,并始终超时。

"How can i run a C# program from Qt C++ program?" - Same as you'd run any other program written in any other language. The fact that the target program was written in C# is irrelevant.

“如何从Qt C++程序运行C#程序?”--与运行任何其他语言编写的任何其他程序一样。目标程序是用C#编写的,这一点无关紧要。

优秀答案推荐

After effort and perseverance, I managed to find the problem.
This line had to be changed

经过努力和坚持不懈,我终于找到了问题所在。这条线必须改一下


File.WriteAllText("control.csv", string.Join("\n", rows));

By this one

由这一位


File.WriteAllText(System.AppDomain.CurrentDomain.BaseDirectory+"control.csv", string.Join("\n", rows));

I already run it using QProcess object in Qt.

我已经在Qt中使用QProcess对象运行了它。


Indeed, I must specify the whole path using System.AppDomain.CurrentDomain.BaseDirectory where to store control.csv file, I thought it will be created near the C# program.

事实上,我必须使用System.AppDomain.CurrentDomain.BaseDirectory来指定存储Control.csv文件的完整路径,我认为它将在C#程序附近创建。


I noticed one think, the program is extremly slow to find the automationElement object, it takes more than 20 seconds.

我注意到一个人认为,程序找到AutomationElement对象的速度非常慢,需要20多秒。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com