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.

202 lines
6.1 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: launchinf.cpp
  4. //
  5. // Module: CMSETUP.LIB
  6. //
  7. // Synopsis: Implementation of the LaunchInfSection function.
  8. //
  9. // Copyright (c) 1998-1999 Microsoft Corporation
  10. //
  11. // Author: quintinb Created Header 09/19/99
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "cmsetup.h"
  15. #include <advpub.h> // advpack.dll (IExpress) includes
  16. //+----------------------------------------------------------------------------
  17. //
  18. // Function: LaunchInfSection
  19. //
  20. // Synopsis: Launches an specified inf section in a specified inf file using
  21. // advpack.dll's RunSetupCommand Function.
  22. //
  23. // Arguments: HINSTANCE hInstance - Instance Handle for string resources
  24. // LPCTSTR szInfFile - Inf file
  25. // LPCTSTR szInfSection - section to launch
  26. //
  27. // Returns: HRESULT -- return code from advpack.dll's RunSetupCommand Function
  28. //
  29. // History: Anas Jarrah A-anasj Created 2/10/98
  30. // quintinb added hInstance to signature and modified to use
  31. // CDynamicLibrary class 7-14-98
  32. // quintinb added bQuiet Flag 7-27-98
  33. // quintinb changed to use static linking to advpack.lib 11-1-98
  34. //
  35. //+----------------------------------------------------------------------------
  36. HRESULT LaunchInfSection(LPCTSTR szInfFile, LPCTSTR szInfSection, LPCTSTR szTitle, BOOL bQuiet)
  37. {
  38. //
  39. // These flags control how the Inf Files are launched.
  40. //
  41. DWORD dwFlags;
  42. if (bQuiet)
  43. {
  44. dwFlags = RSC_FLAG_INF | RSC_FLAG_QUIET;
  45. }
  46. else
  47. {
  48. dwFlags = RSC_FLAG_INF;
  49. }
  50. //
  51. // holds return value of the calls to RunSetupCommand
  52. //
  53. HRESULT hrReturn;
  54. //
  55. // Set the current directory to the dir where the inf is located.
  56. //
  57. CHAR szCurDir[MAX_PATH+1];
  58. CFileNameParts InfFile(szInfFile);
  59. #ifdef UNICODE
  60. MYVERIFY(CELEMS(szCurDir) > (UINT)wsprintfA(szCurDir, "%S%S", InfFile.m_Drive, InfFile.m_Dir));
  61. #else
  62. MYVERIFY(CELEMS(szCurDir) > (UINT)wsprintfA(szCurDir, "%s%s", InfFile.m_Drive, InfFile.m_Dir));
  63. #endif
  64. HANDLE hWait = NULL; // passed to the RunSetupCommand function. Can be used to hold a process handle
  65. //
  66. // Create the Char pointers to pass to RunSetupCommand
  67. //
  68. CHAR* pszInfFile;
  69. CHAR* pszInfSection;
  70. CHAR* pszTitle;
  71. //
  72. // There is no UNICODE version of RunSetupCommand. Thus we must convert strings and
  73. // run it with the CHAR versions.
  74. //
  75. #ifdef UNICODE
  76. pszInfFile = (CHAR*)CmMalloc(sizeof(CHAR)*(MAX_PATH+1));
  77. pszInfSection = (CHAR*)CmMalloc(sizeof(CHAR)*(MAX_PATH+1));
  78. pszTitle = (CHAR*)CmMalloc(sizeof(CHAR)*(MAX_PATH+1));
  79. if (pszInfFile && pszInfSection && pszTitle)
  80. {
  81. MYVERIFY (0 != WideCharToMultiByte(CP_ACP, 0, szInfFile, -1,
  82. pszInfFile, MAX_PATH, NULL, NULL));
  83. MYVERIFY (0 != WideCharToMultiByte(CP_ACP, 0, szInfSection, -1,
  84. pszInfSection, MAX_PATH, NULL, NULL));
  85. MYVERIFY (0 != WideCharToMultiByte(CP_ACP, 0, szTitle, -1,
  86. pszTitle, MAX_PATH, NULL, NULL));
  87. }
  88. else
  89. {
  90. CmFree(pszInfFile);
  91. CmFree(pszInfSection);
  92. CmFree(pszTitle);
  93. return E_OUTOFMEMORY;
  94. }
  95. #else
  96. pszInfFile = (char*)szInfFile;
  97. pszInfSection = (char*)szInfSection;
  98. pszTitle = (char*)szTitle;
  99. #endif
  100. hrReturn = RunSetupCommand(NULL, pszInfFile,
  101. pszInfSection, szCurDir, pszTitle, &hWait, dwFlags, NULL);
  102. CloseHandle(hWait);
  103. #ifdef UNICODE
  104. //
  105. // Free the Allocated Buffers
  106. //
  107. CmFree(pszInfFile);
  108. CmFree(pszInfSection);
  109. CmFree(pszTitle);
  110. #endif
  111. return hrReturn;
  112. }
  113. //+----------------------------------------------------------------------------
  114. //
  115. // Function: CallLaunchInfSectionEx
  116. //
  117. // Synopsis: Launches an specified inf section in a specified inf file using
  118. // advpack.dll's RunSetupCommand Function.
  119. //
  120. // Arguments: LPCSTR pszInfFile - full path to the Inf file
  121. // LPCSTR pszInfSection - section to launch from the inf file
  122. // DWORD dwFlags - flags to give LaunchINFSectionEx, see advpub.h for more details
  123. //
  124. // Returns: HRESULT -- standard COM error codes. If ERROR_SUCCESS_REBOOT_REQUIRED,
  125. // is returned, the caller should ask the user to reboot.
  126. //
  127. //
  128. // History: quintinb created 02/09/2001
  129. //
  130. //+----------------------------------------------------------------------------
  131. HRESULT CallLaunchInfSectionEx(LPCSTR pszInfFile, LPCSTR pszInfSection, DWORD dwFlags)
  132. {
  133. //
  134. // Check the inputs
  135. //
  136. if ((NULL == pszInfFile) || (NULL == pszInfSection) || (TEXT('\0') == pszInfFile[0]) || (TEXT('\0') == pszInfSection[0]))
  137. {
  138. return E_INVALIDARG;
  139. }
  140. //
  141. // Now calculate how large of a buffer we will need to send to LaunchINFSectionEx with the params and allocate it.
  142. //
  143. DWORD dwSize = (lstrlenA(pszInfFile) + lstrlenA(pszInfSection) + 10 + 2 + 1)*sizeof(CHAR); // 10 chars is max size of a DWORD + 2 commas + a NULL
  144. LPSTR pszParams = (LPSTR)CmMalloc (dwSize);
  145. if (NULL == pszParams)
  146. {
  147. return E_OUTOFMEMORY;
  148. }
  149. //
  150. // Fill in the allocated buffer
  151. //
  152. wsprintfA(pszParams, "%s,%s,,%d", pszInfFile, pszInfSection, dwFlags);
  153. //
  154. // Call LaunchINFSectionEx
  155. //
  156. HRESULT hr = LaunchINFSectionEx(NULL, NULL, pszParams, 0);
  157. if (FAILED(hr))
  158. {
  159. CMTRACE3A("CallLaunchInfSectionEx -- LaunchINFSectionEx on file ""%s"" and section ""%s"" FAILED! hr=0x%x", pszInfFile, pszInfSection, hr);
  160. }
  161. else
  162. {
  163. if ((HRESULT)ERROR_SUCCESS_REBOOT_REQUIRED == hr)
  164. {
  165. CMTRACE2A("CallLaunchInfSectionEx -- LaunchINFSectionEx on file ""%s"" and section ""%s"" returned reboot required.", pszInfFile, pszInfSection);
  166. }
  167. }
  168. CmFree(pszParams);
  169. return hr;
  170. }