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.

234 lines
6.9 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1994 - 1998.
  5. //
  6. // File: script.cpp
  7. //
  8. // Contents: Functions for working with Darwin files, both packages,
  9. // transforms and scripts.
  10. //
  11. // Classes:
  12. //
  13. // Functions: BuildScriptAndGetActInfo
  14. //
  15. // History: 1-14-1998 stevebl Created
  16. //
  17. //---------------------------------------------------------------------------
  18. #include "precomp.hxx"
  19. //+--------------------------------------------------------------------------
  20. //
  21. // Function: RegDeleteTree
  22. //
  23. // Synopsis: deletes a registry key and all of its children
  24. //
  25. // Arguments: [hKey] - handle to the key's parent
  26. // [szSubKey] - name of the key to be deleted
  27. //
  28. // Returns: ERROR_SUCCESS
  29. //
  30. // History: 1-14-1998 stevebl Moved from old project
  31. //
  32. //---------------------------------------------------------------------------
  33. LONG RegDeleteTree(HKEY hKey, TCHAR * szSubKey)
  34. {
  35. HKEY hKeyNew;
  36. LONG lResult = RegOpenKey(hKey, szSubKey, &hKeyNew);
  37. if (lResult != ERROR_SUCCESS)
  38. {
  39. return lResult;
  40. }
  41. TCHAR szName[256];
  42. szName[0] = 0;
  43. while (ERROR_SUCCESS == RegEnumKey(hKeyNew, 0, szName, 256))
  44. {
  45. RegDeleteTree(hKeyNew, szName);
  46. }
  47. RegCloseKey(hKeyNew);
  48. return RegDeleteKey(hKey, szSubKey);
  49. }
  50. extern "C"
  51. HRESULT
  52. GenerateScript( PACKAGEDETAIL* pd, WCHAR* wszScriptPath )
  53. {
  54. return BuildScriptAndGetActInfo( *pd, FALSE, FALSE, wszScriptPath );
  55. }
  56. //+--------------------------------------------------------------------------
  57. //
  58. // Function: BuildScriptAndGetActInfo
  59. //
  60. // Synopsis: Builds the script file and fills in the ACTINFO structure
  61. // member in the PACKAGEDETAIL structure.
  62. //
  63. // Arguments: [szScriptRoot] - [in] the subdirectory that the script file
  64. // should be place in.
  65. // [pd] - [in/out] package detail structure - see
  66. // notes for complete list of fields that
  67. // should be filled in and the list of fields
  68. // that are set on return
  69. //
  70. // Returns: S_OK - success
  71. // <other> - error
  72. //
  73. // Modifies: all fields under pd.pActInfo (only on success)
  74. // also modifies pd.pInstallInfo->cScriptLen
  75. //
  76. // History: 1-14-1998 stevebl Created
  77. //
  78. // Notes: On input:
  79. // pd.cSources must be >= 1.
  80. // pd.pszSourceList[] contains the MSI package and the list of
  81. // (if any) transforms to be applied.
  82. // pd.pPlatformInfo should be completely filled in (only one
  83. // locale).
  84. // pd.pInstallInfo->pszScriptFile contains the name of the
  85. // script file to be generated.
  86. //
  87. // On output:
  88. // The script file will be generated under the appropriate name
  89. // and in the appropriate directory.
  90. // pd.pActInfo will be completely filled in.
  91. //
  92. //---------------------------------------------------------------------------
  93. HRESULT BuildScriptAndGetActInfo(PACKAGEDETAIL & pd, BOOL bFileExtensionsOnly, BOOL bGenerateClasses /* =TRUE */, WCHAR* wszScriptPath /* = NULL */ )
  94. {
  95. DebugMsg((DM_VERBOSE, TEXT("BuldScriptAndGetActInfo called with bFileExtensionsOnly == %u"), bFileExtensionsOnly));
  96. CHourglass hourglass;
  97. HRESULT hr;
  98. UINT uMsiStatus;
  99. LONG error;
  100. int i;
  101. WCHAR* szScriptPath = pd.pInstallInfo->pszScriptPath;
  102. if ( wszScriptPath )
  103. {
  104. szScriptPath = wszScriptPath;
  105. }
  106. CString szTransformList = L"";
  107. CClassCollection Classes( &pd );
  108. if (pd.cSources > 1)
  109. {
  110. CString szSource = pd.pszSourceList[0];
  111. int nChars = 1 + szSource.ReverseFind(L'\\');
  112. BOOL fTransformsAtSource = TRUE;
  113. for (i = 1; i < pd.cSources && TRUE == fTransformsAtSource; i++)
  114. {
  115. if (0 == wcsncmp(szSource, pd.pszSourceList[i], nChars))
  116. {
  117. // make sure there isn't a sub-path
  118. int n = nChars;
  119. while (0 != pd.pszSourceList[i][n] && TRUE == fTransformsAtSource)
  120. {
  121. if (pd.pszSourceList[i][n] == L'\\')
  122. {
  123. fTransformsAtSource = FALSE;
  124. }
  125. n++;
  126. }
  127. }
  128. else
  129. {
  130. fTransformsAtSource = FALSE;
  131. }
  132. }
  133. if (fTransformsAtSource)
  134. {
  135. szTransformList = L"@";
  136. }
  137. else
  138. {
  139. szTransformList = L"|";
  140. nChars = 0;
  141. }
  142. for (i = 1; i < pd.cSources; i++)
  143. {
  144. if (i > 1)
  145. {
  146. szTransformList += L";";
  147. }
  148. szTransformList += &pd.pszSourceList[i][nChars];
  149. }
  150. }
  151. // disable MSI ui
  152. MsiSetInternalUI(INSTALLUILEVEL_NONE, NULL);
  153. // build the script file
  154. TCHAR szTempPath[MAX_PATH];
  155. TCHAR szTempFileName[MAX_PATH];
  156. if (0 != GetTempPath(sizeof(szTempPath) / sizeof(szTempPath[0]), szTempPath))
  157. {
  158. if (0 == GetTempFileName(szTempPath, TEXT("ADE"), 0, szTempFileName))
  159. {
  160. goto Failure;
  161. }
  162. DWORD dwPlatform;
  163. if ( CAppData::Is64Bit( &pd ) )
  164. {
  165. dwPlatform = CAppData::Get64BitMsiArchFlags( &pd );
  166. }
  167. else
  168. {
  169. dwPlatform = MSIARCHITECTUREFLAGS_X86;
  170. }
  171. uMsiStatus = MsiAdvertiseProductEx(
  172. pd.pszSourceList[0],
  173. szTempFileName,
  174. szTransformList,
  175. LANGIDFROMLCID(pd.pPlatformInfo->prgLocale[0]),
  176. dwPlatform,
  177. 0);
  178. if (uMsiStatus)
  179. {
  180. DeleteFile(szTempFileName);
  181. DebugMsg((DM_WARNING, TEXT("MsiAdvertiseProduct failed with %u"), uMsiStatus));
  182. LogADEEvent(EVENTLOG_ERROR_TYPE, EVENT_ADE_GENERATESCRIPT_ERROR, HRESULT_FROM_WIN32(uMsiStatus), pd.pszSourceList[0]);
  183. // an error occurred
  184. return HRESULT_FROM_WIN32((long)uMsiStatus);
  185. }
  186. // fill in the ActInfo
  187. if ( bGenerateClasses )
  188. {
  189. hr = Classes.GetClasses( bFileExtensionsOnly );
  190. }
  191. else
  192. {
  193. hr = S_OK;
  194. }
  195. if ( SUCCEEDED( hr ) )
  196. {
  197. if (!CopyFile(szTempFileName, szScriptPath, FALSE))
  198. {
  199. hr = HRESULT_FROM_WIN32(GetLastError());
  200. DeleteFile(szTempFileName);
  201. return hr;
  202. }
  203. DeleteFile(szTempFileName);
  204. }
  205. }
  206. else
  207. {
  208. Failure:
  209. hr = HRESULT_FROM_WIN32(GetLastError());
  210. }
  211. return hr;
  212. }