From b6b82fee3f672f4212b6fc1ca2903b959a318610 Mon Sep 17 00:00:00 2001 From: Katayama Hirofumi MZ Date: Sun, 21 Jul 2024 10:08:16 +0900 Subject: [PATCH] [SHELL32] Refactor CShellLink::DoOpen (#7149) Reduce code. Modernize code. JIRA issue: N/A Use CStringW and CComHeapPtr for dynamic allocation. --- dll/win32/shell32/CShellLink.cpp | 55 ++++++++++++-------------------- 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/dll/win32/shell32/CShellLink.cpp b/dll/win32/shell32/CShellLink.cpp index e3064bbcdae..337bd9cbb95 100644 --- a/dll/win32/shell32/CShellLink.cpp +++ b/dll/win32/shell32/CShellLink.cpp @@ -6,7 +6,7 @@ * Copyright 2009 Andrew Hill * Copyright 2013 Dominik Hornung * Copyright 2017 Hermes Belusca-Maito - * Copyright 2018-2021 Katayama Hirofumi MZ + * Copyright 2018-2024 Katayama Hirofumi MZ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -2592,40 +2592,33 @@ HRESULT STDMETHODCALLTYPE CShellLink::InvokeCommand(LPCMINVOKECOMMANDINFO lpici) HRESULT CShellLink::DoOpen(LPCMINVOKECOMMANDINFO lpici) { - HRESULT hr; - LPWSTR args = NULL; - LPWSTR path = strdupW(m_sPath); BOOL unicode = lpici->cbSize >= FIELD_OFFSET(CMINVOKECOMMANDINFOEX, ptInvoke) && (lpici->fMask & CMIC_MASK_UNICODE); + CStringW args; + if (m_sArgs) + args = m_sArgs; + if (unicode) { LPCMINVOKECOMMANDINFOEX iciex = (LPCMINVOKECOMMANDINFOEX)lpici; - SIZE_T len = 2; - - if (m_sArgs) - len += wcslen(m_sArgs); - if (iciex->lpParametersW) - len += wcslen(iciex->lpParametersW); - - args = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); - *args = 0; - if (m_sArgs) - wcscat(args, m_sArgs); - if (iciex->lpParametersW) + if (iciex->lpParametersW && iciex->lpParametersW[0]) { - wcscat(args, L" "); - wcscat(args, iciex->lpParametersW); + args += L' '; + args += iciex->lpParametersW; } } - else if (m_sArgs != NULL) + else { - args = strdupW(m_sArgs); + CComHeapPtr pszParams; + if (lpici->lpParameters && lpici->lpParameters[0] && __SHCloneStrAtoW(&pszParams, lpici->lpParameters)) + { + args += L' '; + args += pszParams; + } } - SHELLEXECUTEINFOW sei; - ZeroMemory(&sei, sizeof(sei)); - sei.cbSize = sizeof(sei); + SHELLEXECUTEINFOW sei = { sizeof(sei) }; sei.fMask = SEE_MASK_HASLINKNAME | SEE_MASK_UNICODE | (lpici->fMask & (SEE_MASK_NOASYNC | SEE_MASK_ASYNCOK | SEE_MASK_FLAG_NO_UI)); if (m_pPidl) @@ -2635,7 +2628,7 @@ HRESULT CShellLink::DoOpen(LPCMINVOKECOMMANDINFO lpici) } else { - sei.lpFile = path; + sei.lpFile = m_sPath; } sei.lpParameters = args; sei.lpClass = m_sLinkPath; @@ -2643,19 +2636,11 @@ HRESULT CShellLink::DoOpen(LPCMINVOKECOMMANDINFO lpici) sei.lpDirectory = m_sWorkDir; sei.lpVerb = L"open"; - // HACK for ShellExecuteExW - if (m_sPath && wcsstr(m_sPath, L".cpl")) + // HACK for ShellExecuteExW: Change the default verb if this is a Control Panel applet + if (m_sPath && lstrcmpiW(PathFindExtensionW(m_sPath), L".cpl") == 0) sei.lpVerb = L"cplopen"; - if (ShellExecuteExW(&sei)) - hr = S_OK; - else - hr = E_FAIL; - - HeapFree(GetProcessHeap(), 0, args); - HeapFree(GetProcessHeap(), 0, path); - - return hr; + return (ShellExecuteExW(&sei) ? S_OK : E_FAIL); } HRESULT STDMETHODCALLTYPE CShellLink::GetCommandString(UINT_PTR idCmd, UINT uType, UINT* pwReserved, LPSTR pszName, UINT cchMax)