简体   繁体   中英

how can I run an external program In C?

How can I run an external program in 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? For example a window size of 300 X 300 pixels.

The standard way is system -- works pretty much anywhere, but gives you no control over how the child process runs.

In ascending order of control (and complexity), Windows provides: WinExec , ShellExecute , ShellExecuteEx , and CreateProcess . With CreateProcess you pass a STARTUPINFO or STARTUPINFOEX structure. 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).

You run and external program using system from the C standard library or the Win32 CreateProcess function.

To resize a the main window of an application you create. First start the process with CreateWindow. Then use EnumThreadWindows with the handle from CreateProcess to find the main window of that process. Finally, you can call MoveWindow with that handle to set the size and position.

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

To run an external program, the most straighforward method is with 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).

You could also check _popen ( stdio.h ) for console applications (only).

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上使用system()(禁止)使用Shell apis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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