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.

302 lines
6.7 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name :
  4. sakit.cxx
  5. Abstract:
  6. Class that is used as a wrapper to install and uninstall the
  7. Server Administration Tool Kit.
  8. Author:
  9. Christopher Achille (cachille)
  10. Project:
  11. Internet Services Setup
  12. Revision History:
  13. August 2001: Created
  14. --*/
  15. #include "stdafx.h"
  16. #define _WIN32_DCOM
  17. #include <windows.h>
  18. #include <objbase.h>
  19. #include <ole2.h>
  20. #include "sakit.hxx"
  21. #include "tchar.h"
  22. // Define the GUIDs used by the Server Appliance Kit COM object
  23. #include <initguid.h>
  24. DEFINE_GUID(CLSID_SaInstall,0x142B8185,0x53AE,0x45B3,0x88,0x8F,0xC9,0x83,0x5B,0x15,0x6C,0xA9);
  25. // Constructor
  26. //
  27. SAKit::SAKit()
  28. : m_bCoInit(FALSE),
  29. m_pcSaKit(NULL)
  30. {
  31. }
  32. // Destructor
  33. //
  34. SAKit::~SAKit()
  35. {
  36. if ( m_pcSaKit )
  37. {
  38. // Release the SAKit Object if we still have it
  39. m_pcSaKit->Release();
  40. }
  41. // Uninitialize COM
  42. DoCoUnInit();
  43. }
  44. // function: DoCoInit
  45. //
  46. // Initialize COM for us
  47. //
  48. // Return Values:
  49. // TRUE - It is initialized
  50. // FALSE - It failed to initialize
  51. BOOL
  52. SAKit::DoCoInit()
  53. {
  54. HRESULT hRes;
  55. if (m_bCoInit)
  56. {
  57. // We have already done this
  58. return TRUE;
  59. }
  60. hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  61. // track our calls to coinit
  62. if ( hRes == S_OK )
  63. {
  64. m_bCoInit = TRUE;
  65. }
  66. if ( ( hRes == S_FALSE ) || ( hRes == RPC_E_CHANGED_MODE ) )
  67. {
  68. return TRUE;
  69. }
  70. return SUCCEEDED( hRes );
  71. }
  72. // function: QI
  73. //
  74. // Qi the object, and get it put into m_pcSaKit
  75. BOOL
  76. SAKit::QI()
  77. {
  78. HRESULT hRes;
  79. ISaInstall *ppv;
  80. if ( !DoCoInit() )
  81. {
  82. // If we can not CoInit we have no hope
  83. return FALSE;
  84. }
  85. hRes = CoCreateInstance( CLSID_SaInstall, // ClassID of SAInstall
  86. NULL, // No pUnkOuter
  87. CLSCTX_INPROC_SERVER, // No remote server
  88. __uuidof(ISaInstall), // uuid of SAInstall
  89. (LPVOID *) &ppv);
  90. if ( SUCCEEDED(hRes) )
  91. {
  92. m_pcSaKit = ppv;
  93. return TRUE;
  94. }
  95. iisDebugOut((LOG_TYPE_TRACE, _T("SAKit::Could not QI the Server Appliance Kit, Error=0x%x\n"),hRes));
  96. return FALSE;
  97. }
  98. // function: DoCoUnInit()
  99. //
  100. // Uninit Com
  101. //
  102. void
  103. SAKit::DoCoUnInit()
  104. {
  105. if (m_bCoInit)
  106. {
  107. CoUninitialize( );
  108. m_bCoInit = FALSE;
  109. }
  110. }
  111. // function: GetDiskName
  112. //
  113. // Get the name of the disk from the inf file. This is so that the
  114. // SAKit knows which Disk to ask for
  115. //
  116. // Parameters:
  117. // [out] bstrDiskName - The name of the returning disk.
  118. BOOL
  119. SAKit::GetDiskName(BSTR &bstrDiskName)
  120. {
  121. TSTR strDiskName;
  122. DWORD dwRequiredSize = 0;
  123. if ( ( g_pTheApp->m_hInfHandle != INVALID_HANDLE_VALUE ) &&
  124. SetupGetLineText( NULL,
  125. g_pTheApp->m_hInfHandle,
  126. SECTIONNAME_STRINGS,
  127. SECTION_STRINGS_CDNAME,
  128. NULL,
  129. 0,
  130. &dwRequiredSize) &&
  131. ( dwRequiredSize != 0 )
  132. )
  133. {
  134. if ( strDiskName.Resize( dwRequiredSize + 1 ) &&
  135. ( SetupGetLineText( NULL,
  136. g_pTheApp->m_hInfHandle,
  137. SECTIONNAME_STRINGS,
  138. SECTION_STRINGS_CDNAME,
  139. strDiskName.QueryStr(),
  140. strDiskName.QuerySize(),
  141. NULL) )
  142. )
  143. {
  144. bstrDiskName = SysAllocString( strDiskName.QueryStr() );
  145. if (bstrDiskName)
  146. {
  147. return TRUE;
  148. }
  149. }
  150. }
  151. iisDebugOut((LOG_TYPE_ERROR, _T("SAKit::GetDiskName: Can not retrieve the DiskName from the inf\n")));
  152. return FALSE;
  153. }
  154. // function: IsInstalled
  155. //
  156. // Determine if the component is already installed
  157. BOOL
  158. SAKit::IsInstalled(SA_TYPE sType)
  159. {
  160. VARIANT_BOOL IsInstalled = VARIANT_FALSE;
  161. HRESULT hRes;
  162. if ( !QI() )
  163. {
  164. // If we can not QI then fail
  165. return FALSE;
  166. }
  167. hRes = m_pcSaKit->SAAlreadyInstalled( sType, &IsInstalled );
  168. if ( SUCCEEDED(hRes) )
  169. {
  170. return IsInstalled == VARIANT_TRUE;
  171. }
  172. return FALSE;
  173. }
  174. // function: InstallKit
  175. //
  176. // Install the Kit
  177. //
  178. // Parameters:
  179. // sType = The type of Kit to Install
  180. BOOL
  181. SAKit::InstallKit(SA_TYPE sType)
  182. {
  183. VARIANT_BOOL bDisplayError = VARIANT_FALSE;
  184. VARIANT_BOOL bUnattend = g_pTheApp->m_fUnattended ? VARIANT_TRUE : VARIANT_FALSE;
  185. BSTR ErrorMessage = NULL;
  186. BSTR DiskName = NULL;
  187. HRESULT hRes;
  188. if ( g_pTheApp->m_fNTGuiMode )
  189. {
  190. // We are not resposible, and should not install the kit in
  191. // gui mode. They are responsible for that.
  192. return FALSE;
  193. }
  194. if ( !QI() ||
  195. !GetDiskName(DiskName) )
  196. {
  197. // If we can not QI then fail
  198. iisDebugOut((LOG_TYPE_ERROR, _T("SAKit::InstallKit: Failed to install the Server Appliance\n")));
  199. return FALSE;
  200. }
  201. hRes = m_pcSaKit->SAInstall(sType, // Install Type
  202. DiskName, // Disk Name
  203. bDisplayError, // Display Error?
  204. bUnattend, // Unattended?
  205. &ErrorMessage); // Error Message
  206. // Free the DiskName
  207. SysFreeString(DiskName);
  208. if ( FAILED(hRes) )
  209. {
  210. // Failed to install
  211. iisDebugOut((LOG_TYPE_ERROR, _T("SAKit::InstallKit: Failed to install the Server Appliance, Return Err=0x%x Error Message='%s'\n"), hRes, ErrorMessage));
  212. SysFreeString(ErrorMessage);
  213. return FALSE;
  214. }
  215. SysFreeString(ErrorMessage);
  216. return TRUE;
  217. }
  218. // function: UninstallKit
  219. //
  220. // UnInstall the Kit
  221. BOOL
  222. SAKit::UninstallKit(SA_TYPE sType)
  223. {
  224. BSTR ErrorMessage = NULL;
  225. HRESULT hRes;
  226. BOOL bRet = TRUE;
  227. if ( g_pTheApp->m_fNTGuiMode )
  228. {
  229. // We are not resposible, and should not uninstall the kit in
  230. // gui mode. They are responsible for that.
  231. return FALSE;
  232. }
  233. if ( !QI() )
  234. {
  235. // If we can not QI then fail
  236. iisDebugOut((LOG_TYPE_ERROR, _T("SAKit::InstallKit: Failed to uninstall the Server Appliance\n")));
  237. return FALSE;
  238. }
  239. hRes = m_pcSaKit->SAUninstall(sType, // Install Type
  240. &ErrorMessage); // Error Message
  241. if ( FAILED(hRes) )
  242. {
  243. // Failed to install
  244. iisDebugOut((LOG_TYPE_ERROR, _T("SAKit::UninstallKit: Failed to uninstall the Server Appliance, Return Err=0x%x Error Message='%s'\n"), hRes, ErrorMessage));
  245. bRet = FALSE;
  246. }
  247. SysFreeString(ErrorMessage);
  248. return bRet;
  249. }