Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This has worked for me without any errors:

ShellExecute(NULL, _T("open"), _T("C:\\Program Files\\...."), NULL, NULL, SW_SHOW);

The file i am opening here is a PDF file and its working fine. We know ShellExecute will open the file with whatever viewer the PDF file is associated with. But i am having a question,

1.) What if the user doesn't have an associated viewer for the PDF, In this case what would be the default PDF reader the ShellExecute will pick to open the PDF?

2.) What if the user doesn't have any acrobat or PDF reader or PDF viewer installed (he never has anything to open a PDF file)? How do i handle this case?
Posted

If the .pdf file extension is not associated with any program, ShellExecute will return with SE_ERR_NOASSOC.

If this happens, a nice way to handle it would be to do a new ShellExecute on the following command line:
rundll32.exe shell32.dll,OpenAs_RunDLL, followed by the name of your .pdf file. This will present the user with the Windows 'Open with' dialog, which allows to associate a program with this extension.

You could decide to do something else too though, f.e. give a MessageBox with the URL to download Acrobat Reader.
 
Share this answer
 
Comments
amarasat 10-Feb-11 11:03am    
I really like your suggestion:

But i am confused how do i use the ShellExecute on those commands u gave

<pre>ShellExecute(NULL, OpenAs_RunDLL, _T("C:\\Program Files\\...."), NULL, NULL, SW_SHOW); </pre>

is this what r u saying? Can you give me an example pls!!
[no name] 10-Feb-11 11:21am    
ShellExecute(0,0,"rundll32.exe","shell32.dll,OpenAs_RunDLL C:\\MyFile.pdf",0,SW_SHOW)
Where C:\\MyFile.pdf is the file name of the .pdf file.

Or in this case WinExec is probably easier;
WinExec("rundll32.exe shell32.dll,OpenAs_RunDLL C:\\MyFile.pdf",SW_SHOWDEFAULT);
Sergey Alexandrovich Kryukov 10-Feb-11 11:58am    
Correct, my 5.
--SA
Red.Devlab 11-Jul-16 7:03am    
this solution open select program for pdf.
But if I want open the PDF without the dialog selector? It's possible?
Thanks
Red.Devlab 11-Jul-16 8:18am    
ok, found!
If you use NULL inplace of "open" Windows open pdf file with the default program.
ShellExecute returns several errors. In your case you will get SE_ERR_NOASSOC. See the documentation for comeplete error list. http://msdn.microsoft.com/en-us/library/bb762153(v=vs.85).aspx[^]
 
Share this answer
 
Comments
amarasat 10-Feb-11 11:06am    
Thanks,

i never knew ShellExecute is having a return value, i thoguht only ShellExecuteEx has it!!
Sergey Alexandrovich Kryukov 10-Feb-11 11:57am    
I just wanted to up-vote by 5. The advice is correct and actually conducted important message to OP.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900