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.

663 lines
16 KiB

  1. //////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // ApplicationManager.cpp
  4. //
  5. // Copyright (C) 1998, 1999 Microsoft Corporation. All rights reserved.
  6. //
  7. // Abstract :
  8. //
  9. // This is the implementation of IApplicationManager
  10. //
  11. // History :
  12. //
  13. // 05/06/1999 luish Created
  14. //
  15. //////////////////////////////////////////////////////////////////////////////////////////////
  16. #include "Resource.h"
  17. #include "AppMan.h"
  18. #include "ExceptionHandler.h"
  19. #include "ApplicationManager.h"
  20. #include "AppManDebug.h"
  21. #include "Global.h"
  22. #include "RegistryKey.h"
  23. #include "Win32API.h"
  24. //To flag as DBG_APPMAN
  25. #ifdef DBG_MODULE
  26. #undef DBG_MODULE
  27. #endif
  28. #define DBG_MODULE DBG_APPMAN
  29. //////////////////////////////////////////////////////////////////////////////////////////////
  30. //
  31. //////////////////////////////////////////////////////////////////////////////////////////////
  32. CApplicationManager::CApplicationManager(void)
  33. {
  34. FUNCTION("CApplicationManager::CApplicationManager (void)");
  35. assert(TRUE);
  36. }
  37. //////////////////////////////////////////////////////////////////////////////////////////////
  38. //
  39. //////////////////////////////////////////////////////////////////////////////////////////////
  40. CApplicationManager::CApplicationManager(CApplicationManagerRoot * pParent)
  41. {
  42. FUNCTION("CApplicationManager::CApplicationManager (CApplicationManagerRoot * pParent)");
  43. HRESULT hResult = S_OK;
  44. assert(NULL != pParent);
  45. m_pParentObject = pParent;
  46. if ((hResult = m_InformationManager.Initialize()) == E_ACCESSDENIED)
  47. {
  48. pParent->m_bInsufficientAccessToRun = TRUE;
  49. }
  50. }
  51. //////////////////////////////////////////////////////////////////////////////////////////////
  52. //
  53. //////////////////////////////////////////////////////////////////////////////////////////////
  54. CApplicationManager::~CApplicationManager(void)
  55. {
  56. FUNCTION("CApplicationManager::~CApplicationManager (void)");
  57. }
  58. //////////////////////////////////////////////////////////////////////////////////////////////
  59. //
  60. // IUnknown interface implementation
  61. //
  62. //////////////////////////////////////////////////////////////////////////////////////////////
  63. STDMETHODIMP CApplicationManager::QueryInterface(REFIID RefIID, LPVOID * ppVoidObject)
  64. {
  65. FUNCTION("CApplicationManager::QueryInterface ()");
  66. HRESULT hResult = S_OK;
  67. ///////////////////////////////////////////////////////////////////////////////////////
  68. try
  69. {
  70. if (NULL == &RefIID)
  71. {
  72. THROW(E_UNEXPECTED);
  73. }
  74. *ppVoidObject = NULL;
  75. if ((RefIID == IID_IUnknown)||(RefIID == IID_ApplicationManager))
  76. {
  77. *ppVoidObject = (LPVOID) this;
  78. }
  79. if (*ppVoidObject)
  80. {
  81. ((LPUNKNOWN)*ppVoidObject)->AddRef();
  82. }
  83. else
  84. {
  85. hResult = E_NOINTERFACE;
  86. }
  87. }
  88. ///////////////////////////////////////////////////////////////////////////////////////
  89. catch(CAppManExceptionHandler * pException)
  90. {
  91. hResult = pException->GetResultCode();
  92. delete pException;
  93. }
  94. catch(...)
  95. {
  96. if ((NULL == &RefIID)||(NULL == ppVoidObject)||(IsBadWritePtr(ppVoidObject, sizeof(LPVOID))))
  97. {
  98. hResult = E_INVALIDARG;
  99. }
  100. else
  101. {
  102. hResult = E_UNEXPECTED;
  103. }
  104. }
  105. return hResult;
  106. }
  107. //////////////////////////////////////////////////////////////////////////////////////////////
  108. //
  109. // IUnknown interface implementation
  110. //
  111. //////////////////////////////////////////////////////////////////////////////////////////////
  112. STDMETHODIMP_(ULONG) CApplicationManager::AddRef(void)
  113. {
  114. FUNCTION("CApplicationManager::AddRef ()");
  115. if (NULL != m_pParentObject)
  116. {
  117. return m_pParentObject->AddRef();
  118. }
  119. return 0;
  120. }
  121. //////////////////////////////////////////////////////////////////////////////////////////////
  122. //
  123. // IUnknown interface implementation
  124. //
  125. //////////////////////////////////////////////////////////////////////////////////////////////
  126. STDMETHODIMP_(ULONG) CApplicationManager::Release(void)
  127. {
  128. FUNCTION("CApplicationManager::Release ()");
  129. if (NULL != m_pParentObject)
  130. {
  131. return m_pParentObject->Release();
  132. }
  133. return 0;
  134. }
  135. //////////////////////////////////////////////////////////////////////////////////////////////
  136. //
  137. //////////////////////////////////////////////////////////////////////////////////////////////
  138. STDMETHODIMP CApplicationManager::GetAdvancedMode(LPDWORD lpdwAdvancedModeMask)
  139. {
  140. FUNCTION("CApplicationManager::GetAdvancedMode ()");
  141. HRESULT hResult = APPMAN_E_INVALIDDATA;
  142. try
  143. {
  144. //
  145. // Check to make sure the pointer we receive is good
  146. //
  147. if ((NULL == lpdwAdvancedModeMask)||(IsBadWritePtr(lpdwAdvancedModeMask, sizeof(DWORD))))
  148. {
  149. THROW(APPMAN_E_INVALIDDATA);
  150. }
  151. //
  152. // Get the advanced mode
  153. //
  154. hResult = m_InformationManager.GetAdvancedMode(lpdwAdvancedModeMask);
  155. }
  156. ///////////////////////////////////////////////////////////////////////////////////////
  157. catch(CAppManExceptionHandler * pException)
  158. {
  159. //
  160. // Get Result code
  161. //
  162. hResult = pException->GetResultCode();
  163. //
  164. // Delete exception handler
  165. //
  166. delete pException;
  167. }
  168. catch(...)
  169. {
  170. //
  171. // If we failed to handle an exception, we default to APPMAN_E_CRITICALERROR
  172. //
  173. hResult = E_UNEXPECTED;
  174. }
  175. return hResult;
  176. }
  177. //////////////////////////////////////////////////////////////////////////////////////////////
  178. //
  179. //////////////////////////////////////////////////////////////////////////////////////////////
  180. STDMETHODIMP CApplicationManager::GetAvailableSpace(const DWORD dwApplicationCategory, LPDWORD lpdwMaximumSpace, LPDWORD lpdwOptimalSpace)
  181. {
  182. FUNCTION("CApplicationManager::GetAvailableSpace ()");
  183. HRESULT hResult = S_OK;
  184. try
  185. {
  186. //
  187. // Check to make sure the pointer we receive is good
  188. //
  189. if ((NULL == lpdwMaximumSpace)||(NULL == lpdwOptimalSpace)||(IsBadWritePtr(lpdwMaximumSpace, sizeof(DWORD)))||(IsBadWritePtr(lpdwOptimalSpace, sizeof(DWORD))))
  190. {
  191. THROW(E_INVALIDARG);
  192. }
  193. hResult = m_InformationManager.GetMaximumAvailableSpace(dwApplicationCategory, lpdwMaximumSpace);
  194. if (SUCCEEDED(hResult))
  195. {
  196. hResult = m_InformationManager.GetOptimalAvailableSpace(dwApplicationCategory, lpdwOptimalSpace);
  197. }
  198. }
  199. ///////////////////////////////////////////////////////////////////////////////////////
  200. catch(CAppManExceptionHandler * pException)
  201. {
  202. //
  203. // Get Result code
  204. //
  205. hResult = pException->GetResultCode();
  206. //
  207. // Delete exception handler
  208. //
  209. delete pException;
  210. }
  211. catch(...)
  212. {
  213. //
  214. // If we failed to handle an exception, we default to APPMAN_E_CRITICALERROR
  215. //
  216. hResult = E_UNEXPECTED;
  217. }
  218. return hResult;
  219. }
  220. //////////////////////////////////////////////////////////////////////////////////////////////
  221. //
  222. // The CreateObject() method will return a new instantiation of an IApplicationEntry
  223. // to the caller.
  224. //
  225. //////////////////////////////////////////////////////////////////////////////////////////////
  226. STDMETHODIMP CApplicationManager::CreateApplicationEntry(IApplicationEntry ** lppObject)
  227. {
  228. FUNCTION("CApplicationManager::CreateApplicationEntry ()");
  229. HRESULT hResult = S_OK;
  230. ///////////////////////////////////////////////////////////////////////////////////////
  231. try
  232. {
  233. //
  234. // Check to make sure the pointer we receive is good
  235. //
  236. if ((NULL == lppObject)||(IsBadWritePtr(lppObject, sizeof(IApplicationEntry *))))
  237. {
  238. THROW(E_INVALIDARG);
  239. }
  240. *lppObject = (IApplicationEntry *) new CApplicationEntry;
  241. hResult = ((CApplicationEntry *) *lppObject)->Initialize();
  242. }
  243. ///////////////////////////////////////////////////////////////////////////////////////
  244. catch(CAppManExceptionHandler * pException)
  245. {
  246. //
  247. // Get Result code
  248. //
  249. hResult = pException->GetResultCode();
  250. //
  251. // Make sure we clean up and delete the CApplicationEntry object
  252. //
  253. if ((NULL != lppObject)&&(!IsBadReadPtr(lppObject, sizeof(IApplicationEntry *))))
  254. {
  255. delete ((CApplicationEntry *) *lppObject);
  256. *lppObject = NULL;
  257. }
  258. //
  259. // Delete exception handler
  260. //
  261. delete pException;
  262. }
  263. catch(...)
  264. {
  265. //
  266. // Make sure we clean up and delete the CApplicationEntry object
  267. //
  268. if ((NULL != lppObject)&&(!IsBadReadPtr(lppObject, sizeof(IApplicationEntry *))))
  269. {
  270. delete ((CApplicationEntry *) *lppObject);
  271. *lppObject = NULL;
  272. }
  273. //
  274. // If we failed to handle an exception, we default to APPMAN_E_CRITICALERROR
  275. //
  276. hResult = E_UNEXPECTED;
  277. }
  278. ///////////////////////////////////////////////////////////////////////////////////////
  279. return hResult;
  280. }
  281. //////////////////////////////////////////////////////////////////////////////////////////////
  282. //
  283. // The GetApplicationInfo() method will take a pre-initialized object as parameter and seek
  284. // the first matching registered object based on what was pre-initialized in the pObject. The
  285. // valid fields for pre-initialization in order of importance are:
  286. //
  287. // OBJECT_PROPERTY_GUID
  288. // OBJECT_PROPERTY_SIGNATURE
  289. //
  290. // If none of the above properties are set within the object being passed in, the function
  291. // will fail.
  292. //
  293. //////////////////////////////////////////////////////////////////////////////////////////////
  294. STDMETHODIMP CApplicationManager::GetApplicationInfo(IApplicationEntry * lpObject)
  295. {
  296. FUNCTION("CApplicationManager::GetApplicationInfo ()");
  297. HRESULT hResult = S_OK;
  298. ///////////////////////////////////////////////////////////////////////////////////////
  299. try
  300. {
  301. HRESULT hGuidInitialized;
  302. //
  303. // Check to make sure the pointer we receive is good
  304. //
  305. if ((NULL == lpObject)||(IsBadReadPtr(lpObject, sizeof(CApplicationEntry))))
  306. {
  307. THROW(E_INVALIDARG);
  308. }
  309. if (CURRENT_ACTION_NONE != ((CApplicationEntry *) lpObject)->GetActionState())
  310. {
  311. THROW(APPMAN_E_ACTIONINPROGRESS);
  312. }
  313. hGuidInitialized = m_InformationManager.IsApplicationPropertyInitializedWithIndex(IDX_PROPERTY_GUID, ((CApplicationEntry *) lpObject)->GetApplicationDataPtr());
  314. hResult = m_InformationManager.GetApplicationData(((CApplicationEntry *) lpObject)->GetApplicationDataPtr());
  315. if (SUCCEEDED(hResult))
  316. {
  317. if (S_OK == hGuidInitialized)
  318. {
  319. ((CApplicationEntry *) lpObject)->SetInitializationLevel(INIT_LEVEL_TOTAL);
  320. }
  321. else
  322. {
  323. ((CApplicationEntry *) lpObject)->SetInitializationLevel(INIT_LEVEL_BASIC);
  324. }
  325. }
  326. }
  327. ///////////////////////////////////////////////////////////////////////////////////////
  328. catch(CAppManExceptionHandler * pException)
  329. {
  330. //
  331. // Get Result code
  332. //
  333. hResult = pException->GetResultCode();
  334. //
  335. // Delete exception handler
  336. //
  337. delete pException;
  338. }
  339. catch(...)
  340. {
  341. //
  342. // If we failed to handle an exception, we default to APPMAN_E_CRITICALERROR
  343. //
  344. hResult = E_UNEXPECTED;
  345. }
  346. ///////////////////////////////////////////////////////////////////////////////////////
  347. return hResult;
  348. }
  349. //////////////////////////////////////////////////////////////////////////////////////////////
  350. //
  351. //////////////////////////////////////////////////////////////////////////////////////////////
  352. STDMETHODIMP CApplicationManager::EnumApplications(const DWORD dwApplicationIndex, IApplicationEntry * lpObject)
  353. {
  354. FUNCTION("CApplicationManager::EnumApplications ()");
  355. HRESULT hResult = S_OK;
  356. ///////////////////////////////////////////////////////////////////////////////////////
  357. try
  358. {
  359. //
  360. // Check to make sure the pointer we receive is good
  361. //
  362. if ((NULL == lpObject)||(IsBadReadPtr(lpObject, sizeof(CApplicationEntry))))
  363. {
  364. THROW(E_INVALIDARG);
  365. }
  366. hResult = m_InformationManager.GetApplicationDataWithIndex(dwApplicationIndex, ((CApplicationEntry *) lpObject)->GetApplicationDataPtr());
  367. if (SUCCEEDED(hResult))
  368. {
  369. ((CApplicationEntry *) lpObject)->SetInitializationLevel(INIT_LEVEL_BASIC);
  370. }
  371. }
  372. ///////////////////////////////////////////////////////////////////////////////////////
  373. catch(CAppManExceptionHandler * pException)
  374. {
  375. //
  376. // Get Result code
  377. //
  378. hResult = pException->GetResultCode();
  379. //
  380. // Delete exception handler
  381. //
  382. delete pException;
  383. }
  384. catch(...)
  385. {
  386. //
  387. // If we failed to handle an exception, we default to APPMAN_E_CRITICALERROR
  388. //
  389. hResult = E_UNEXPECTED;
  390. }
  391. ///////////////////////////////////////////////////////////////////////////////////////
  392. return hResult;
  393. }
  394. //////////////////////////////////////////////////////////////////////////////////////////////
  395. //
  396. //////////////////////////////////////////////////////////////////////////////////////////////
  397. STDMETHODIMP CApplicationManager::EnumDevices(const DWORD dwTargetIndex, LPDWORD lpdwKilobytes, LPDWORD lpdwExclusionMask, const DWORD dwStringMask, LPVOID lpData, const DWORD dwDataLen)
  398. {
  399. FUNCTION("CApplicationManager::EnumDevices()");
  400. HRESULT hResult = S_OK;
  401. ///////////////////////////////////////////////////////////////////////////////////////
  402. try
  403. {
  404. DEVICE_RECORD sDeviceRecord = {0};
  405. BOOL fFound;
  406. DWORD dwIndex, dwActualTargetIndex;
  407. DWORD dwKilobytes = 0;
  408. //
  409. // Make sure the string mask is good
  410. //
  411. if ((APP_PROPERTY_STR_ANSI != dwStringMask)&&(APP_PROPERTY_STR_UNICODE != dwStringMask))
  412. {
  413. THROW(APPMAN_E_INVALIDPARAMETERS);
  414. }
  415. //
  416. // Check to make sure dwDataLen is greater than 0
  417. //
  418. if ((0 == dwDataLen)||(NULL == lpData)||(IsBadWritePtr(lpData, dwDataLen)))
  419. {
  420. THROW(APPMAN_E_INVALIDPARAMETERS);
  421. }
  422. //
  423. // Check to make sure lpdwKilobytes is valid
  424. //
  425. if ((NULL == lpdwKilobytes)||(IsBadWritePtr(lpdwKilobytes, sizeof(DWORD))))
  426. {
  427. THROW(APPMAN_E_INVALIDPARAMETERS);
  428. }
  429. //
  430. // Check to make sure lpdwExclusionMask is valid
  431. //
  432. if ((NULL == lpdwExclusionMask)||(IsBadWritePtr(lpdwExclusionMask, sizeof(DWORD))))
  433. {
  434. THROW(APPMAN_E_INVALIDPARAMETERS);
  435. }
  436. //
  437. // Get the nth device record.
  438. //
  439. fFound = FALSE;
  440. dwActualTargetIndex = dwTargetIndex;
  441. dwIndex = 0;
  442. do
  443. {
  444. if (S_OK == m_InformationManager.CheckDeviceExistance(dwIndex))
  445. {
  446. if (dwActualTargetIndex == dwIndex)
  447. {
  448. hResult = m_InformationManager.GetDeviceInfoWithIndex(dwIndex, &sDeviceRecord);
  449. if (SUCCEEDED(hResult))
  450. {
  451. hResult = m_InformationManager.GetDeviceMaximumSpaceWithIndex(dwIndex, &dwKilobytes);
  452. if (SUCCEEDED(hResult))
  453. {
  454. fFound = TRUE;
  455. }
  456. }
  457. }
  458. }
  459. else
  460. {
  461. if (25 > dwActualTargetIndex)
  462. {
  463. dwActualTargetIndex++;
  464. }
  465. }
  466. dwIndex++;
  467. }
  468. while ((FALSE == fFound)&&(dwIndex <= dwActualTargetIndex)&&(SUCCEEDED(hResult)));
  469. //
  470. // Did we get the information we were looking for
  471. //
  472. if (TRUE == fFound)
  473. {
  474. //
  475. // Get the Program files folder
  476. //
  477. CRegistryKey oRegistryKey;
  478. CHAR szProgramFiles[MAX_PATH];
  479. DWORD dwSize, dwType;
  480. if (SUCCEEDED(oRegistryKey.OpenKey(HKEY_LOCAL_MACHINE, TEXT("Software\\Microsoft\\Windows\\CurrentVersion"), KEY_READ)))
  481. {
  482. dwSize = sizeof(szProgramFiles);
  483. if (SUCCEEDED(oRegistryKey.GetValue(TEXT("ProgramFilesDir"), &dwType, (LPBYTE) szProgramFiles, &dwSize)))
  484. {
  485. *lpdwKilobytes = dwKilobytes;
  486. *lpdwExclusionMask = sDeviceRecord.sDeviceInfo.dwApplicationCategoryExclusionMask;
  487. szProgramFiles[0] = ((CHAR) (sDeviceRecord.sDeviceInfo.dwDeviceIndex + 65));
  488. strcat(szProgramFiles, "\\");
  489. if (StrLenA(szProgramFiles) > dwDataLen)
  490. {
  491. THROW(APPMAN_E_OVERFLOW);
  492. }
  493. if (APP_PROPERTY_STR_ANSI == dwStringMask)
  494. {
  495. sprintf((LPSTR) lpData, "%s", szProgramFiles);
  496. }
  497. else
  498. {
  499. CWin32API oWin32API;
  500. oWin32API.MultiByteToWideChar(szProgramFiles, MAX_PATH, (LPWSTR) lpData, dwDataLen);
  501. }
  502. }
  503. }
  504. }
  505. else
  506. {
  507. hResult = APPMAN_E_INVALIDINDEX;
  508. }
  509. }
  510. ///////////////////////////////////////////////////////////////////////////////////////
  511. catch(CAppManExceptionHandler * pException)
  512. {
  513. hResult = pException->GetResultCode();
  514. delete pException;
  515. }
  516. catch(...)
  517. {
  518. hResult = E_UNEXPECTED;
  519. }
  520. ///////////////////////////////////////////////////////////////////////////////////////
  521. return hResult;
  522. }