Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I run an external program In C?

Tags:

c

winapi

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.

like image 749
JavaX Avatar asked Mar 11 '11 21:03

JavaX


3 Answers

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).

like image 88
Jerry Coffin Avatar answered Oct 08 '22 05:10

Jerry Coffin


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.

like image 25
Judge Maygarden Avatar answered Oct 08 '22 04:10

Judge Maygarden


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

like image 2
Tayyab Avatar answered Oct 08 '22 06:10

Tayyab