5

I'm sorry if this was covered before, but I can't find it anywhere on StackOverflow.

Basically I'm trying to run things that you usually run at a Windows command prompt:

msiexec /i file.msi /q

and other sort of commands from my C program. Is this possible?

Thanks.

Qosmo
  • 1,142
  • 5
  • 21
  • 31

4 Answers4

3

In windows using the Win API ShellExecute will give you best control of your child process. However the other two methods mentioned by Dave18 and Pablo work as well.

AndersK
  • 35,813
  • 6
  • 60
  • 86
2

Try C system function

#include <stdlib.h>

int main ()
{

  system ("msiexec /i file.msi /q");
  return 0;
}
cpx
  • 17,009
  • 20
  • 87
  • 142
1

You need to use one of the functions from the exec family of function. Here's a list of them.

So, to run your example you can use:

execl("msiexec","/i","file.msi","/q",NULL);
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • -1 execl is not C89 and not C99 -> no standard C; it's POSIX 90, therefore contains in windows-compilers without guarantee. see below the system-example, which ever will work. – user411313 Dec 23 '10 at 13:04
1

Pablo and Dave are right, depending on what you want to do.

execl loads the new application into memory and runs it in place of the current process. Your program will end after the execl() call.

System runs the application in a subshell, you can retrieve it's exit status but not any information about it's stdin/stdout data.

How interested are you in what happens after you start the process?

richo
  • 8,717
  • 3
  • 29
  • 47
  • Now that you ask, would be nice to "hold" my program while it performs and exits. – Qosmo Dec 23 '10 at 03:00
  • `system` creates a subshell and executes your string. It doesn't give you much control. `fork` and then `execl` is the common idiom for if you're more interested in what goes one (interact with the fd's of your forked process) – richo Dec 23 '10 at 03:16