Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a document programmatically in C++

Tags:

c++

I have a console program written in C++. Now I want to open a manual document(in .txt or .pdf) everytime a user of the program types 'manual' in console. How can I do this? Any links to a tutorial would be helpful.Thanks

like image 864
James Avatar asked Dec 17 '22 01:12

James


2 Answers

Try to compile this code (Open.cpp) to Open.exe Then, you can execute it with (for example) these parameters :

Open "C:\your file.doc"

Open "C:\your file.exe"

Open notepad

#include "windows.h"

int main(int argc, char *argv[])
{
    ShellExecute(GetDesktopWindow(), "open", argv[1], NULL, NULL, SW_SHOWNORMAL);
}

Explanation of the program :

  1. You should first include windows library (windows.h) to get ShellExecute and GetDesktopWindow function.
  2. ShellExecute is the function to execute the file with parameter argv[1] that is path to the file to be opened
  3. Another option for lpOperation arguments instead of "open" is NULL. "explore" and "find" are also the options but they are not for opening a file.
  4. SW_SHOWNORMAL is the constant to show the program in normal mode (not minimize or maximize)
like image 108
Zai Avatar answered Jan 02 '23 15:01

Zai


Assuming you're on Windows, you're looking for the ShellExecute function. (Use the "open" verb)

like image 41
Billy ONeal Avatar answered Jan 02 '23 16:01

Billy ONeal