1

I am running exe with 2 parameters in an MFC application. how can i achieve this.

Eg: sox.exe a.wav b.mp3 I need to execute within an MFC application

Thanks in advance.

Raj
  • 263
  • 1
  • 2
  • 14

2 Answers2

0

If a MFC application supports stdlib.h, you might be able to use the system() function: create a C string with the command you want to run, i.e. "sox.exe a.wav b.mp3" and use it as parameter for the system() function, like this:

system("sox.exe a.wav b.mp3");

Convert a set of files named track01.wav, track02.wav, ... , track99.wav into song01.mp3, song02.mp3, ...

char command[100];
int i;

for (i=1;i<=99;i++)
{
  sprintf (command, "sox.exe track%.2d.wav song%.2d.mp3", i, i);
  system (command);
}
mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
  • This is 2013 - UNICODE is not this newfangled stuff anymore. [`system`](http://msdn.microsoft.com/en-us/library/277bwbdz.aspx) passes the string to the **command interpreter** for interpretation. This is error-prone and unnecessary. Use `CreateProcess` - that's what it was meant for. – IInspectable Nov 27 '13 at 16:45
  • `system` can be still preferred in some situations, see [here](http://stackoverflow.com/a/9972833/971443) – Zac May 10 '16 at 08:00
0

The preferred way of doing this is using ShellExecuteEx.

rrirower
  • 4,338
  • 4
  • 27
  • 45
  • This is the **hard** way of doing things. Don't ever use `ShellExecuteEx` unless you absolutely have to. `CreateProcess` is the right tool. – IInspectable Nov 27 '13 at 16:42
  • @IInspectable I respectfully disagree. Aren't you simply expressing an opinion? – rrirower Nov 27 '13 at 20:35
  • For one thing, `ShellExecuteEx` requires that you initialize COM on the calling thread. If that thread happens to be in the MTA, it is game over already. The way it reports errors is confusing to say the least. If you are running `ShellExecuteEx` under the loader lock, you are in for an instant deadlock. None of these issues exist with `CreateProcess`. Those are the all facts - not opinions. – IInspectable Nov 27 '13 at 21:22
  • Here's my last word(s) on this as I have no desire to debate the merits of a solution. While there are some interesting points raised, it would be remiss of me if I did not point out that within the context of this question, COM [may not even be required to load](http://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx). – rrirower Nov 28 '13 at 14:01
  • The *"may not even be required"* section is an interesting one, as it continues to conclude: You cannot ever know whether or not it is required, so you have to bite the bullet and initialize COM anyway. I have made a point why `CreateProcess` is superior, in all aspects. You have not come up with a single reason, why one should prefer `ShellExecuteEx` over `CreateProcess`. Do so, or live with the downvote for good reason. – IInspectable Nov 28 '13 at 15:19