Find icon from shortcut target if shortcut doesn't specify it (#6277)

Implements what I was suggesting in #6266 where if a shortcut doesn't
specify an icon, the shortcut target full path is used before searching
for a matching executable in the path.

## References

Found due to not getting the right icon in conhost from the Yori
installer.  It's fixed in the installer from
5af366b6a5
for all current users of conhost though, so this PR is just trying to
minimize surprises for the next guy.

## Detailed Description of the Pull Request / Additional comments

I know conhost and shortcut settings aren't really the team's focus
which is why I'm doing this.  I understand though if there's a better
way or there are factors that I hadn't considered.  Note that the path
searching code is used when programs are launched without using a
shortcut, and it will match if the working directory of the shortcut is
the directory containing the executable.

## Validation Steps Performed

Created a shortcut that didn't specify an icon to a binary that wasn't
in the path, and verified that the icon in the upper left of the console
window could resolve correctly when opening the shortcut.  I'm not aware
of a way to get into this path (of launching via a shortcut to a command
line process) without replacing the system conhost, which is what I did
to verify it.  In order to diagnose it, I used hardcoded DebugBreak()
since even ImageFileExecutionOptions didn't like running against conhost-
is there are better way to debug and test these cases without being so
invasive on the system?

Closes #6266
This commit is contained in:
Malcolm Smith 2020-06-01 10:19:05 -07:00 committed by GitHub
parent af56088cb6
commit 601286ac69
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 15 deletions

View file

