PDA

View Full Version : Using ShellExecute



ShamusVW
21st April 2010, 13:35
I am trying to use ShellExecute in my program to open up various files, using the default program depending on the file extension. After reading various posts, I have the following...


#include "qt_windows.h"
#include "qwindowdefs_win.h"
#include <shellapi.h>
...
long result = (long)ShellExecute(0, 0, reinterpret_cast<const WCHAR*>("test.csv"), 0, 0, SW_NORMAL);
QMessageBox::critical(this, "Error", QString::number( result, 'f', 0 ) );

Now the error I get is always "2", which if I'm correct, is "file not found", possibly "path no found". I have made sure that the file is in the same directory as my executable. I have even tried "c:\test.csv", as well as "c:/test.csv", making sure the file is in the root directory.

In another post I read that I need to use "( TCHAR * ) qt_winTchar" since it is expecting a wide character string. But that is apparently old, and I should use ucs2, but I only have fromUcs4(). I really am confused.
See http://lists.trolltech.com/qt-interest/2003-02/thread01271-0.html for my ref.

ShamusVW
21st April 2010, 13:44
Ok, I figured it out...


QString pathDocument = "test.csv";
long result = (long)ShellExecute(0, 0, reinterpret_cast<const WCHAR*>(pathDocument.utf16()), 0, 0, SW_NORMAL);

borisbn
21st April 2010, 19:25
Use QDesktopServises::openUrl (doc.qt.nokia.com/4.6/qdesktopservices.html) instead of ShellExecute

P.S. In shellExecute tou should use L"my path.ext" but not (WCHAR_T) operator

ShamusVW
22nd April 2010, 06:52
Regarding ShellExecute and your suggestion, can you type out the full command with usage of L"my path.ext"? Not exactly sure what you mean.
As to QDesktopServices, thank you very much! Much easier! Wish I knew about it before, but oh well.

squidge
22nd April 2010, 07:52
long result = (long)ShellExecute(0, 0, reinterpret_cast<const WCHAR*>("test.csv"), 0, 0, SW_NORMAL); // << Incorrect
long result = (long)ShellExecute(0, 0, L"test.csv", 0, 0, SW_NORMAL); // << Correct


But QDesktopServices is much better and more portable.



"Hello" << ASCII Text string
L"Hello" << Unicode text string


Also, if you build a non-unicode version of your app for some reason, all strings prefixed with L will immediately generate ascii string instead of unicode strings.

Gecsevar
17th April 2011, 11:46
Hi!

This thread helps me a lot.
On Windows >= Vista I'm programming a simple updater. It needs to "run as administrator, to get the preferred rights.
Now, I use this:


QString doing = "runas";
QString proggi = qApp->applicationDirPath()+ "/Updater.exe";

int result = (int)ShellExecute(hwnd,
reinterpret_cast<const WCHAR*>(doing.utf16()),
reinterpret_cast<const WCHAR*>(proggi.utf16()),
NULL,
NULL,
SW_SHOWNORMAL);


How can you this behave with "OpenUrl" ?

Tenx

Attila