简体   繁体   中英

Execute a C program from within another C program as if it was a function call (in Windows)?

Is it possible call a separate C program (.exe file) within a C program, like if it was a function?

I would like to be able to pass arguments of any kind (like any other function) to this separate program, and get the return value (so it can be used in the host program).

I imagine that the arguments can be passed by using int argc, char *argv[] , but I don't know if it's possible to pass integers, arrays, pointers to structures and so on.

On the other hand, I've read that the return value from the main function is system specific. Since I'm using Windows, is there any limitations to this return value (type, size, etc.)? Can it be anything that could be used as a return value in any normal function?

Thanks!

What you describe, is the basic premise of the Unix operating system. Unix was designed to allow accomplishing very complex tasks by chaining several commands, piping the (text) output of a command as the input of the next one (this was pretty revolutionary back then).

As klutt already suggested, you can accomplish the same with a Windows executable. To his list, I would add learning how to redirect the input/output of a program to a file handle .

The Windows PowerShell extended this concept to allow passing different data-types other than text, to some special executables known as cmdlets , however, to write your own, you need support from the.Net Framework or the.Net Core infrastructure, so you must do so from a managed language such as C# or C++/CLI.

Keep in mind that spawning a whole process is an extremely expensive operation (compared to simply calling a linked function), so there is some significant overhead you need to be aware of.

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