调用exe可以获得返回值吗
时间: 2023-10-06 13:05:01 浏览: 247
是的,可以通过调用exe文件并获取其返回值来实现与外部程序的交互。具体实现方式因开发语言和操作系统而异,一般可以通过使用系统调用或API函数来实现。例如,在Windows平台上,可以使用CreateProcess函数来启动一个外部程序,并通过GetExitCodeProcess函数获取其返回值。在Linux平台上,可以使用system函数或popen函数调用外部程序,并通过errno或WIFEXITED等宏获取其返回值。
相关问题
c++调用exe并传入参数得到返回值
在C语言中,要调用一个.exe文件并传递参数以获取返回值,我们可以使用系统调用函数 `system()` 或 `popen()`。
使用 `system()` 函数可以很方便地调用一个.exe文件,例如:
```c
#include <stdio.h>
int main() {
int result = system("myprogram.exe argument1 argument2");
printf("返回值为:%d\n", result);
return 0;
}
```
在上述代码中,`system()` 函数会执行名为 `myprogram.exe` 的可执行文件,并传递两个参数 `argument1` 和 `argument2`。该函数的返回值是可执行文件的退出状态码。
如果我们需要获取可执行文件的输出,可以使用 `popen()` 函数,如下所示:
```c
#include <stdio.h>
int main() {
char buffer[1024];
FILE *fp = popen("myprogram.exe argument1 argument2", "r");
if (fp == NULL) {
printf("无法打开管道!\n");
return -1;
}
if (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("返回值为:%s\n", buffer);
}
pclose(fp);
return 0;
}
```
上述代码会执行名为 `myprogram.exe` 的可执行文件,并传递两个参数 `argument1` 和 `argument2`。`popen()` 函数会打开一个管道,将可执行文件的输出发送到该管道中。我们可以使用 `fgets()` 函数读取管道中的数据,获取可执行文件的输出。最后,记得要调用 `pclose()` 函数关闭管道。
以上是使用C语言调用.exe文件并传递参数以获取返回值的方法。
vc 调用exe并传入参数得到返回值
VC(Visual C++)可以通过调用外部的.exe文件并传入参数,然后获取该程序的返回值。以下是一个简单的示例代码:
```cpp
#include <windows.h>
#include <iostream>
int main()
{
std::string programPath = "C:\\path\\to\\program.exe";
std::string parameters = "param1 param2 param3";
// 创建一个匿名管道,用于接收程序的输出
HANDLE hReadPipe, hWritePipe;
SECURITY_ATTRIBUTES securityAttributes;
securityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
securityAttributes.bInheritHandle = TRUE;
securityAttributes.lpSecurityDescriptor = NULL;
if (!CreatePipe(&hReadPipe, &hWritePipe, &securityAttributes, 0))
{
std::cerr << "无法创建管道!" << std::endl;
return -1;
}
// 启动程序并将输出重定向到管道
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
ZeroMemory(&startupInfo, sizeof(startupInfo));
ZeroMemory(&processInformation, sizeof(processInformation));
startupInfo.cb = sizeof(startupInfo);
startupInfo.hStdInput = NULL;
startupInfo.hStdOutput = hWritePipe;
startupInfo.hStdError = hWritePipe;
startupInfo.dwFlags |= STARTF_USESTDHANDLES;
if (!CreateProcess(NULL, const_cast<LPSTR>(programPath.c_str()), NULL, NULL, TRUE, 0, NULL, NULL, &startupInfo, &processInformation))
{
std::cerr << "无法启动程序!" << std::endl;
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
return -1;
}
// 等待程序结束并获取返回值
WaitForSingleObject(processInformation.hProcess, INFINITE);
DWORD exitCode;
GetExitCodeProcess(processInformation.hProcess, &exitCode);
// 读取程序输出
char buffer[1024];
DWORD bytesRead;
std::string output;
while (ReadFile(hReadPipe, buffer, sizeof(buffer), &bytesRead, NULL) && bytesRead > 0)
{
output.append(buffer, bytesRead);
}
// 关闭句柄
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
CloseHandle(processInformation.hProcess);
CloseHandle(processInformation.hThread);
std::cout << "程序返回值:" << exitCode << std::endl;
std::cout << "程序输出:" << std::endl;
std::cout << output << std::endl;
return 0;
}
```
上述代码首先使用`CreatePipe`函数创建了一个匿名管道,然后使用`CreateProcess`函数启动目标程序,并将其标准输出重定向到管道。接下来,使用`WaitForSingleObject`函数等待程序结束,并通过`GetExitCodeProcess`函数获得程序的返回值。最后,使用`ReadFile`函数读取管道中的输出,并关闭相关的句柄。
这样,我们就可以通过VC调用外部的.exe文件并传入参数,并获取到该程序的返回值。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)