Leaked source code of windows server 2003
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.

337 lines
8.6 KiB

  1. // ---------------------------------------------------------------------------
  2. // UTIL.CPP
  3. // ---------------------------------------------------------------------------
  4. // Copyright (c) 1999 Microsoft Corporation
  5. //
  6. // Helper functions
  7. //
  8. // ---------------------------------------------------------------------------
  9. #include "util.h"
  10. #include "strings.h"
  11. /****************************************************************************
  12. NAME: GoodEnough
  13. SYNOPSIS: Returns true if pwVerGot is newer or equal to pwVerNeed
  14. ****************************************************************************/
  15. BOOL GoodEnough(WORD *pwVerGot, WORD *pwVerNeed)
  16. {
  17. BOOL fOK = FALSE;
  18. Assert(pwVerGot);
  19. Assert(pwVerNeed);
  20. if (pwVerGot[0] > pwVerNeed[0])
  21. fOK = TRUE;
  22. else if (pwVerGot[0] == pwVerNeed[0])
  23. {
  24. if (pwVerGot[1] > pwVerNeed[1])
  25. fOK = TRUE;
  26. else if (pwVerGot[1] == pwVerNeed[1])
  27. {
  28. if (pwVerGot[2] > pwVerNeed[2])
  29. fOK = TRUE;
  30. else if (pwVerGot[2] == pwVerNeed[2])
  31. {
  32. if (pwVerGot[3] >= pwVerNeed[3])
  33. fOK = TRUE;
  34. }
  35. }
  36. }
  37. return fOK;
  38. }
  39. /*******************************************************************
  40. NAME: ConvertVerToEnum
  41. ********************************************************************/
  42. SETUPVER ConvertVerToEnum(WORD *pwVer)
  43. {
  44. SETUPVER sv;
  45. Assert(pwVer);
  46. switch (pwVer[0])
  47. {
  48. case 0:
  49. sv = VER_NONE;
  50. break;
  51. case 1:
  52. if (0 == pwVer[1])
  53. sv = VER_1_0;
  54. else
  55. sv = VER_1_1;
  56. break;
  57. case 4:
  58. sv = VER_4_0;
  59. break;
  60. case 5:
  61. sv = VER_5_0;
  62. break;
  63. default:
  64. sv = VER_MAX;
  65. }
  66. return sv;
  67. }
  68. /*******************************************************************
  69. NAME: ConvertStrToVer
  70. ********************************************************************/
  71. void ConvertStrToVer(LPCSTR pszStr, WORD *pwVer)
  72. {
  73. int i;
  74. Assert(pszStr);
  75. Assert(pwVer);
  76. ZeroMemory(pwVer, 4 * sizeof(WORD));
  77. for (i=0; i<4; i++)
  78. {
  79. while (*pszStr && (*pszStr != ',') && (*pszStr != '.'))
  80. {
  81. pwVer[i] *= 10;
  82. pwVer[i] += *pszStr - '0';
  83. pszStr++;
  84. }
  85. if (*pszStr)
  86. pszStr++;
  87. }
  88. return;
  89. }
  90. /*******************************************************************
  91. NAME: GetVers
  92. ********************************************************************/
  93. void GetVers(WORD *pwVerCurr, WORD *pwVerPrev)
  94. {
  95. HKEY hkeyT;
  96. DWORD cb;
  97. CHAR szVer[VERLEN];
  98. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, si.pszVerInfo, 0, KEY_QUERY_VALUE, &hkeyT))
  99. {
  100. if (pwVerCurr)
  101. {
  102. cb = sizeof(szVer);
  103. RegQueryValueExA(hkeyT, c_szRegCurrVer, NULL, NULL, (LPBYTE)szVer, &cb);
  104. ConvertStrToVer(szVer, pwVerCurr);
  105. }
  106. if (pwVerPrev)
  107. {
  108. cb = sizeof(szVer);
  109. RegQueryValueExA(hkeyT, c_szRegPrevVer, NULL, NULL, (LPBYTE)szVer, &cb);
  110. ConvertStrToVer(szVer, pwVerPrev);
  111. }
  112. RegCloseKey(hkeyT);
  113. }
  114. }
  115. /*******************************************************************
  116. NAME: GetVerInfo
  117. ********************************************************************/
  118. void GetVerInfo(SETUPVER *psvCurr, SETUPVER *psvPrev)
  119. {
  120. WORD wVerCurr[4];
  121. WORD wVerPrev[4];
  122. GetVers(wVerCurr, wVerPrev);
  123. if (psvCurr)
  124. *psvCurr = ConvertVerToEnum(wVerCurr);
  125. if (psvPrev)
  126. *psvPrev = ConvertVerToEnum(wVerPrev);
  127. }
  128. /*******************************************************************
  129. NAME: InterimBuild
  130. ********************************************************************/
  131. BOOL InterimBuild(SETUPVER *psv)
  132. {
  133. HKEY hkeyT;
  134. DWORD cb;
  135. BOOL fInterim = FALSE;
  136. Assert(psv);
  137. ZeroMemory(psv, sizeof(SETUPVER));
  138. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, si.pszVerInfo, 0, KEY_QUERY_VALUE, &hkeyT))
  139. {
  140. cb = sizeof(SETUPVER);
  141. fInterim = (ERROR_SUCCESS == RegQueryValueExA(hkeyT, c_szRegInterimVer, NULL, NULL, (LPBYTE)psv, &cb));
  142. RegCloseKey(hkeyT);
  143. }
  144. return fInterim;
  145. }
  146. /*******************************************************************
  147. NAME: GetASetupVer
  148. ********************************************************************/
  149. BOOL GetASetupVer(LPCTSTR pszGUID, WORD *pwVer, LPTSTR pszVer, int cch)
  150. {
  151. HKEY hkey;
  152. TCHAR szPath[MAX_PATH], szVer[64];
  153. BOOL fInstalled = FALSE;
  154. DWORD dwValue, cb;
  155. Assert(pszGUID);
  156. if (pszVer)
  157. pszVer[0] = 0;
  158. if (pwVer)
  159. ZeroMemory(pwVer, 4 * sizeof(WORD));
  160. wsprintf(szPath, c_szPathFileFmt, c_szRegASetup, pszGUID);
  161. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szPath, 0, KEY_QUERY_VALUE, &hkey))
  162. {
  163. cb = sizeof(dwValue);
  164. if (ERROR_SUCCESS == RegQueryValueEx(hkey, c_szIsInstalled, 0, NULL, (LPBYTE)&dwValue, &cb))
  165. {
  166. if (1 == dwValue)
  167. {
  168. cb = sizeof(szVer);
  169. if (ERROR_SUCCESS == RegQueryValueEx(hkey, c_szValueVersion, 0, NULL, (LPBYTE)szVer, &cb))
  170. {
  171. if (pwVer)
  172. ConvertStrToVer(szVer, pwVer);
  173. if (pszVer)
  174. lstrcpyn(pszVer, szVer, cch);
  175. fInstalled = TRUE;
  176. }
  177. }
  178. }
  179. RegCloseKey(hkey);
  180. }
  181. return fInstalled;
  182. }
  183. /*******************************************************************
  184. NAME: GetFileVer
  185. ********************************************************************/
  186. HRESULT GetFileVer(LPCTSTR pszExePath, LPTSTR pszVer, DWORD cch)
  187. {
  188. DWORD dwVerInfoSize, dwVerHnd;
  189. HRESULT hr = S_OK;
  190. LPSTR pszInfo = NULL;
  191. LPSTR pszVersion;
  192. LPWORD pwTrans;
  193. TCHAR szGet[MAX_PATH];
  194. UINT uLen;
  195. // Validate Parameters
  196. Assert(pszExePath);
  197. Assert(pszVer);
  198. Assert(cch);
  199. // Validate global state
  200. Assert(g_pMalloc);
  201. // Initialize out parameters
  202. pszVer[0] = TEXT('\0');
  203. // Allocate space for version info block
  204. if (0 == (dwVerInfoSize = GetFileVersionInfoSize(const_cast<LPTSTR> (pszExePath), &dwVerHnd)))
  205. {
  206. hr = E_FAIL;
  207. TraceResult(hr);
  208. goto exit;
  209. }
  210. IF_NULLEXIT(pszInfo = (LPTSTR)g_pMalloc->Alloc(dwVerInfoSize));
  211. ZeroMemory(pszInfo, dwVerInfoSize);
  212. // Get Version info block
  213. IF_FALSEEXIT(GetFileVersionInfo(const_cast<LPTSTR> (pszExePath), dwVerHnd, dwVerInfoSize, pszInfo), E_FAIL);
  214. // Figure out language for version info
  215. IF_FALSEEXIT(VerQueryValue(pszInfo, "\\VarFileInfo\\Translation", (LPVOID *)&pwTrans, &uLen) && uLen >= (2 * sizeof(WORD)), E_FAIL);
  216. // Set up buffer with correct language and get version
  217. wsprintf(szGet, "\\StringFileInfo\\%04X%04X\\FileVersion", pwTrans[0], pwTrans[1]);
  218. IF_FALSEEXIT(VerQueryValue(pszInfo, szGet, (LPVOID *)&pszVersion, &uLen) && uLen, E_FAIL);
  219. // Copy version out of version block, into out param
  220. Assert(pszVersion);
  221. lstrcpyn(pszVer, pszVersion, cch);
  222. exit:
  223. if (pszInfo)
  224. g_pMalloc->Free(pszInfo);
  225. return hr;
  226. }
  227. /*******************************************************************
  228. NAME: GetExeVer
  229. ********************************************************************/
  230. HRESULT GetExeVer(LPCTSTR pszExeName, WORD *pwVer, LPTSTR pszVer, int cch)
  231. {
  232. HRESULT hr = S_OK;
  233. TCHAR szPath[MAX_PATH];
  234. TCHAR szVer[64];
  235. // Validate params
  236. Assert(pszExeName);
  237. // Initialize out params
  238. if (pszVer)
  239. {
  240. Assert(cch);
  241. pszVer[0] = 0;
  242. }
  243. if (pwVer)
  244. // Version is an array of 4 words
  245. ZeroMemory(pwVer, 4 * sizeof(WORD));
  246. // Find the exe
  247. IF_FALSEEXIT(GetExePath(pszExeName, szPath, ARRAYSIZE(szPath), FALSE), E_FAIL);
  248. // Get the string representation of the version
  249. IF_FAILEXIT(hr = GetFileVer(szPath, szVer, ARRAYSIZE(szVer)));
  250. // Fill in out params
  251. if (pwVer)
  252. ConvertStrToVer(szVer, pwVer);
  253. if (pszVer)
  254. lstrcpyn(pszVer, szVer, cch);
  255. exit:
  256. return hr;
  257. }