简体   繁体   English

如何在C中运行外部程序?

[英]how can I run an external program In C?

How can I run an external program in C? 如何在C中运行外部程序? For example application programs like a browser , word , Notepad , etc. Also how can I set a certain size of the window of the external application program? 例如应用程序,如browserwordNotepad等。另外,如何设置外部应用程序窗口的特定大小? For example a window size of 300 X 300 pixels. 例如,窗口大小为300 X 300像素。

The standard way is system -- works pretty much anywhere, but gives you no control over how the child process runs. 标准方式是system - 几乎可以在任何地方工作,但是无法控制子进程的运行方式。

In ascending order of control (and complexity), Windows provides: WinExec , ShellExecute , ShellExecuteEx , and CreateProcess . 按控制(和复杂性)的升序,Windows提供: WinExecShellExecuteShellExecuteExCreateProcess With CreateProcess you pass a STARTUPINFO or STARTUPINFOEX structure. 使用CreateProcess,您可以传递STARTUPINFOSTARTUPINFOEX结构。 Either way, you can specify the starting position and/or size for the child window (though the child process can and may move/resize its window before it's even visible). 无论哪种方式,您都可以指定子窗口的起始位置和/或大小(尽管子进程可以并且可以在窗口甚至可见之前移动/调整其大小)。

You might also want to consider Boost Process , which isn't accepted as an official part of Boost, but provides a bit more control than system , while remaining reasonably portable to a fair number of the most widely used systems (including both Windows and anything reasonably POSIX-like, such as Linux or OS X). 您可能还需要考虑Boost Process ,它不被接受为Boost的官方部分,但提供了比system更多的控制,同时保持合理的可移植到相当多的最广泛使用的系统(包括Windows和任何东西)合理的POSIX,如Linux或OS X)。

You run and external program using system from the C standard library or the Win32 CreateProcess function. 您可以使用C标准库或Win32 CreateProcess函数中的系统运行外部程序。

To resize a the main window of an application you create. 要调整您创建的应用程序的主窗口的大小。 First start the process with CreateWindow. 首先使用CreateWindow启动该过程。 Then use EnumThreadWindows with the handle from CreateProcess to find the main window of that process. 然后使用带有CreateProcess句柄的EnumThreadWindows来查找该进程的主窗口。 Finally, you can call MoveWindow with that handle to set the size and position. 最后,您可以使用该句柄调用MoveWindow来设置大小和位置。

you can use system function for this purpose like, 你可以使用系统功能,如,

#include <stdlib.h>

int main()
{
    system("your-program-name");

    return 0;
}

This will be executed in command prompt. 这将在命令提示符下执行。

But if you want to use winapi the best way is to use CreateProcess() function, http://msdn.microsoft.com/en-us/library/ms682425.aspx 但是如果你想使用winapi,最好的方法是使用CreateProcess()函数, http://msdn.microsoft.com/en-us/library/ms682425.aspx

To run an external program, the most straighforward method is with system . 要运行外部程序,最直接的方法是使用system There are other options (use search). 还有其他选项(使用搜索)。

To set the window size, you must interact with your window manager and ask it to do that. 要设置窗口大小,您必须与窗口管理器进行交互并要求它执行此操作。 I don't know if that is possible for you (Windows has a "start" command that you may find helpful, if you use that OS). 我不知道你是否可以这样做(Windows有一个“启动”命令,如果你使用那个操作系统,你会发现它很有帮助)。

You could also check _popen ( stdio.h ) for console applications (only). 您还可以检查_popenstdio.h )以获取控制台应用程序(仅限)。

To create a Windows application that redirects input and output, see Creating a Child Process with Redirected Input and Output in the Windows SDK. 要创建重定向输入和输出的Windows应用程序,请参阅在Windows SDK中创建具有重定向输入和输出的子进程

切勿在Windows上使用system()(禁止)使用Shell apis

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

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