Skip to content

Commit

Permalink
[WIN32] Migrate to ShellExecuteEx()
Browse files Browse the repository at this point in the history
I would like to suggest to migrate from ShellExecute() to ShellExecuteEx().
That function allows to write a more readable code, without the ugly that needs to test if returned value is greater than 32.
Supported platforms are:

    Windows 95: Supported.
    Windows 98: Supported.
    Windows NT: Required Windows NT 4.0 or later.
    Windows 2000 and newer: Supported.
    Windows CE: Requires Windows CE 1.0 or later. 

This fix comes from my port of SCUMMVM for Windows CE/Embedded/Mobile since this family of OSs support ShellExecuteEx(), but not ShellExecute().
  • Loading branch information
carlo-bramini committed May 1, 2022
1 parent 954cb53 commit d84947b
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions backends/platform/sdl/win32/win32.cpp
Expand Up @@ -159,8 +159,15 @@ bool OSystem_Win32::displayLogFile() {
// Try opening the log file with the default text editor
// log files should be registered as "txtfile" by default and thus open in the default text editor
TCHAR *tLogFilePath = Win32::stringToTchar(_logFilePath);
HINSTANCE shellExec = ShellExecute(getHwnd(), nullptr, tLogFilePath, nullptr, nullptr, SW_SHOWNORMAL);
if ((intptr_t)shellExec > 32) {
SHELLEXECUTEINFO sei;

memset(&sei, 0, sizeof(sei));
sei.fMask = SEE_MASK_FLAG_NO_UI;
sei.hwnd = getHwnd();
sei.lpFile = tLogFilePath;
sei.nShow = SW_SHOWNORMAL;

if (ShellExecuteEx(&sei)) {
free(tLogFilePath);
return true;
}
Expand Down Expand Up @@ -196,11 +203,20 @@ bool OSystem_Win32::displayLogFile() {

bool OSystem_Win32::openUrl(const Common::String &url) {
TCHAR *tUrl = Win32::stringToTchar(url);
HINSTANCE result = ShellExecute(getHwnd(), nullptr, tUrl, nullptr, nullptr, SW_SHOWNORMAL);
SHELLEXECUTEINFO sei;

memset(&sei, 0, sizeof(sei));
sei.cbSize = sizeof(sei);
sei.fMask = SEE_MASK_FLAG_NO_UI;
sei.hwnd = getHwnd();
sei.lpFile = tUrl;
sei.nShow = SW_SHOWNORMAL;

BOOL success = ShellExecuteEx(&sei);

free(tUrl);
// ShellExecute returns a value greater than 32 if successful
if ((intptr_t)result <= 32) {
warning("ShellExecute failed: error = %p", (void*)result);
if (!success) {
warning("ShellExecuteEx failed: error = %08lX", GetLastError());
return false;
}
return true;
Expand Down

0 comments on commit d84947b

Please sign in to comment.