0

I'm writing a program in C++ and at one point I want to open a file with a certain program (either Libreoffice or Word, depending on what is installed on the pc). For that I need to check which program is installed on the pc first.
I'm usually using linux and for that I have found

if (!system("which libreoffice --writer > /dev/null 2>&1")) {
       const char* command = "libreoffice --writer myfile.rtf &";
       system(command);
}

which works perfectly.
However, I cannot figure out how to do the same for Windows (the program is meant to run on a Windows pc).
I know that I can query if a program is installed in Windows using where -command, however apparently I don't quite understand how to use it for I cannot get it to work for me.
Help would be very appreciated.

2
  • How do you open the file with Libreoffice or Word? Commented Oct 13, 2022 at 18:14
  • 1
    system("which libreoffice --writer > /dev/null 2>&1") assumes the program is accessible through the system path. You can't always count on this, and you definitely can't with Windows. Commented Oct 13, 2022 at 18:15

1 Answer 1

2

You do not need to check which program is installed on this PC before executing a data file on Windows.

You can use ShellExecute directly on a data file, and the system will find the program that has been associated with that file type, and execute it appropriately.

Depending on your needs, you may prefer to use ShellExecuteEx instead. In particular, if you want to find when the child process finishes execution (or similar), ShellExecuteEx gives you a handle to the child process, which ShellExecute does not.

If you really want to find the executable associated with a data file (even though it's unnecessary for the case you've cited), you can use FindExecutable to do that.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for taking the time to answer. My problem is that the file is a rtf document and therefore likely to be opened with wordpad. However, I need it to be opened with a program that can handle the formation.
@Bee isn't that for the user to decide? If they don't want rtf files opened with wordpad they should select a different program
The purpose of the program is to create the file (and preferably open it). There is an earlier part in which the user can chose the program they want to open the file with, or whether they don't want to have it opened. This part is mostly to make sure the program the user choses is actually installed on the pc. Not opening it with a program that recognizes the formatting would make using the program and creating the file at all pretty much useless.
@Bee I don't understand. wordpad handles rtf files just fine for me (although no doubt it has limitations). Also, There is an earlier part in which the user can chose the program they want to open the file with, so don't you already know what program that is?
Sorry for taking so long to reply, it took me a while to figure this out and test it. So far ShellExecuteA seems to open the file automatically with Word or Libreoffice, which is what I wanted, so I'm going to use it as it is. Thank you very much again for providing this answer!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.