@ -63,6 +63,7 @@ void SystemConfigurationProvider::GetSettingsFromLink(
_In_ PCWSTR pwszAppName)
{
CONSOLE_INFORMATION& gci = ServiceLocator::LocateGlobals().getConsoleInformation();
WCHAR wszLinkTarget[MAX_PATH] = { 0 };
WCHAR wszIconLocation[MAX_PATH] = { 0 };
int iIconIndex = 0;
@ -96,6 +97,8 @@ void SystemConfigurationProvider::GetSettingsFromLink(
&fReadConsoleProperties,
wszShortcutTitle,
ARRAYSIZE(wszShortcutTitle),
wszLinkTarget,
ARRAYSIZE(wszLinkTarget),
wszIconLocation,
ARRAYSIZE(wszIconLocation),
&iIconIndex,
@ -111,6 +114,12 @@ void SystemConfigurationProvider::GetSettingsFromLink(
dwHotKey = wHotKey;
pLinkSettings->SetHotKey(dwHotKey);
if (wszLinkTarget[0] != L'\0')
{
// guarantee null termination to make OACR happy.
wszLinkTarget[ARRAYSIZE(wszLinkTarget) - 1] = L'\0';
}
// if we got a title, use it. even on overall link value load failure, the title will be correct if
// filled out.
if (wszShortcutTitle[0] != L'\0')
@ -152,21 +161,28 @@ void SystemConfigurationProvider::GetSettingsFromLink(
// Go get the icon
if (wszIconLocation[0] == L'\0')
{
// search for the application along the path so that we can load its icons (if we didn't find one explicitly in
// the shortcut)
const DWORD dwLinkLen = SearchPathW(pwszCurrDir, pwszAppName, nullptr, ARRAYSIZE(wszIconLocation), wszIconLocation, nullptr);
// If we cannot find the application in the path, then try to fall back and see if the window title is a valid path and use that.
if (dwLinkLen <= 0 || dwLinkLen > sizeof(wszIconLocation))
if (PathFileExists(wszLinkTarget))
{
if (PathFileExistsW(pwszTitle) && (wcslen(pwszTitle) < sizeof(wszIconLocation)))
StringCchCopyW(wszIconLocation, ARRAYSIZE(wszIconLocation), wszLinkTarget);
}
else
{
// search for the application along the path so that we can load its icons (if we didn't find one explicitly in
// the shortcut)
const DWORD dwLinkLen = SearchPathW(pwszCurrDir, pwszAppName, nullptr, ARRAYSIZE(wszIconLocation), wszIconLocation, nullptr);
// If we cannot find the application in the path, then try to fall back and see if the window title is a valid path and use that.
if (dwLinkLen <= 0 || dwLinkLen > sizeof(wszIconLocation))
{
StringCchCopyW(wszIconLocation, ARRAYSIZE(wszIconLocation), pwszTitle);
}
else
{
// If all else fails, just stick the app name into the path and try to resolve just the app name.
StringCchCopyW(wszIconLocation, ARRAYSIZE(wszIconLocation), pwszAppName);
if (PathFileExistsW(pwszTitle) && (wcslen(pwszTitle) < sizeof(wszIconLocation)))
{
StringCchCopyW(wszIconLocation, ARRAYSIZE(wszIconLocation), pwszTitle);
}
else
{
// If all else fails, just stick the app name into the path and try to resolve just the app name.
StringCchCopyW(wszIconLocation, ARRAYSIZE(wszIconLocation), pwszAppName);
}
}
}
}

View file

@ -118,7 +118,7 @@ private:
// Not all console shortcuts have console-specific properties. We just take the registry defaults in
// those cases.
BOOL readSettings = FALSE;
NTSTATUS s = ShortcutSerialization::s_GetLinkValues(gpStateInfo, &readSettings, nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr);
NTSTATUS s = ShortcutSerialization::s_GetLinkValues(gpStateInfo, &readSettings, nullptr, 0, nullptr, 0, nullptr, 0, nullptr, nullptr, nullptr);
hr = HRESULT_FROM_NT(s);
}
else

View file

@ -344,6 +344,8 @@ void ShortcutSerialization::s_GetLinkTitle(_In_ PCWSTR pwszShortcutFilename,
_Out_ BOOL* const pfReadConsoleProperties,
_Out_writes_opt_(cchShortcutTitle) PWSTR pwszShortcutTitle,
const size_t cchShortcutTitle,
_Out_writes_opt_(cchLinkTarget) PWSTR pwszLinkTarget,
const size_t cchLinkTarget,
_Out_writes_opt_(cchIconLocation) PWSTR pwszIconLocation,
const size_t cchIconLocation,
_Out_opt_ int* const piIcon,
@ -357,6 +359,11 @@ void ShortcutSerialization::s_GetLinkTitle(_In_ PCWSTR pwszShortcutFilename,
pwszShortcutTitle[0] = L'\0';
}
if (pwszLinkTarget && cchLinkTarget > 0)
{
pwszLinkTarget[0] = L'\0';
}
if (pwszIconLocation && cchIconLocation > 0)
{
pwszIconLocation[0] = L'\0';
@ -374,7 +381,12 @@ void ShortcutSerialization::s_GetLinkTitle(_In_ PCWSTR pwszShortcutFilename,
s_GetLinkTitle(pStateInfo->LinkTitle, pwszShortcutTitle, cchShortcutTitle);
}
if (pwszIconLocation && piIcon)
if (pwszLinkTarget)
{
hr = psl->GetPath(pwszLinkTarget, static_cast<int>(cchLinkTarget), NULL, 0);
}
if (SUCCEEDED(hr) && pwszIconLocation && piIcon)
{
hr = psl->GetIconLocation(pwszIconLocation, static_cast<int>(cchIconLocation), piIcon);
}

View file

@ -30,6 +30,8 @@ public:
_Out_ BOOL* const pfReadConsoleProperties,
_Out_writes_opt_(cchShortcutTitle) PWSTR pwszShortcutTitle,
const size_t cchShortcutTitle,
_Out_writes_opt_(cchLinkTarget) PWSTR pwszLinkTarget,
const size_t cchLinkTarget,
_Out_writes_opt_(cchIconLocation) PWSTR pwszIconLocation,
const size_t cchIconLocation,
_Out_opt_ int* const piIcon,