Using ShellExecute to open file in browser - c++

I am trying to use ShellExecute to open a .txt file in the default browser.
I am currently using
ShellExecute(0, L"open", L"http://E:/path/to/file.txt", 0, 0, 1);
This correctly creates a new window in the browser but tries to open
E/path/to/file.txt (without the ":")
and then can not find the associated file. The file location is definitely correct as manually adding the ":" back in the browser opens the file as desired.

Option 1
Use AssocQueryString or IQueryAssocations to figure out the default browser, then launch that browser with your text file on the command line.
Option 2
Create a temporary .html file with a <meta> tag that redirects to a file:// URL that loads the text file. Then ShellExecute with your temporary .html file. Since the temp file is of type .html, it should load the user's default browser. The redirect will then cause the browser to load the text file.
The trick is figuring out when to clean up your temporary file. ShellExecute doesn't make it easy to get a handle to the launched process (and, in reality, the process you launch may just launch another process), so you don't have an easy way to know when the browser is done with your temporary file. You might just keep track of the temporary files you create and try to delete them when your application closes.

ShellExecute will open a txt file inside the default text viewer, which isn't a browser I suppose. To open inside a browser, explicitely run the browser and give the file as argument. For example:
ShellExecute(
0,
L"open",
L"C:\\Program Files\\Internet Explorer\\iexplore",
L"E:\\path\\to\\file.txt",
0,
1
);

Related

ShellExecute in MFC does not open Adobe XI

note: I already looked at the following question but it provided no insight: PDF file does not get open from ShellExecute function in Visual Studio, C++
I have the same problem as the poster in the above question.
Trying to open a PDF file from an MFC Application using ShellExecute. Adobe XI on Win 8 64 bit system.
hReturn = ::ShellExecuteA(NULL,"open",sPath,NULL,NULL,SW_SHOWMAXIMIZED);
sPath is a CString although I've tried alternatives with the same result. I've also tried SW_SHOW, SW_SHOWNORMAL. ShellExecute returns 42 when I cast the HINSTANCE to an int. I understand that means it opens successfully.
In fact, if I watch the task manager when executing the command, Adobe Reader opens in response in Task Manger but the Adobe window never opens.
When I exit my application, Adobe is still open.
If I try to open the target PDF file, the O/S says it can't be opened because it is already open in another application. When I "End Task" on the Adobe Reader in task manager, it frees up the file.
If I then click on the file, it opens fine in Adobe Reader.
So, I'm inferring from this that the ShellExecute is launching Adobe, that it is getting the right file, and that the file is, indeed, readable.
I've also confirmed that ShellExecute is working by directly referencing a txt file. It opens fine in Notepad.
hReturn = ::ShellExecuteA(NULL,"open","d:\\develop\\readme.txt", NULL, NULL, SW_SHOWMAXIMIZED);
Any ideas?
Thanks
try disable "Protected mode"
Adobe Reader - Protected Mode
This seems to be a known problem with opening a PDF file on Win8 via ShellExecuteEx. I had the same issue with an installer that would try to open a PDF at the end of the install process. On Win7 it worked fine. On Win8, it displayed the symptoms you describe. I was able to track it down to a problem with elevated privileges on our install program. I'm not sure why that would adversely affect things, but, it did. As an alternative, we looked at opening the file as an HTML document instead. We are still looking for a better solution.

Open .doc file without providing the path of its handler application C++

I need to open PDF and DOC files within my C++ project, the only limitation I have I can not use ShellExecute and WinExeute for opening extension files.
Now, I tried to open the files with WMI queries and OpenProcess() , but both these procedures require the Handler application path along with the path of DOC/PDF file.
I can not give the default handler application path, Is there any way to open files directly without specifying the Handler Application Path ?
You can resolve which application is associated with file you need to open. A start point here and here. It may be tricky because of various details you may need to take into account but it's what ShellExecute does.
If you know which application you want to use then search it in known applications (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths). This is useful only if you know which file type you're handling and which application you want to use.
A more easy method may be to execute cmd.exe, you won't call ShellExecute and it'll do the job for you (executing default verb):
cmd /c MyFile.txt
In code (just an example...):
CreateProcess("cmd.exe",
"/c c:\\MyFile.txt",
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&startupInfo,
&processInformation);

Launching html file in c++

I am trying to open a local html document using Shell Execute(). But, what i need is, Suppose if that particular document is already opened and if ShellExecute is triggered again, then that particular file should not be opened again instead bring the already opened file into foreground. Can you please suggest how can i do this?
void main()
{
ShellExecute(NULL, "open", "C:\\prograomgiles\\help.html",
NULL, NULL, SW_SHOWNORMAL);
}
It's nearly impossible, because it depends on which program processes the html files and how does it work. Suppose, that my OS opens html files by printing them directly on a printer. How would you bring opened file into foreground?
If you want to display HTML content in the way specified by you, write your own browser (It's quite easy, you can - for example - embed IE in C#.NET application) and run it instead of default system browser. You would have then full control on how your files are displayed.
Many web browsers respond to DDE messages, particularly the WWW_OpenURL message. Not sure about other browsers, but IE also responds to WWW_GetWindowInfo and WWW_Activate messages, which you could use to enumerate open windows and their URLs, and then activate a particular window.

Show text from opened file

I am not sure how to explain this, so I will give a scenario.
I want to write a program, which will be set as the default program for a file extension. When the program opens, it will output everything in the program. i.e file.txt contains the word hello, and when opened, my program opens and displays the word hello.
The idea is like Notepad. When a text file is opened, the contents are displayed. However, mine will be in a DOS window.
How can I achieve this?
Sorry if there is another question like this somewhere on here, TBH I am not too sure what this is called and thus can't search.
EDIT: My apologies. I am running Windows 7
If you open a file "with" your program (for instance using file type associations or by dragging the file onto the .exe), then the filename of the data file is passed as the command-line parameter to your application.
See:
What are the arguments to main() for?
I want to write a program, which will be set as the default program for a file extension.
See:
How File Associations Work
File Types and File Associations
You will associate your app with file extension and your app will do whatever it has to do.

Open an html page on an install CD with the default browser

I am trying to build an install CD with a custom MFC application set to autorun when the CD is inserted.
The instructions are included as a simple html page with images and links to PDF documents, all of which are located on the CD.
In the past I'd used the following to open the html page with the default browser:
ShellExecute(NULL, "open", <full path to .htm file including CD drive letter>, NULL, NULL, SHOWNORMAL);
But when testing with IE8 under vista I've encountered the following:
Explorer launches
The tab says 'Connecting'
Explorer dissappears
This has to do with protected mode, since if you turn off protected mode for the internet zone, the problem goes away.
The strange thing is that the problem only shows up once everthing is burned onto a CD. If I just run the autorun executable manually from my hard drive, the html page comes up just fine.
So I'm asking if there is anything specific I can do to fix this?
Or if there is another mechanism for opening URLs with the user's default browser that might not have this problem?
Since you can be reasonably sure that IE is installed you might want to execute
iexplore.exe [URL]
in your shellex call.
In case IE is not installed, check the return value to see if it isn't not found. If so do your original shellex call directly on the .htm file.
Alternatively, You can copy the .htm to a %TEMP%, and run the shellex call from there. That way it gets around the protected aceess from a "risky" location.
Can you capture and display the return code from the ShellExecute? That might give us a clue as to what is happening.
result = ShellExecute(...
Possible return codes are listed here:
http://support.microsoft.com/kb/238245
It says in your question that you are hard-coding the CD-ROM drive letter. Does the machine you are testing on have a different drive letter than the one you created the CD on?