| 1 |
// Copyright (C) 2020-2026 Anders Kjersem |
| 2 |
// |
| 3 |
// This file is a part of NSIS. |
| 4 |
// |
| 5 |
// Licensed under the zlib/libpng license (the "License"); |
| 6 |
// you may not use this file except in compliance with the License. |
| 7 |
// |
| 8 |
// Licence details can be found in the file COPYING. |
| 9 |
// |
| 10 |
// This software is provided 'as-is', without any express or implied |
| 11 |
// warranty. |
| 12 |
|
| 13 |
#include "makensisw.h" |
| 14 |
#include <windows.h> |
| 15 |
#include <windowsx.h> |
| 16 |
#include <shlobj.h> |
| 17 |
#include <shlwapi.h> |
| 18 |
#include "utils.h" |
| 19 |
#include "resource.h" |
| 20 |
|
| 21 |
enum { reg_mui_string_truncate = 0x01 }; |
| 22 |
|
| 23 |
static void StrTToW(LPCTSTR Src, LPWSTR Dst, UINT cch) |
| 24 |
{ |
| 25 |
if (sizeof(*Src) == sizeof(*Dst)) |
| 26 |
lstrcpynW(Dst, (LPCWSTR) Src, cch); |
| 27 |
else |
| 28 |
MultiByteToWideChar(CP_ACP, 0, (LPCSTR) Src, -1, Dst, cch); |
| 29 |
} |
| 30 |
|
| 31 |
template<class T> static LPWSTR SmartStrTToW(T*Src, LPWSTR Dst, UINT cch) |
| 32 |
{ |
| 33 |
if (sizeof(T) == sizeof(*Dst)) return (LPWSTR) Src; |
| 34 |
return (StrTToW(Src, Dst, cch), Dst); |
| 35 |
} |
| 36 |
|
| 37 |
#define StrBase10ToSInt(s) StrToSInt((s), 10) |
| 38 |
template<class T> static int WINAPI StrToSInt(const T*Str, UINT Base = 0) |
| 39 |
{ |
| 40 |
if (Base && !(*Str >= '0' && *Str <= '9') && *Str != '-') return 0; // Don't allow leading space |
| 41 |
int v, succ; |
| 42 |
if (sizeof(*Str) > 1) |
| 43 |
succ = StrToIntExW((WCHAR*) Str, Base != 10 ? STIF_SUPPORT_HEX : STIF_DEFAULT, &v); |
| 44 |
else |
| 45 |
succ = StrToIntExA((CHAR *) Str, Base != 10 ? STIF_SUPPORT_HEX : STIF_DEFAULT, &v); |
| 46 |
return succ ? v : 0; // Not full base support, we only need 10 and 16 |
| 47 |
} |
| 48 |
|
| 49 |
template<class T> static ULARGE_INTEGER PathParseIconLocationEx(T*Path) |
| 50 |
{ |
| 51 |
ULARGE_INTEGER li; |
| 52 |
int idx = 0; |
| 53 |
SIZE_T i, comma = 0; |
| 54 |
for (i = 0; Path[i]; ++i) |
| 55 |
{ |
| 56 |
if (Path[i] == ',') comma = i; |
| 57 |
if (FSPath::IsAgnosticSeparator(Path[i])) comma = 0; |
| 58 |
} |
| 59 |
if (comma) |
| 60 |
{ |
| 61 |
Path[comma] = T('\0'); |
| 62 |
idx = StrBase10ToSInt(Path + comma + 1); |
| 63 |
} |
| 64 |
return (li.HighPart = (UINT) comma, li.LowPart = idx, li); |
| 65 |
} |
| 66 |
|
| 67 |
#ifndef _WIN64 |
| 68 |
static int WINAPI PathParseIconLocationFallback(LPSTR Path) |
| 69 |
{ |
| 70 |
ULARGE_INTEGER li = PathParseIconLocationEx(Path); |
| 71 |
return li.LowPart; |
| 72 |
} |
| 73 |
#endif |
| 74 |
|
| 75 |
static HRESULT GetSpecialFolderPath(HWND hWnd, LPTSTR Buf, UINT csidl) |
| 76 |
{ |
| 77 |
UINT create = csidl & CSIDL_FLAG_CREATE, succ, ec; |
| 78 |
csidl &= ~CSIDL_FLAG_CREATE; |
| 79 |
if (SupportsWNT4() || SupportsW95()) |
| 80 |
{ |
| 81 |
LPITEMIDLIST pidl; |
| 82 |
HRESULT hr = SHGetSpecialFolderLocation(hWnd, csidl, &pidl); |
| 83 |
if (SUCCEEDED(hr)) |
| 84 |
{ |
| 85 |
hr = SHGetPathFromIDList(pidl, Buf) ? S_OK : E_FAIL, CoTaskMemFree(pidl); |
| 86 |
if (SUCCEEDED(hr) && create) |
| 87 |
{ |
| 88 |
succ = CreateDirectory(Buf, NULL); |
| 89 |
hr = (succ || (ec = GetLastError()) == ERROR_ALREADY_EXISTS) ? S_OK : HRESULT_FROM_WIN32(ec); |
| 90 |
} |
| 91 |
} |
| 92 |
return hr; |
| 93 |
} |
| 94 |
succ = SHGetSpecialFolderPath(hWnd, Buf, csidl, false); |
| 95 |
if (!succ && create) succ = SHGetSpecialFolderPath(hWnd, Buf, csidl, create); |
| 96 |
return succ ? S_OK : E_FAIL; |
| 97 |
} |
| 98 |
|
| 99 |
static HICON LoadIconFromLocation(LPCTSTR Path, int Idx) |
| 100 |
{ |
| 101 |
// web.archive.org/web/201901/https://blogs.msdn.microsoft.com/oldnewthing/20100505-00/?p=14153# -1 is special |
| 102 |
HICON hIco = ExtractIcon(NULL, Path, Idx == -1 ? 0 : Idx); |
| 103 |
if (!hIco && !GetLastError()) SetLastError(ERROR_NOT_FOUND); |
| 104 |
return hIco; |
| 105 |
} |
| 106 |
|
| 107 |
template<class T> static void AppendText(HWND hEdit, const T*Str) |
| 108 |
{ |
| 109 |
SNDMSG(hEdit, EM_SETSEL, 0, -1), SNDMSG(hEdit, EM_SETSEL, -1, -1); |
| 110 |
if (sizeof(T) > 1) |
| 111 |
SendMessageW(hEdit, EM_REPLACESEL, 0, (SIZE_T) Str); |
| 112 |
else |
| 113 |
SendMessageA(hEdit, EM_REPLACESEL, 0, (SIZE_T) Str); |
| 114 |
} |
| 115 |
|
| 116 |
static int AddComboStringWithData(HWND hWnd, LPCSTR Str, SIZE_T Data) |
| 117 |
{ |
| 118 |
int idx = (int) SendMessageA(hWnd, CB_INSERTSTRING, (INT_PTR) -1, (SIZE_T) const_cast<LPCSTR>(Str)); |
| 119 |
if (idx >= 0) SendMessage(hWnd, CB_SETITEMDATA, idx, Data); |
| 120 |
return idx; |
| 121 |
} |
| 122 |
|
| 123 |
template<class I, class O> static HKEY ParseRegPath(I*In, O*&Path, O*&Name) |
| 124 |
{ |
| 125 |
HKEY hKey = GetRegRootKey(In); |
| 126 |
I*pp = 0, *pn = 0; |
| 127 |
if (hKey) |
| 128 |
{ |
| 129 |
SIZE_T i; |
| 130 |
for (i = 0; In[i]; ++i) |
| 131 |
if (In[i] == I('\\')) |
| 132 |
(!pp ? pp : pn) = &In[i + 1]; // First separator after HK* or last separator |
| 133 |
if (pn) pn[-1] = I('\0'); // Split path and name |
| 134 |
} |
| 135 |
return (Path = const_cast<O*>(pp), Name = const_cast<O*>(pn), hKey); |
| 136 |
} |
| 137 |
|
| 138 |
HRESULT (WINAPI*g_RLMSOld)(HKEY hKey, LPCWSTR pszValue, LPWSTR pszOutBuf, DWORD cbOutBuf); |
| 139 |
|
| 140 |
static LONG WINAPI RegLoadMUIStringFallbackW(HKEY hKey, LPCWSTR Name, LPWSTR Out, DWORD cbOutBuf, LPDWORD pcbData, DWORD Flags, LPCSTR pszDirectory) |
| 141 |
{ |
| 142 |
if ((Flags & reg_mui_string_truncate) && pcbData) return ERROR_INVALID_PARAMETER; |
| 143 |
if (g_RLMSOld) |
| 144 |
{ |
| 145 |
HRESULT hr = g_RLMSOld(hKey, Name, Out, cbOutBuf); |
| 146 |
if (SUCCEEDED(hr)) |
| 147 |
{ |
| 148 |
if (pcbData) *pcbData = (StrLenT(Out) + 1) * sizeof(*Out); |
| 149 |
return ERROR_SUCCESS; |
| 150 |
} |
| 151 |
if (pcbData) *pcbData = (cbOutBuf|1) * 2; // We have no real value to set, caller just has to loop until success |
| 152 |
return hr; // Note: Not converting E_* here, FormatMessage knows what to do. |
| 153 |
} |
| 154 |
return ERROR_NOT_SUPPORTED; |
| 155 |
} |
| 156 |
|
| 157 |
enum { LM_SHGLN, LM_SHLIS, LM_RLMS, LM_PPIL, LM_FMTMSG }; |
| 158 |
LPCSTR g_ModeLbl[] = { "Path:", "Path:", "Registry:", "Path:", "[Path,]Number:" }; |
| 159 |
|
| 160 |
struct DIALOGDATA { |
| 161 |
HRESULT (WINAPI*SHLIS)(LPCWSTR s, LPWSTR o, UINT cch, PVOID*ppvReserved); |
| 162 |
HRESULT (WINAPI*SHGLN)(PCWSTR p, PWSTR m, UINT cch, int*rid); |
| 163 |
LONG (WINAPI*RLMS)(HKEY hKey, LPCWSTR pszValue, LPWSTR pszOutBuf, DWORD cbOutBuf, LPDWORD pcbData, DWORD Flags, LPCSTR pszDirectory); |
| 164 |
int (WINAPI*PPIL)(LPTSTR p); |
| 165 |
void*OrgParentDlgData; |
| 166 |
HWND hMode, hExtra, hOutTxt, hOutIco; |
| 167 |
|
| 168 |
DIALOGDATA() { extern void* g_ModalDlgData; OrgParentDlgData = g_ModalDlgData, g_ModalDlgData = this; } |
| 169 |
~DIALOGDATA() { extern void* g_ModalDlgData; g_ModalDlgData = OrgParentDlgData; } |
| 170 |
static DIALOGDATA* Get(HWND hDlg) { return (DIALOGDATA*) g_ModalDlgData; } |
| 171 |
static inline void Set(HWND hDlg, DIALOGDATA*p) { } |
| 172 |
}; |
| 173 |
|
| 174 |
static INT_PTR CALLBACK LookupDlgProc(HWND hDlg, UINT Msg, WPARAM WParam, LPARAM LParam) |
| 175 |
{ |
| 176 |
DIALOGDATA*pDD = DIALOGDATA::Get(hDlg); |
| 177 |
int idx = pDD ? (int) SendMessage(pDD->hMode, CB_GETCURSEL, 0, 0) : 0; |
| 178 |
UINT mode = pDD ? (UINT) SendMessage(pDD->hMode, CB_GETITEMDATA, idx, 0) : 0; |
| 179 |
HRESULT hr; |
| 180 |
TCHAR buf[MAX_PATH + 1 + 6]; // Path + ',' + resid |
| 181 |
|
| 182 |
switch(Msg) |
| 183 |
{ |
| 184 |
case WM_SIZE: |
| 185 |
if (WParam == SIZE_MAXIMIZED) ShowWindow(hDlg, SW_SHOWNOACTIVATE); // Disallow STARTF_USESHOWWINDOW+SW_MAXIMIZE |
| 186 |
break; |
| 187 |
case WM_INITDIALOG: |
| 188 |
DIALOGDATA::Set(hDlg, (pDD = (DIALOGDATA*) LParam)); |
| 189 |
CenterOnParent(hDlg); |
| 190 |
pDD->hMode = GetDlgItem(hDlg, IDC_LUMODE); |
| 191 |
pDD->hExtra = GetDlgItem(hDlg, IDC_LUEXTRATEXT); |
| 192 |
pDD->hOutTxt = GetDlgItem(hDlg, IDC_LUOUTPUTTEXT); |
| 193 |
pDD->hOutIco = CreateWindowEx(WS_EX_TRANSPARENT, _T("STATIC"), 0, WS_CHILD|WS_VISIBLE|SS_ICON|SS_CENTERIMAGE|SS_REALSIZECONTROL, 0, 0, 0, 0, pDD->hOutTxt, 0, 0, 0); |
| 194 |
if (pDD->SHLIS) AddComboStringWithData(pDD->hMode, "SHLoadIndirectString", LM_SHLIS); |
| 195 |
if (pDD->RLMS) AddComboStringWithData(pDD->hMode, "RegLoadMUIString", LM_RLMS); |
| 196 |
if (pDD->SHGLN) AddComboStringWithData(pDD->hMode, "SHGetLocalizedName", LM_SHGLN); |
| 197 |
if (pDD->PPIL) AddComboStringWithData(pDD->hMode, "PathParseIconLocation", LM_PPIL); |
| 198 |
AddComboStringWithData(pDD->hMode, "FormatMessage", LM_FMTMSG); |
| 199 |
SNDMSG(pDD->hExtra, EM_LIMITTEXT, COUNTOF(buf), 0); |
| 200 |
SNDMSG(pDD->hMode, CB_SETCURSEL, (SIZE_T) 0, 0), SNDMSG(hDlg, WM_COMMAND, MAKELONG(IDC_LUMODE, CBN_SELENDOK), (SIZE_T) pDD->hMode); |
| 201 |
return TRUE; |
| 202 |
case WM_CLOSE: close: |
| 203 |
return EndDialog(hDlg, 0); |
| 204 |
case WM_DESTROY: |
| 205 |
{ |
| 206 |
HICON hOld = (HICON) SNDMSG(pDD->hOutIco, STM_SETICON, (SIZE_T) NULL, 0); |
| 207 |
if (hOld) DestroyIcon(hOld); |
| 208 |
} |
| 209 |
break; |
| 210 |
case WM_COMMAND: |
| 211 |
switch((UINT) WParam) |
| 212 |
{ |
| 213 |
case IDCANCEL: |
| 214 |
goto close; |
| 215 |
case MAKELONG(IDC_LUMODE, CBN_SELENDOK): |
| 216 |
{ |
| 217 |
SendMessageA(GetDlgItem(hDlg, IDC_LUEXTRALABEL), WM_SETTEXT, 0, (SIZE_T) const_cast<LPCSTR>(g_ModeLbl[mode])); |
| 218 |
LPCTSTR extra = buf; |
| 219 |
*buf = '\0'; |
| 220 |
switch(mode) |
| 221 |
{ |
| 222 |
case LM_SHGLN: |
| 223 |
GetSpecialFolderPath(NULL, buf, CSIDL_COMMON_DESKTOPDIRECTORY); |
| 224 |
break; |
| 225 |
case LM_SHLIS: |
| 226 |
extra = _T("@shell32,-4097"); |
| 227 |
break; |
| 228 |
case LM_RLMS: |
| 229 |
extra = _T("HKCR\\AudioCD\\shell\\play\\MUIVerb"); |
| 230 |
break; |
| 231 |
case LM_PPIL: |
| 232 |
extra = _T("%WINDIR%\\Explorer.exe,-101"); |
| 233 |
break; |
| 234 |
case LM_FMTMSG: |
| 235 |
extra = _T("wininet.dll,12005"); |
| 236 |
break; |
| 237 |
} |
| 238 |
ShowWindow(pDD->hOutIco, SW_HIDE); |
| 239 |
SetWindowText(pDD->hExtra, extra); |
| 240 |
} |
| 241 |
break; |
| 242 |
case MAKELONG(IDC_LUEXTRATEXT, EN_UPDATE): |
| 243 |
SendMessage(pDD->hExtra, WM_GETTEXT, COUNTOF(buf), (SIZE_T) buf); |
| 244 |
SetWindowText(pDD->hOutTxt, _T("")); |
| 245 |
switch(mode) |
| 246 |
{ |
| 247 |
case LM_SHGLN: |
| 248 |
{ |
| 249 |
int resid; |
| 250 |
WCHAR path[COUNTOF(buf)], mod[MAX_PATH], *pis; |
| 251 |
pis = SmartStrTToW(buf, path, COUNTOF(path)); |
| 252 |
hr = pDD->SHGLN(pis, mod, COUNTOF(mod), &resid); |
| 253 |
if (SUCCEEDED(hr)) |
| 254 |
{ |
| 255 |
AppendText(pDD->hOutTxt, mod); |
| 256 |
AppendText(pDD->hOutTxt, (wsprintfW(path, L",%d\r\n\r\n", resid), path)); |
| 257 |
if (ExpandEnvironmentStringsW(mod, path, COUNTOF(path))) |
| 258 |
{ |
| 259 |
if (HMODULE hMod = LoadLibraryExW(path, NULL, LOAD_LIBRARY_AS_DATAFILE)) |
| 260 |
{ |
| 261 |
if (LoadStringW(hMod, resid, mod, COUNTOF(mod))) |
| 262 |
{ |
| 263 |
AppendText(pDD->hOutTxt, mod); |
| 264 |
} |
| 265 |
FreeLibrary(hMod); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
else die_hr: |
| 270 |
{ |
| 271 |
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hr, 0, buf, COUNTOF(buf), NULL); |
| 272 |
AppendText(pDD->hOutTxt, (wsprintfW(path, L"%#x: ", hr), path)); |
| 273 |
AppendText(pDD->hOutTxt, buf); |
| 274 |
} |
| 275 |
} |
| 276 |
break; |
| 277 |
case LM_SHLIS: |
| 278 |
{ |
| 279 |
WCHAR is[COUNTOF(buf)], os[MAX_PATH], *pis; |
| 280 |
pis = SmartStrTToW(buf, is, COUNTOF(is)); |
| 281 |
hr = pDD->SHLIS(pis, os, COUNTOF(os), NULL); |
| 282 |
if (FAILED(hr)) goto die_hr; |
| 283 |
AppendText(pDD->hOutTxt, os); |
| 284 |
} |
| 285 |
break; |
| 286 |
case LM_RLMS: |
| 287 |
{ |
| 288 |
UINT ec; |
| 289 |
TCHAR *pkey, *pname; |
| 290 |
if (HKEY hKey = ParseRegPath(buf, pkey, pname)) |
| 291 |
{ |
| 292 |
if (!(ec = RegOpenKeyForReading(hKey, pkey, &hKey))) |
| 293 |
{ |
| 294 |
WCHAR namebuf[255+!0], outbuf[MAX_PATH], *pwn = SmartStrTToW(pname, namebuf, COUNTOF(namebuf)); |
| 295 |
ec = pDD->RLMS(hKey, pwn, outbuf, sizeof(outbuf), NULL, reg_mui_string_truncate, NULL); |
| 296 |
if (!ec) AppendText(pDD->hOutTxt, outbuf); |
| 297 |
RegCloseKey(hKey); |
| 298 |
} |
| 299 |
if (FAILED(hr = HRESULT_FROM_WIN32(ec))) goto die_hr; |
| 300 |
} |
| 301 |
} |
| 302 |
break; |
| 303 |
case LM_PPIL: |
| 304 |
{ |
| 305 |
RECT r; |
| 306 |
int idx = pDD->PPIL(buf); |
| 307 |
HICON hIco = LoadIconFromLocation(buf, idx), hOld; |
| 308 |
hr = GetLastError(); |
| 309 |
GetClientRect(pDD->hOutTxt, &r); |
| 310 |
SetWindowPos(pDD->hOutIco, HWND_TOP, 0, 0, r.right, r.bottom, SWP_NOACTIVATE|(hIco ? SWP_SHOWWINDOW : SWP_HIDEWINDOW)); |
| 311 |
hOld = (HICON) SNDMSG(pDD->hOutIco, STM_SETICON, (SIZE_T) hIco, 0); |
| 312 |
if (hOld) DestroyIcon(hOld); |
| 313 |
if (!hIco) goto die_hr; |
| 314 |
} |
| 315 |
break; |
| 316 |
case LM_FMTMSG: |
| 317 |
{ |
| 318 |
UINT flags = FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, succ = false; |
| 319 |
ULARGE_INTEGER li = PathParseIconLocationEx(buf); |
| 320 |
LPCTSTR numstr = buf; |
| 321 |
HMODULE hMod = 0; |
| 322 |
if (li.HighPart) |
| 323 |
{ |
| 324 |
numstr = buf + ++li.HighPart; |
| 325 |
hMod = LoadLibraryEx(buf, 0, LOAD_LIBRARY_AS_DATAFILE), flags |= FORMAT_MESSAGE_FROM_HMODULE; |
| 326 |
if (!hMod) goto badmsgmod; |
| 327 |
} |
| 328 |
hr = StrToSInt(numstr); |
| 329 |
succ = FormatMessage(flags, hMod, hr, 0, buf, COUNTOF(buf), NULL) != 0; |
| 330 |
badmsgmod: |
| 331 |
hr = GetLastError(); |
| 332 |
if (hMod) FreeLibrary(hMod); |
| 333 |
if (!succ) goto die_hr; |
| 334 |
SetWindowText(pDD->hOutTxt, buf); |
| 335 |
} |
| 336 |
break; |
| 337 |
} |
| 338 |
break; |
| 339 |
} |
| 340 |
break; |
| 341 |
} |
| 342 |
return FALSE; |
| 343 |
} |
| 344 |
|
| 345 |
INT_PTR ShowLookupDialog(HWND hOwner) |
| 346 |
{ |
| 347 |
DIALOGDATA dd; |
| 348 |
(FARPROC&) dd.SHLIS = GetSysProcAddr("SHLWAPI", "SHLoadIndirectString"); // WXP+ |
| 349 |
(FARPROC&) dd.SHGLN = GetSysProcAddr("SHELL32", "SHGetLocalizedName"); // WVista+ |
| 350 |
(FARPROC&) dd.RLMS = GetSysProcAddr("ADVAPI32", "RegLoadMUIStringW"); // WVista+ Note: RegLoadMUIStringA always returns ERROR_CALL_NOT_IMPLEMENTED |
| 351 |
if (!dd.RLMS && ((FARPROC&) g_RLMSOld = GetSysProcAddr("SHLWAPI", (LPCSTR) 439))) dd.RLMS = RegLoadMUIStringFallbackW; // W98SE+,IE5+ |
| 352 |
(FARPROC&) dd.PPIL = |
| 353 |
#ifdef _WIN64 |
| 354 |
(FARPROC) PathParseIconLocation; |
| 355 |
#else |
| 356 |
GetSysProcAddr("SHLWAPI", sizeof(TCHAR) == 1 ? "PathParseIconLocationA" : "PathParseIconLocationW"); // W95OSR2+,IE3.1+ |
| 357 |
if (!dd.PPIL || (SupportsWNT4() || IsWin9598ME())) |
| 358 |
{ |
| 359 |
(FARPROC&) dd.PPIL = GetSysProcAddr("SHELL32", (LPCSTR) 249); // WNT4+ PathParseIconLocationT |
| 360 |
if (sizeof(TCHAR) == 1) (FARPROC&) dd.PPIL = (FARPROC) PathParseIconLocationFallback; |
| 361 |
} |
| 362 |
#endif |
| 363 |
|
| 364 |
return DialogBoxParam(HINST_APPLICATION, MAKEINTRESOURCE(DLG_LOOKUP), hOwner, LookupDlgProc, (LPARAM) &dd); |
| 365 |
} |