Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: XE5 Opening a file with the default program on Windows


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Apr 18, 2018 10:38 AM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Thomas Juntgen

Posts: 1
Registered: 11/2/02
XE5 Opening a file with the default program on Windows  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 17, 2018 11:37 PM
Hello all,

I want to open a file with the default program on Windows 10.
I used the command

UnicodeString path;
...
int ret = (int)ShellExecute(Handle, L"open", path.w_str(), 0, 0, SW_SHOWDEFAULT);

But using the code on a Windows 10 client an error occures sometimes.
"The key jpeg\shell\open\command is invalid"

Thanks
Thomas
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: XE5 Opening a file with the default program on Windows  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 18, 2018 10:38 AM   in response to: Thomas Juntgen in response to: Thomas Juntgen
Thomas Juntgen wrote:

I want to open a file with the default program on Windows 10.

The correct way to launch the default action using ShellExecute() is to
set the lpOperation parameter to NULL instead of L"open", eg:

int ret = (int) ShellExecute(Handle, NULL, path.w_str(), NULL, NULL,
SW_SHOWDEFAULT);


This is documented behavior:

https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153.aspx

lpOperation [in, optional]
Type: LPCTSTR

A pointer to a null-terminated string, referred to in this case as
a verb, that specifies the action to be performed. The set of available
verbs depends on the particular file or folder. Generally, the actions
available from an object's shortcut menu are available verbs. The
following verbs are commonly used:

...

NULL

The default verb is used, if available. If not, the "open" verb
is used. If neither verb is available, the system uses the first verb
listed in the registry.

Not all apps define an "open" verb, or use it as their default verb.
Specifying NULL will use the actual default verb of the registered app.

That being said, you really should be using ShellExecuteEx() instead,
as it provides more options on input, and provides more meaningful
error codes on failure:

SHELLEXECUTEINFO sei = {}
sei.cbSize = sizeof(sei);
sei.fMask = SEE_MASK_FLAG_NO_UI;
sei.hwnd = Handle;
sei.lpFile = path.w_str();
sei.nShow = SW_SHOWDEFAULT;
 
if (!ShellExecuteEx(&sei))
{
    DWORD err = GetLastError();
    ...
}


But using the code on a Windows 10 client an error occures sometimes.
"The key jpeg\shell\open\command is invalid"

Then clearly, either the registration for JPEG files is malformed in
the Registry, or the registration does not have an "open" verb defined.
Nothing you can do about it from your app either way. Detect the
failure, notify the user if needed, and move on.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02