Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

497 lines
15 KiB

  1. #include "shellprv.h"
  2. #pragma hdrstop
  3. #include "help.h"
  4. #define DWORDUP(x) (((x)+3)&~3)
  5. #define VerKeyToValue(lpKey) (lpKey + DWORDUP(lstrlen(lpKey)+1))
  6. #pragma warning(disable: 4200) // zero size array in struct
  7. // magic undoced explort from version.dll
  8. STDAPI_(BOOL) VerQueryValueIndexW(const void *pBlock, LPTSTR lpSubBlock, DWORD dwIndex, void **ppBuffer, void **ppValue, PUINT puLen);
  9. #ifdef UNICODE
  10. #define VerQueryValueIndex VerQueryValueIndexW
  11. #endif
  12. typedef struct
  13. {
  14. WORD wTotLen;
  15. WORD wValLen;
  16. TCHAR szKey[];
  17. } SHELLVERBLOCK, *LPSHELLVERBLOCK;
  18. // Following code is copied from fileman\wfdlgs2.c
  19. // The following data structure associates a version stamp datum
  20. // name (which is not localized) with a string ID. This is so we
  21. // can show translations of these names to the user.
  22. struct vertbl {
  23. TCHAR const *pszName;
  24. short idString;
  25. };
  26. // Note that version stamp datum names are NEVER internationalized,
  27. // so the following literal strings are just fine.
  28. const struct vertbl vernames[] = {
  29. // For the first NUM_SPECIAL_STRINGS, the second column is the dialog ID.
  30. { TEXT("LegalCopyright"), IDD_VERSION_COPYRIGHT },
  31. { TEXT("FileDescription"), IDD_VERSION_DESCRIPTION },
  32. // For the rest, the second column is the string ID.
  33. { TEXT("FileVersion"), IDS_VN_FILEVERSION },
  34. { TEXT("Comments"), IDS_VN_COMMENTS },
  35. { TEXT("CompanyName"), IDS_VN_COMPANYNAME },
  36. { TEXT("InternalName"), IDS_VN_INTERNALNAME },
  37. { TEXT("LegalTrademarks"), IDS_VN_LEGALTRADEMARKS },
  38. { TEXT("OriginalFilename"), IDS_VN_ORIGINALFILENAME },
  39. { TEXT("PrivateBuild"), IDS_VN_PRIVATEBUILD },
  40. { TEXT("ProductName"), IDS_VN_PRODUCTNAME },
  41. { TEXT("ProductVersion"), IDS_VN_PRODUCTVERSION },
  42. { TEXT("SpecialBuild"), IDS_VN_SPECIALBUILD }
  43. };
  44. #define NUM_SPECIAL_STRINGS 2
  45. #define VERSTR_MANDATORY TEXT("FileVersion")
  46. typedef struct { // vp
  47. PROPSHEETPAGE psp;
  48. HWND hDlg;
  49. LPTSTR pVerBuffer; /* pointer to version data */
  50. TCHAR szVersionKey[60]; /* big enough for anything we need */
  51. struct _VERXLATE
  52. {
  53. WORD wLanguage;
  54. WORD wCodePage;
  55. } *lpXlate; /* ptr to translations data */
  56. int cXlate; /* count of translations */
  57. LPTSTR pszXlate;
  58. int cchXlateString;
  59. TCHAR szFile[MAX_PATH];
  60. } VERPROPSHEETPAGE, * LPVERPROPSHEETPAGE;
  61. #define VER_KEY_END 25 /* length of "\StringFileInfo\xxxxyyyy\" */
  62. /* (not localized) */
  63. #define MAXMESSAGELEN (50 + MAX_PATH * 2)
  64. /*
  65. Gets a particular datum about a file. The file's version info
  66. should have already been loaded by GetVersionInfo. If no datum
  67. by the specified name is available, NULL is returned. The name
  68. specified should be just the name of the item itself; it will
  69. be concatenated onto "\StringFileInfo\xxxxyyyy\" automatically.
  70. Version datum names are not localized, so it's OK to pass literals
  71. such as "FileVersion" to this function.
  72. Note that since the returned datum is in a global memory block,
  73. the return value of this function is LPSTR, not PSTR.
  74. */
  75. LPTSTR GetVersionDatum(LPVERPROPSHEETPAGE pvp, LPCTSTR pszName)
  76. {
  77. UINT cbValue = 0;
  78. LPTSTR lpValue;
  79. if (!pvp->pVerBuffer)
  80. return NULL;
  81. lstrcpy(pvp->szVersionKey + VER_KEY_END, pszName);
  82. VerQueryValue(pvp->pVerBuffer, pvp->szVersionKey, (void **)&lpValue, &cbValue);
  83. return (cbValue != 0) ? lpValue : NULL;
  84. }
  85. /*
  86. Frees global version data about a file. After this call, all
  87. GetVersionDatum calls will return NULL. To avoid memory leaks,
  88. always call this before the main properties dialog exits.
  89. */
  90. void FreeVersionInfo(LPVERPROPSHEETPAGE pvp)
  91. {
  92. if (pvp->pVerBuffer)
  93. {
  94. GlobalFree(pvp->pVerBuffer);
  95. pvp->pVerBuffer = NULL;
  96. }
  97. if (pvp->pszXlate)
  98. {
  99. LocalFree((HLOCAL)(HANDLE)pvp->pszXlate);
  100. pvp->pszXlate = NULL;
  101. }
  102. pvp->lpXlate = NULL;
  103. }
  104. /*
  105. Initialize version information for the properties dialog. The
  106. above global variables are initialized by this function, and
  107. remain valid (for the specified file only) until FreeVersionInfo
  108. is called.
  109. The first language we try will be the first item in the
  110. "\VarFileInfo\Translations" section; if there's nothing there,
  111. we try the one coded into the IDS_FILEVERSIONKEY resource string.
  112. If we can't even load that, we just use English (040904E4). We
  113. also try English with a null codepage (04090000) since many apps
  114. were stamped according to an old spec which specified this as
  115. the required language instead of 040904E4.
  116. GetVersionInfo returns TRUE if the version info was read OK,
  117. otherwise FALSE. If the return is FALSE, the buffer may still
  118. have been allocated; always call FreeVersionInfo to be safe.
  119. pszPath is modified by this call (pszName is appended).
  120. */
  121. BOOL GetVersionInfo(LPVERPROPSHEETPAGE pvp, LPCTSTR pszPath)
  122. {
  123. UINT cbValue = 0;
  124. LPTSTR pszValue = NULL;
  125. DWORD dwHandle; /* version subsystem handle */
  126. DWORD dwVersionSize; /* size of the version data */
  127. FreeVersionInfo(pvp); /* free old version buffer */
  128. // cast const -> non const for bad API def
  129. dwVersionSize = GetFileVersionInfoSize((LPTSTR)pszPath, &dwHandle);
  130. if (dwVersionSize == 0L)
  131. return FALSE; /* no version info */
  132. pvp->pVerBuffer = GlobalAlloc(GPTR, dwVersionSize);
  133. if (pvp->pVerBuffer == NULL)
  134. return FALSE;
  135. // cast const -> non const for bad API def
  136. if (!GetFileVersionInfo((LPTSTR)pszPath, dwHandle, dwVersionSize, pvp->pVerBuffer))
  137. {
  138. return FALSE;
  139. }
  140. // Look for translations
  141. if (VerQueryValue(pvp->pVerBuffer, TEXT("\\VarFileInfo\\Translation"), (void **)&pvp->lpXlate, &cbValue)
  142. && cbValue)
  143. {
  144. pvp->cXlate = cbValue / sizeof(DWORD);
  145. pvp->cchXlateString = pvp->cXlate * 64; /* figure 64 chars per lang name */
  146. pvp->pszXlate = (LPTSTR)(void*)LocalAlloc(LPTR, pvp->cchXlateString*sizeof(TCHAR));
  147. // failure of above will be handled later
  148. }
  149. else
  150. {
  151. pvp->lpXlate = NULL;
  152. }
  153. // Try same language as this program
  154. if (LoadString(HINST_THISDLL, IDS_VN_FILEVERSIONKEY, pvp->szVersionKey, ARRAYSIZE(pvp->szVersionKey)))
  155. {
  156. if (GetVersionDatum(pvp, VERSTR_MANDATORY))
  157. {
  158. return TRUE;
  159. }
  160. }
  161. // Try first language this supports
  162. if (pvp->lpXlate)
  163. {
  164. wsprintf(pvp->szVersionKey, TEXT("\\StringFileInfo\\%04X%04X\\"),
  165. pvp->lpXlate[0].wLanguage, pvp->lpXlate[0].wCodePage);
  166. if (GetVersionDatum(pvp, VERSTR_MANDATORY)) /* a required field */
  167. {
  168. return TRUE;
  169. }
  170. }
  171. #ifdef UNICODE
  172. // try English, unicode code page
  173. lstrcpy(pvp->szVersionKey, TEXT("\\StringFileInfo\\040904B0\\"));
  174. if (GetVersionDatum(pvp, VERSTR_MANDATORY))
  175. {
  176. return TRUE;
  177. }
  178. #endif
  179. // try English
  180. lstrcpy(pvp->szVersionKey, TEXT("\\StringFileInfo\\040904E4\\"));
  181. if (GetVersionDatum(pvp, VERSTR_MANDATORY))
  182. {
  183. return TRUE;
  184. }
  185. // try English, null codepage
  186. lstrcpy(pvp->szVersionKey, TEXT("\\StringFileInfo\\04090000\\"));
  187. if (GetVersionDatum(pvp, VERSTR_MANDATORY))
  188. {
  189. return TRUE;
  190. }
  191. // Could not find FileVersion info in a reasonable format
  192. return FALSE;
  193. }
  194. /*
  195. Fills the version key listbox with all available keys in the
  196. StringFileInfo block, and sets the version value text to the
  197. value of the first item.
  198. */
  199. void FillVersionList(LPVERPROPSHEETPAGE pvp)
  200. {
  201. LPTSTR pszName;
  202. LPTSTR pszValue;
  203. TCHAR szStringBase[VER_KEY_END+1];
  204. int i, j, idx;
  205. TCHAR szMessage[MAXMESSAGELEN+1];
  206. UINT uOffset, cbValue;
  207. HWND hwndLB = GetDlgItem(pvp->hDlg, IDD_VERSION_KEY);
  208. ListBox_ResetContent(hwndLB);
  209. for (i=0; i<NUM_SPECIAL_STRINGS; ++i)
  210. {
  211. SetDlgItemText(pvp->hDlg, vernames[i].idString, szNULL);
  212. }
  213. pvp->szVersionKey[VER_KEY_END] = 0; /* don't copy too much */
  214. lstrcpy(szStringBase, pvp->szVersionKey); /* copy to our buffer */
  215. szStringBase[VER_KEY_END - 1] = 0; /* strip the backslash */
  216. // Note: The Nt Version of version.dll has other exports. If/When they are
  217. // available in Win version then we can remove this section...
  218. // Get the binary file version from the VS_FIXEDFILEINFO
  219. {
  220. VS_FIXEDFILEINFO *pffi;
  221. if (VerQueryValue(pvp->pVerBuffer, TEXT("\\"), (void **)&pffi, &cbValue) && cbValue)
  222. {
  223. TCHAR szString[128];
  224. // display the binary version info, not the useless
  225. // string version (that can be out of sync)
  226. wnsprintf(szString, ARRAYSIZE(szString), TEXT("%d.%d.%d.%d"),
  227. HIWORD(pffi->dwFileVersionMS),
  228. LOWORD(pffi->dwFileVersionMS),
  229. HIWORD(pffi->dwFileVersionLS),
  230. LOWORD(pffi->dwFileVersionLS));
  231. SetDlgItemText(pvp->hDlg, IDD_VERSION_FILEVERSION, szString);
  232. }
  233. }
  234. //
  235. // Now iterate through all of the strings
  236. //
  237. for (j = 0; ; j++)
  238. {
  239. if (!VerQueryValueIndex(pvp->pVerBuffer, szStringBase, j, &pszName, &pszValue, &cbValue))
  240. break;
  241. for (i = 0; i < ARRAYSIZE(vernames); i++)
  242. {
  243. if (!lstrcmp(vernames[i].pszName, pszName))
  244. {
  245. break;
  246. }
  247. }
  248. if (i < NUM_SPECIAL_STRINGS)
  249. {
  250. SetDlgItemText(pvp->hDlg, vernames[i].idString, pszValue);
  251. }
  252. else
  253. {
  254. if (i == ARRAYSIZE(vernames) ||
  255. !LoadString(HINST_THISDLL, vernames[i].idString, szMessage, ARRAYSIZE(szMessage)))
  256. {
  257. lstrcpy(szMessage, pszName);
  258. }
  259. idx = ListBox_AddString(hwndLB, szMessage);
  260. if (idx != LB_ERR)
  261. {
  262. ListBox_SetItemData(hwndLB, idx, (DWORD_PTR)pszValue);
  263. }
  264. }
  265. }
  266. // Now look at the \VarFileInfo\Translations section and add an
  267. // item for the language(s) this file supports.
  268. if (pvp->lpXlate == NULL || pvp->pszXlate == NULL)
  269. return;
  270. if (!LoadString(HINST_THISDLL, (pvp->cXlate == 1) ? IDS_VN_LANGUAGE : IDS_VN_LANGUAGES,
  271. szMessage, ARRAYSIZE(szMessage)))
  272. return;
  273. idx = ListBox_AddString(hwndLB, szMessage);
  274. if (idx == LB_ERR)
  275. return;
  276. pvp->pszXlate[0] = 0;
  277. uOffset = 0;
  278. for (i = 0; i < pvp->cXlate; i++) {
  279. if (uOffset + 2 > (UINT)pvp->cchXlateString)
  280. break;
  281. if (i != 0) {
  282. lstrcat(pvp->pszXlate, TEXT(", "));
  283. uOffset += 2; // skip over ", "
  284. }
  285. if (VerLanguageName(pvp->lpXlate[i].wLanguage, pvp->pszXlate + uOffset, pvp->cchXlateString - uOffset) >
  286. (DWORD)(pvp->cchXlateString - uOffset))
  287. break;
  288. uOffset += lstrlen(pvp->pszXlate + uOffset);
  289. }
  290. pvp->pszXlate[pvp->cchXlateString - 1] = 0;
  291. ListBox_SetItemData(hwndLB, idx, (LPARAM)(LPTSTR)pvp->pszXlate);
  292. ListBox_SetCurSel(hwndLB, 0);
  293. FORWARD_WM_COMMAND(pvp->hDlg, IDD_VERSION_KEY, hwndLB, LBN_SELCHANGE, PostMessage);
  294. }
  295. //
  296. // Function: _UpdateVersionPrsht, private
  297. //
  298. // Descriptions:
  299. // This function fills fields of the "version" dialog box (a page of
  300. // a property sheet) with attributes of the associated file.
  301. //
  302. // Returns:
  303. // TRUE, if successfully done; FALSE, otherwise.
  304. //
  305. // History:
  306. // 01-06-93 Shrikant Created
  307. //
  308. BOOL _UpdateVersionPrsht(LPVERPROPSHEETPAGE pvp)
  309. {
  310. if (GetVersionInfo(pvp, pvp->szFile)) /* changes szPath */
  311. FillVersionList(pvp);
  312. return TRUE;
  313. }
  314. void _VersionPrshtCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
  315. {
  316. LPTSTR pszValue;
  317. int idx;
  318. switch (id)
  319. {
  320. case IDD_VERSION_KEY:
  321. if (codeNotify != LBN_SELCHANGE)
  322. {
  323. break;
  324. }
  325. idx = ListBox_GetCurSel(hwndCtl);
  326. pszValue = (LPTSTR)ListBox_GetItemData(hwndCtl, idx);
  327. if (pszValue)
  328. {
  329. SetDlgItemText(hwnd, IDD_VERSION_VALUE, pszValue);
  330. }
  331. break;
  332. }
  333. }
  334. // Array for context help:
  335. static const DWORD aVersionHelpIds[] = {
  336. IDD_VERSION_FILEVERSION, IDH_FPROP_VER_ABOUT,
  337. IDD_VERSION_DESCRIPTION, IDH_FPROP_VER_ABOUT,
  338. IDD_VERSION_COPYRIGHT, IDH_FPROP_VER_ABOUT,
  339. IDD_VERSION_FRAME, IDH_FPROP_VER_INFO,
  340. IDD_VERSION_KEY, IDH_FPROP_VER_INFO,
  341. IDD_VERSION_VALUE, IDH_FPROP_VER_INFO,
  342. 0, 0
  343. };
  344. BOOL_PTR CALLBACK _VersionPrshtDlgProc(HWND hDlg, UINT uMessage, WPARAM wParam, LPARAM lParam)
  345. {
  346. LPVERPROPSHEETPAGE pvp = (LPVERPROPSHEETPAGE)GetWindowLongPtr(hDlg, DWLP_USER);
  347. switch (uMessage)
  348. {
  349. case WM_INITDIALOG:
  350. SetWindowLongPtr(hDlg, DWLP_USER, lParam);
  351. pvp = (LPVERPROPSHEETPAGE)lParam;
  352. pvp->hDlg = hDlg;
  353. break;
  354. case WM_DESTROY:
  355. FreeVersionInfo(pvp); // free anything we created
  356. break;
  357. case WM_HELP:
  358. WinHelp((HWND) ((LPHELPINFO) lParam)->hItemHandle, NULL, HELP_WM_HELP,
  359. (ULONG_PTR) (LPTSTR) aVersionHelpIds);
  360. break;
  361. case WM_CONTEXTMENU: // right mouse click
  362. WinHelp((HWND) wParam, NULL, HELP_CONTEXTMENU,
  363. (ULONG_PTR) (LPTSTR) aVersionHelpIds);
  364. break;
  365. case WM_NOTIFY:
  366. switch (((NMHDR *)lParam)->code)
  367. {
  368. case PSN_SETACTIVE:
  369. _UpdateVersionPrsht(pvp);
  370. break;
  371. }
  372. break;
  373. case WM_COMMAND:
  374. HANDLE_WM_COMMAND(hDlg, wParam, lParam, _VersionPrshtCommand);
  375. break;
  376. default:
  377. return FALSE;
  378. }
  379. return TRUE;
  380. }
  381. //
  382. // creates a property sheet for the "version" page which shows version information.
  383. //
  384. STDAPI_(void) AddVersionPage(LPCTSTR pszFile, LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam)
  385. {
  386. DWORD dwAttr = GetFileAttributes(pszFile);
  387. if (0xFFFFFFFF != dwAttr && 0 == (dwAttr & FILE_ATTRIBUTE_OFFLINE) /*avoid HSM recall*/)
  388. {
  389. DWORD dwVerLen, dwVerHandle;
  390. VERPROPSHEETPAGE vp = {0};
  391. lstrcpyn(vp.szFile, pszFile, ARRAYSIZE(vp.szFile));
  392. dwVerLen = GetFileVersionInfoSize(vp.szFile, &dwVerHandle);
  393. if (dwVerLen)
  394. {
  395. HPROPSHEETPAGE hpage;
  396. vp.psp.dwSize = sizeof(VERPROPSHEETPAGE); // extra data
  397. vp.psp.dwFlags = PSP_DEFAULT;
  398. vp.psp.hInstance = HINST_THISDLL;
  399. vp.psp.pszTemplate = MAKEINTRESOURCE(DLG_VERSION);
  400. vp.psp.pfnDlgProc = _VersionPrshtDlgProc;
  401. hpage = CreatePropertySheetPage(&vp.psp);
  402. if (hpage)
  403. if (!pfnAddPage(hpage, lParam))
  404. DestroyPropertySheetPage(hpage);
  405. }
  406. }
  407. }