How to run a file in a specified program through c++ - c++

I am trying to use the ShellExecute command for launching a program and open a file through a c++ code. I have included windows.h. The code I am writing is as follows:
ShellExecute(GetDesktopWindow(), "open", "C:\\Program Files (x86)\\EMSO\\bin\\emso.exe","C:\\Program Files (x86)\\EMSO\\bin\\MultiCSTR_Reaction.mso", NULL, SW_SHOWNORMAL);
The code opens the executable file but is not able to open the .mso file. It generates dialog boxes saying- 'File 'C:\Program' not found', 'File 'Files' not found' and 'File '(x86)\EMSO\bin\MultiCSTR_Reaction.mso' not found.' I can open the file through the command line. But how to open through a c++ code.

Try the path to a file (4th argument to ShellExecute) as follows:
"\"C:\\Program Files (x86)\\EMSO\\bin\\MultiCSTR_Reaction.mso\""
Programs can take multiple command line arguments separated by spaces. So, if you try to pass a string that contain spaces as an argument, you'll find it split into as many arguments as many spaces there are. To avoid this, the string needs to be surrounded by quotes.

Related

How to call ShellExecute to open a file with specific program using C++ without associating same file extensions

I read MSDN and tried this line of code
ShellExecute(handle, "open", "fully_qualified_path_to_file", NULL, NULL, SW_SHOWNORMAL);
It works fine, but I need some more functionality. I wrote an app, which makes output in file with .bin extension. On my OS .bin files doesn't associated with any specific program. MSDN says that I can associate all .bin files with my app with programming, but this case is unacceptable for me.
How I can add this line of code to open specific file with specific program?
p.s. I tried to put my application path in 4th argument as shown in MSDN, smith like that,
C:\\Application.exe" "%1"
but it didn't work.
If the lpFile parameter is not a .exe then Windows will only be able to execute the file if it has a file type registration in the registry (ShellExecute will read the command line from the registry and replace %1 with the filename).
If you want to force a specific application then lpFile needs to specify the name/path of said application and the file you want it to open has to be part of the parameters in a format supported by the application, usually just the full path to the file (quoted with " if the path contains spaces). ShellExecute will not translate %1 for you in this case.

Open file with Standard editor and jump to a specific line

I'm programming a little tool to handle XML files. It is written in C++ with Qt.
To open such files with an Editor I used:
QDesktopServices::openUrl(QUrl::fromLocalFile(file.xml))
The next step was to open that XML file and jump to a specific line. I tried to use ShellExecute:
ShellExecute (NULL, "open", "Notepad.exe", "path.xml -l 200", NULL, SW_SHOWNORMAL);
However, it didn't work. The line above opens the file path.xml, but not at line 200. I tried the same with VIM instead of Notepad, with the same result. Where is my error?
Thanks for your help.
I would do it in the following way (Windows):
QProcess proc;
proc.startDetached("C:\\Program Files (x86)\\Notepad++\\Notepad++",
QStringList() << "file.xml" << "-n 2000");
The code above opens an instance of Notepad++ application, loads file.xml file and scrolls to the line 2000 (-n command line switch) of the opened file.

How do I open a file in c++ (other than notepad)

I was wondering how to open a file other than notepad... Our prof gave us an example:
s = "notepad.exe test.txt";
system(s.c_str());
That will open a file type of "notepad.exe" and the file name of "test.txt"
Main Question:
Now, I was wondering if there was a way to open other type of files, such as Microsoft Excel, Microsoft Word, Visual Studio, or 7zip.
My attempt opened something in a new cmd.exe (because of the START keyword):
fileNeededtoBeOpened = "START \"New Microsoft Office Excel Worksheet.xlsx\"";
system(fileNeededtoBeOpened.c_str());
(This code is slightly different from my original, where I'm trying to open a file from a vector...) but all I really need to know is instead of "notepad.exe" or "START" is there a different command to open different file types that aren't .txt
Also, a side note, I was reading on the internet that it wasn't safe to use system() to open files, is this correct?
I found the answer by myself... for those who are curious, here an the answers:
To open a text file: system(notepad)
To open an excel file: system(start excel)
To open a word doc file: system(start winword)
To open a 7z file: system(start 7zFM)
To open a visual studio file: system(start devenv)
I think you're confused.
System executes a command as you would on the command line (type cmd into the run prompt under start menu to get that).
So, when you type notepad.exe test.txt it's saying:
Open the program notepad.exe which is on the system path (so the
command line can find it to execute that program), and pass the
parameter test.txt to it.
Notepad itself decides what to do with test.txt, in this case it opens it.
So, you can tell it to run any command (program/executable) and pass any parameters to it in reality. If excel's on your system path, you can probably just type excel.exe to open it from your system command. Otherwise, find the location excel is installed in, and refer to it with the whole path to excel.exe and it will work fine.
For example, on my computer, executing "C:\Program Files\Microsoft Office\Office12\EXCEL.EXE" would open excel from the command line. I can pass further parameters to it by having more information (like filenames) after the Excel.exe" portion, just as you did in your notepad example. Using your system command should have equivilent behavior when that line is executed.
If you are only targeting Windows systems you can use the ShellExecuteEx function (part of the Win32 API). You can just pass a filename to it and it will launch what ever program is registered to handle that file type (just as if you opened the file from windows explorer). Documentation is available on MSDN:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx
There is some examples on Launching Applications (ShellExecute, ShellExecuteEx, SHELLEXECUTEINFO) MSDN article and lots more elsewhere around the internet.
AS the other guys mentioned , the System function only executes a cmd command, .. notepad.exe is in the system's path by default so it works directly
but for example for me if I want to open a zip file on my desktop , I'd type something like
"C:\Program Files\7-Zip\7zFM.exe" Desktop\zipfile.zip
that's when I'm currently at the my user's directory [by default] , or
"C:\Program Files\7-Zip\7zFM.exe" C:\Users\JiMMaR\Desktop\zipfile.zip
[where JiMMaR is my user name on windows 7]
note that this certain command works only on windows , if you are using another OS this won't work as it is
try doing a
fileNeededtoBeOpened = "\"C:\Program Files\7-Zip\7zFM.exe\" C:\Users\YOUR_USER_NAME\Desktop\zipfile.zip";
and see if that executes or not
edit:
if you cannot escape the space , then try this one
fileNeededtoBeOpened = "C:\Program~1\7-Zip\7zFM.exe C:\Users\YOUR_USER_NAME\Desktop\zipfile.zip";
Ok, firstly - system - is a function that starts a separate process to your program. Much the same as in a command window when you type the command. The command lines you provide will be dependent on the applications you want to launch.
Now, I was wondering if there was a way to open other type of files,
such as Microsoft Excel, Microsoft Word, Visual Studio, or 7zip.
Yes I would be pretty shocked if there wasn't a command line parameter you could specify to load a document in these apps at start up. (Ok not shocked, but it is pretty standard)
Does this have anything to do with c++ - not really - you need to look at references for the applications you mention and see what the command lines parameters are for them. Then craft a string and system(...) to your hearts content.

Open file to display content in C++

I have 2 questions to ask regarding opening files (any kind of files) using C++. I am currently working on a GUI program and I want to add a changelog in txt form. Also I want a menu in my program to open that changelog.txt with the default text editor every user has installed or to simply put it to open that text file. Please keep in mind that I want to open the file for display NOT in the program for input/output.I know I can do that using
system("notepad.exe filepath.txt");
or to open them with the preset program:
system("filepath.txt");
The problem is that both of those open a command line behind the notepad. I know there is another command to open files using Win32 API called CreateProccess() but my compiler doesn't recognise that command (OpenWatcom W32).
So here are my questions:
1) Is there any other command to open files or is there a way to stop the command line from opening when using system command?
2) How do you define in Windows that the text file is in the current program folder? I mean instead of giving the entire filepath which will change from user to user is there any way to "tell" the program that the file is always on the current folder that the program is in?
I am sorry for any errors, if you want any clarification please let me know.
CreateProcess would be the wrong function to use here. That would require you to decide which process to run. The user may prefer to use a text editor other than Notepad, I know I do! The right way to do this on Windows is to ask the shell to open the file with whatever program the user has associated with the file. The ShellExecute function does this.
Call it like this:
ShellExecute(
MainWindowHandle,
"open",
FullyQualifiedTextFileName,
NULL,
NULL,
SW_SHOWNORMAL
);
You will need to include the Shellapi.h header file and link to the Shell32.lib library. If your compiler does not include these files, and I would be surprised if that was the case, then you can get them from the Platform SDK. That said, if you are serious about programming on Windows you should get hold of a tool that gives you access to the Windows API.
I do recommend that you use a fully qualified path for a task like this. Since your text file is located in the same directory as the executable you should simply join that directory to your text file's name. Get hold of the full path to the executable by calling GetModuleFileName passing NULL for the hModule parameter.

Invoking command line programs by double clicking on files in windows

I've written a command line program using c++ in the netbeans IDE, then compiled it using mingw. The program takes as its only argument the name of a file (with the fairly unique extension .mmf) , then it plays a movie based on the file.
What I would like is to be able to set the default application for the .mmf extension to my command line program, so that when I double click on a file of this type, it plays my movie. When you open a file in windows like this, does it just call your application with the filename as an argument? Are other arguments passed?
In other words, if I associate the .mmf file type with myprogram.exe, is double clicking on myfile.mmf equivalent to invoking the shell command
myprogram myfile.mmf
If not, what shell command is invoked? Also, is the full path to the file passed, or the relative path?
When you open a file in windows like this, does it just call your application with the filename as an argument?
Yes.