Results 1 to 6 of 6

Thread: Using ShellExecute

  1. #1
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Using ShellExecute

    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...

    Qt Code:
    1. #include "qt_windows.h"
    2. #include "qwindowdefs_win.h"
    3. #include <shellapi.h>
    4. ...
    5. long result = (long)ShellExecute(0, 0, reinterpret_cast<const WCHAR*>("test.csv"), 0, 0, SW_NORMAL);
    6. QMessageBox::critical(this, "Error", QString::number( result, 'f', 0 ) );
    To copy to clipboard, switch view to plain text mode 

    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-intere...ad01271-0.html for my ref.

  2. #2
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using ShellExecute

    Ok, I figured it out...

    Qt Code:
    1. QString pathDocument = "test.csv";
    2. long result = (long)ShellExecute(0, 0, reinterpret_cast<const WCHAR*>(pathDocument.utf16()), 0, 0, SW_NORMAL);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using ShellExecute

    Use QDesktopServises:penUrl instead of ShellExecute

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

  4. The following user says thank you to borisbn for this useful post:

    ShamusVW (22nd April 2010)

  5. #4
    Join Date
    Oct 2009
    Location
    South Africa
    Posts
    94
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using ShellExecute

    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.

  6. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using ShellExecute

    Qt Code:
    1. long result = (long)ShellExecute(0, 0, reinterpret_cast<const WCHAR*>("test.csv"), 0, 0, SW_NORMAL); // << Incorrect
    2. long result = (long)ShellExecute(0, 0, L"test.csv", 0, 0, SW_NORMAL); // << Correct
    To copy to clipboard, switch view to plain text mode 

    But QDesktopServices is much better and more portable.

    Qt Code:
    1. "Hello" << ASCII Text string
    2. L"Hello" << Unicode text string
    To copy to clipboard, switch view to plain text mode 

    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.

  7. #6
    Join Date
    Apr 2011
    Location
    Hungary
    Posts
    1
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using ShellExecute

    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:
    Qt Code:
    1. QString doing = "runas";
    2. QString proggi = qApp->applicationDirPath()+ "/Updater.exe";
    3.  
    4. int result = (int)ShellExecute(hwnd,
    5. reinterpret_cast<const WCHAR*>(doing.utf16()),
    6. reinterpret_cast<const WCHAR*>(proggi.utf16()),
    7. NULL,
    8. NULL,
    9. SW_SHOWNORMAL);
    To copy to clipboard, switch view to plain text mode 

    How can you this behave with "OpenUrl" ?

    Tenx

    Attila

Similar Threads

  1. use qprocess like shellexecute
    By kernel_panic in forum Qt Programming
    Replies: 7
    Last Post: 19th February 2007, 15:51

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.