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.

607 lines
12 KiB

  1. //-------------------------------------------------------------------------
  2. // File: ntdmutils.cpp
  3. //
  4. // Author : Kishnan Nedungadi
  5. //
  6. // created : 3/27/2000
  7. //-------------------------------------------------------------------------
  8. #include "stdafx.h"
  9. #include "Commdlg.h"
  10. #include "Cderr.h"
  11. #include <wbemidl.h>
  12. #include "defines.h"
  13. #include "resource.h"
  14. #include "ntdmutils.h"
  15. //-------------------------------------------------------------------------
  16. // Globals
  17. USHORT g_CIMTypes[] = {
  18. CIM_ILLEGAL,
  19. CIM_EMPTY ,
  20. CIM_SINT8 ,
  21. CIM_UINT8 ,
  22. CIM_SINT16 ,
  23. CIM_UINT16 ,
  24. CIM_SINT32 ,
  25. CIM_UINT32 ,
  26. CIM_SINT64 ,
  27. CIM_UINT64 ,
  28. CIM_REAL32 ,
  29. CIM_REAL64 ,
  30. CIM_BOOLEAN ,
  31. CIM_STRING ,
  32. CIM_DATETIME ,
  33. CIM_REFERENCE ,
  34. CIM_CHAR16 ,
  35. CIM_OBJECT ,
  36. CIM_FLAG_ARRAY
  37. };
  38. USHORT g_CIMToVARIANTTypes[] = {
  39. VT_EMPTY,
  40. VT_EMPTY,
  41. VT_I1 ,
  42. VT_UI1 ,
  43. VT_I2 ,
  44. VT_UI2 ,
  45. VT_I4 ,
  46. VT_UI4 ,
  47. VT_I8 ,
  48. VT_UI8 ,
  49. VT_R4 ,
  50. VT_R8 ,
  51. VT_BOOL ,
  52. VT_BSTR ,
  53. VT_BSTR ,
  54. VT_BYREF,
  55. VT_BSTR ,
  56. VT_DISPATCH ,
  57. VT_ARRAY
  58. };
  59. CSimpleArray<BSTR> g_bstrCIMTypes;
  60. //-------------------------------------------------------------------------
  61. STDMETHODIMP CNTDMUtils::Initialize()
  62. {
  63. HRESULT hr;
  64. NTDM_BEGIN_METHOD()
  65. NTDM_ERR_IF_FAIL(GetValuesInList((long)IDS_CIMTYPES, g_bstrCIMTypes));
  66. NTDM_END_METHOD()
  67. // cleanup
  68. return hr;
  69. }
  70. //-------------------------------------------------------------------------
  71. STDMETHODIMP CNTDMUtils::UnInitialize()
  72. {
  73. HRESULT hr;
  74. long i;
  75. NTDM_BEGIN_METHOD()
  76. for(i=0; i<g_bstrCIMTypes.GetSize(); i++)
  77. {
  78. NTDM_FREE_BSTR(g_bstrCIMTypes[i]);
  79. }
  80. g_bstrCIMTypes.RemoveAll();
  81. NTDM_END_METHOD()
  82. // cleanup
  83. return hr;
  84. }
  85. //-------------------------------------------------------------------------
  86. STDMETHODIMP CNTDMUtils::ErrorHandler(HWND hWnd, HRESULT err_hr, bool bShowError)
  87. {
  88. if(bShowError)
  89. DisplayErrorInfo(hWnd, err_hr);
  90. return S_OK;
  91. }
  92. //-------------------------------------------------------------------------
  93. STDMETHODIMP CNTDMUtils::DisplayErrorInfo(HWND hWnd, HRESULT err_hr)
  94. {
  95. HRESULT hr;
  96. CComBSTR bstrSource, bstrDescription;
  97. CComBSTR bstrTemp;
  98. CComBSTR bstrAllTogether;
  99. NTDM_BEGIN_METHOD()
  100. GetDetailedErrorInfo(hWnd, bstrSource, bstrDescription, err_hr);
  101. bstrAllTogether.LoadString(_Module.GetResourceInstance(), IDS_ERROR_SOURCE);
  102. bstrAllTogether += bstrSource;
  103. bstrAllTogether += _T("\r\n\r\n");
  104. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_ERROR_DESCRIPTION);
  105. bstrAllTogether += bstrTemp;
  106. bstrAllTogether += bstrDescription;
  107. bstrTemp.LoadString(_Module.GetResourceInstance(), IDS_ERROR);
  108. MessageBox(hWnd, bstrAllTogether, bstrTemp, MB_OK);
  109. NTDM_END_METHOD()
  110. // cleanup
  111. return hr;
  112. }
  113. //-------------------------------------------------------------------------
  114. STDMETHODIMP CNTDMUtils::GetDetailedErrorInfo(HWND hWnd, CComBSTR &bstrSource, CComBSTR &bstrDescription, HRESULT err_hr)
  115. {
  116. HRESULT hr;
  117. CComPtr<IErrorInfo>pIErrorInfo;
  118. HWND m_hWnd = hWnd;
  119. NTDM_BEGIN_METHOD()
  120. bstrSource = _T("");
  121. bstrDescription = _T("");
  122. //Get Error Object
  123. hr = GetErrorInfo(0,&pIErrorInfo);
  124. if(S_OK == hr)
  125. {
  126. pIErrorInfo->GetDescription(&bstrDescription);
  127. pIErrorInfo->GetSource(&bstrSource);
  128. }
  129. if(S_OK == hr && !bstrDescription.Length())
  130. {
  131. CComPtr<IWbemClassObject> pIWbemClassObject;
  132. CComVariant vValue;
  133. CIMTYPE vType;
  134. // check if WMI error
  135. if SUCCEEDED(hr = pIErrorInfo->QueryInterface(IID_IWbemClassObject, (void **)&pIWbemClassObject))
  136. {
  137. CComPtr<IWbemStatusCodeText> pIWbemStatusCodeText;
  138. if SUCCEEDED(hr = CoCreateInstance(CLSID_WbemStatusCodeText, 0, CLSCTX_INPROC_SERVER, IID_IWbemStatusCodeText, (LPVOID *) &pIWbemStatusCodeText))
  139. {
  140. pIWbemStatusCodeText->GetErrorCodeText(err_hr, 0, 0, &bstrDescription);
  141. pIWbemStatusCodeText->GetFacilityCodeText(err_hr, 0, 0, &bstrSource);
  142. // check for Rule Validation Results
  143. if SUCCEEDED(hr = pIWbemClassObject->Get(_T("RuleValidationResults"), 0, &vValue, &vType, NULL))
  144. {
  145. if(VT_NULL != V_VT(&vValue))
  146. {
  147. SAFEARRAY *pValidationResults = NULL;
  148. long lLower, lUpper, i;
  149. if( SUCCEEDED(hr = SafeArrayGetUBound(pValidationResults, 1, &lUpper)) &&
  150. SUCCEEDED(hr = SafeArrayGetLBound(pValidationResults, 1, &lLower)))
  151. {
  152. CComBSTR bstrErrors;
  153. for(i=lLower; i<=lUpper; i++)
  154. {
  155. if(V_VT(&vValue) & VT_HRESULT)
  156. {
  157. CComBSTR bstrError;
  158. HRESULT hr2;
  159. if SUCCEEDED(hr = SafeArrayGetElement(pValidationResults, &i, (void *)&hr2))
  160. {
  161. bstrErrors += _T("\r\n\r\n");
  162. pIWbemStatusCodeText->GetErrorCodeText(hr2, 0, 0, &bstrError);
  163. bstrErrors += bstrError;
  164. }
  165. }
  166. }
  167. bstrDescription += bstrErrors;
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. if(!bstrDescription.Length())
  175. {
  176. TCHAR pszTemp[SZ_MAX_SIZE];
  177. pszTemp[0] = NULL;
  178. if(!FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  179. NULL,
  180. err_hr,
  181. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  182. pszTemp,
  183. SZ_MAX_SIZE,
  184. NULL))
  185. {
  186. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  187. NULL,
  188. HRESULT_CODE(err_hr),
  189. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  190. pszTemp,
  191. SZ_MAX_SIZE,
  192. NULL);
  193. }
  194. bstrDescription = pszTemp;
  195. }
  196. if(!bstrDescription.Length())
  197. {
  198. bstrDescription.LoadString(_Module.GetResourceInstance(), IDS_NO_DESCRIPTION_PROVIDED);
  199. }
  200. if(!bstrSource.Length())
  201. {
  202. bstrSource.LoadString(_Module.GetResourceInstance(), IDS_NO_SOURCE_NAME_PROVIDED);
  203. }
  204. NTDM_END_METHOD()
  205. // cleanup
  206. return hr;
  207. }
  208. //-------------------------------------------------------------------------
  209. STDMETHODIMP CNTDMUtils::GetValuesInList(LPCTSTR strList, CSimpleArray<BSTR>&bstrArray, LPCTSTR pcszToken)
  210. {
  211. HRESULT hr;
  212. BSTR bstrTemp = NULL;
  213. BSTR bstrItem = NULL;
  214. TCHAR * pszItem;
  215. NTDM_BEGIN_METHOD()
  216. if(!strList || !_tcslen(strList))
  217. {
  218. NTDM_EXIT(NOERROR);
  219. }
  220. bstrTemp = SysAllocString(strList);
  221. pszItem = _tcstok(bstrTemp, pcszToken);
  222. while(pszItem)
  223. {
  224. bstrItem = SysAllocString(pszItem);
  225. NTDM_ERR_IF_FAIL(bstrArray.Add(bstrItem));
  226. bstrItem = NULL;
  227. pszItem = _tcstok(NULL, pcszToken);
  228. }
  229. NTDM_END_METHOD()
  230. // cleanup
  231. NTDM_FREE_BSTR(bstrTemp);
  232. NTDM_FREE_BSTR(bstrItem);
  233. return hr;
  234. }
  235. //-------------------------------------------------------------------------
  236. STDMETHODIMP CNTDMUtils::GetValuesInList(long lResID, CSimpleArray<BSTR>&bstrArray, LPCTSTR pcszToken)
  237. {
  238. HRESULT hr;
  239. CComBSTR bstrTemp;
  240. NTDM_BEGIN_METHOD()
  241. bstrTemp.LoadString(_Module.GetResourceInstance(), lResID);
  242. GetValuesInList(bstrTemp, bstrArray, pcszToken);
  243. NTDM_END_METHOD()
  244. // cleanup
  245. return hr;
  246. }
  247. //-------------------------------------------------------------------------
  248. STDMETHODIMP CNTDMUtils::GetCIMTypeFromString(LPCTSTR pcszCIMType, long *pCimTYPE)
  249. {
  250. HRESULT hr;
  251. long i;
  252. NTDM_BEGIN_METHOD()
  253. for(i=0; i<g_bstrCIMTypes.GetSize(); i++)
  254. {
  255. if(_tcscmp(pcszCIMType, g_bstrCIMTypes[i]) == 0)
  256. {
  257. *pCimTYPE = g_CIMTypes[i];
  258. break;
  259. }
  260. }
  261. if(i >= g_bstrCIMTypes.GetSize())
  262. {
  263. NTDM_EXIT(E_FAIL);
  264. }
  265. hr = NOERROR;
  266. NTDM_END_METHOD()
  267. // cleanup
  268. return hr;
  269. }
  270. //-------------------------------------------------------------------------
  271. STDMETHODIMP CNTDMUtils::GetVariantTypeFromString(LPCTSTR pcszCIMType, long *pVariantType)
  272. {
  273. HRESULT hr;
  274. long i;
  275. NTDM_BEGIN_METHOD()
  276. for(i=0; i<g_bstrCIMTypes.GetSize(); i++)
  277. {
  278. if(_tcscmp(pcszCIMType, g_bstrCIMTypes[i]) == 0)
  279. {
  280. *pVariantType = g_CIMToVARIANTTypes[i];
  281. break;
  282. }
  283. }
  284. if(i >= g_bstrCIMTypes.GetSize())
  285. {
  286. NTDM_EXIT(E_FAIL);
  287. }
  288. hr = NOERROR;
  289. NTDM_END_METHOD()
  290. // cleanup
  291. return hr;
  292. }
  293. //-------------------------------------------------------------------------
  294. STDMETHODIMP CNTDMUtils::SetStringProperty(IWbemClassObject* pIWbemClassObject, LPCTSTR pszPropName, HWND hwnd, long lResID)
  295. {
  296. HRESULT hr;
  297. CComVariant vValue;
  298. CComBSTR bstrTemp;
  299. long lLength;
  300. TCHAR *pszTemp = NULL;
  301. HWND m_hWnd = hwnd;
  302. NTDM_BEGIN_METHOD()
  303. // Set the Query language
  304. lLength = GetWindowTextLength(GetDlgItem(hwnd, lResID));
  305. if(lLength < 0)
  306. {
  307. NTDM_EXIT(E_FAIL);
  308. }
  309. else if(0 == lLength)
  310. {
  311. bstrTemp = _T("");
  312. }
  313. else
  314. {
  315. pszTemp = new TCHAR[lLength+1];
  316. if(!pszTemp)
  317. {
  318. NTDM_EXIT(E_OUTOFMEMORY);
  319. }
  320. NTDM_ERR_GETLASTERROR_IF_NULL(GetDlgItemText(hwnd, lResID, pszTemp, lLength+1));
  321. bstrTemp = pszTemp;
  322. NTDM_DELETE_OBJECT(pszTemp);
  323. }
  324. vValue = bstrTemp;
  325. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Put(pszPropName, 0, &vValue, CIM_STRING));
  326. NTDM_END_METHOD()
  327. // cleanup
  328. NTDM_DELETE_OBJECT(pszTemp);
  329. return hr;
  330. }
  331. //-------------------------------------------------------------------------
  332. STDMETHODIMP CNTDMUtils::GetDlgItemString(HWND hwnd, long lResID, CComBSTR &bstrValue)
  333. {
  334. HRESULT hr;
  335. CComBSTR bstrTemp;
  336. long lLength;
  337. TCHAR *pszTemp = NULL;
  338. HWND m_hWnd = hwnd;
  339. NTDM_BEGIN_METHOD()
  340. // Set the Query language
  341. lLength = GetWindowTextLength(GetDlgItem(hwnd, lResID));
  342. if(lLength < 0)
  343. {
  344. NTDM_EXIT(E_FAIL);
  345. }
  346. else if(0 == lLength)
  347. {
  348. bstrTemp = _T("");
  349. }
  350. else
  351. {
  352. pszTemp = new TCHAR[lLength+1];
  353. if(!pszTemp)
  354. {
  355. NTDM_EXIT(E_OUTOFMEMORY);
  356. }
  357. NTDM_ERR_GETLASTERROR_IF_NULL(GetDlgItemText(hwnd, lResID, pszTemp, lLength+1));
  358. bstrValue = pszTemp;
  359. NTDM_DELETE_OBJECT(pszTemp);
  360. }
  361. NTDM_END_METHOD()
  362. // cleanup
  363. NTDM_DELETE_OBJECT(pszTemp);
  364. return hr;
  365. }
  366. //-------------------------------------------------------------------------
  367. STDMETHODIMP CNTDMUtils::GetStringProperty(IWbemClassObject* pIWbemClassObject, LPCTSTR pszPropName, HWND hwnd, long lResID)
  368. {
  369. HRESULT hr;
  370. HWND m_hWnd = hwnd;
  371. CComVariant vValue;
  372. CIMTYPE cimType;
  373. NTDM_BEGIN_METHOD()
  374. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(pszPropName, 0, &vValue, &cimType, NULL));
  375. SetDlgItemText(hwnd, lResID, V_BSTR(&vValue));
  376. NTDM_END_METHOD()
  377. return hr;
  378. }
  379. //-----------------------------------------------------------------------------------------
  380. BOOL CNTDMUtils::SaveFileNameDlg(LPCTSTR szFilter, LPCTSTR extension, HWND hwnd, LPTSTR pszFile)
  381. {
  382. OPENFILENAME ofn;
  383. BOOL fRet;
  384. HANDLE hFile = NULL;
  385. memset(&ofn, 0, sizeof(OPENFILENAME));
  386. ofn.lStructSize =sizeof(OPENFILENAME);
  387. ofn.hwndOwner =hwnd;
  388. ofn.lpstrFilter =szFilter;
  389. ofn.nFilterIndex =1L;
  390. ofn.lpstrFile =pszFile;
  391. ofn.nMaxFile =MAX_PATH;
  392. ofn.lpstrDefExt =TEXT("*");
  393. ofn.Flags =OFN_HIDEREADONLY;
  394. fRet=GetSaveFileName(&ofn);
  395. if(fRet == 0)
  396. {
  397. if(FNERR_INVALIDFILENAME == CommDlgExtendedError())
  398. {
  399. _tcscpy(pszFile, TEXT(""));
  400. fRet=GetSaveFileName(&ofn);
  401. }
  402. }
  403. return fRet;
  404. }
  405. //-----------------------------------------------------------------------------------------
  406. BOOL CNTDMUtils::OpenFileNameDlg(LPCTSTR szFilter, LPCTSTR extension, HWND hwnd, LPTSTR pszFile)
  407. {
  408. OPENFILENAME ofn;
  409. BOOL fRet;
  410. HANDLE hFile = NULL;
  411. memset(&ofn, 0, sizeof(OPENFILENAME));
  412. ofn.lStructSize =sizeof(OPENFILENAME);
  413. ofn.hwndOwner =hwnd;
  414. ofn.lpstrFilter =szFilter;
  415. ofn.nFilterIndex =1L;
  416. ofn.lpstrFile =pszFile;
  417. ofn.nMaxFile =MAX_PATH;
  418. ofn.lpstrDefExt =TEXT("*");
  419. ofn.Flags =OFN_HIDEREADONLY;
  420. fRet=GetOpenFileName(&ofn);
  421. if(fRet == 0)
  422. {
  423. if(FNERR_INVALIDFILENAME == CommDlgExtendedError())
  424. {
  425. _tcscpy(pszFile, TEXT(""));
  426. fRet=GetOpenFileName(&ofn);
  427. }
  428. }
  429. return fRet;
  430. }
  431. //-----------------------------------------------------------------------------------------
  432. void CNTDMUtils::ReplaceCharacter(TCHAR * string_val, const TCHAR replace_old, const TCHAR replace_new)
  433. {
  434. TCHAR * p_cur = string_val;
  435. while(*p_cur)
  436. {
  437. if(replace_old == *p_cur)
  438. *p_cur = replace_new;
  439. p_cur++;
  440. }
  441. }
  442. //-------------------------------------------------------------------------
  443. long CNTDMUtils::DisplayMessage(HWND hParent, long iMsgID, long iTitleID, long iType)
  444. {
  445. CComBSTR bstrMsg, bstrTitle;
  446. bstrMsg.LoadString(_Module.GetResourceInstance(), iMsgID);
  447. bstrTitle.LoadString(_Module.GetResourceInstance(), iTitleID);
  448. return MessageBox(hParent, bstrMsg, bstrTitle, iType);
  449. }
  450. //-----------------------------------------------------------------------------------------
  451. void CNTDMUtils::DisplayDlgItem(HWND hWndDlg, long item_id, BOOL show)
  452. {
  453. if(show)
  454. {
  455. ShowWindow(GetDlgItem(hWndDlg, item_id), SW_SHOW);
  456. EnableWindow(GetDlgItem(hWndDlg, item_id), TRUE);
  457. }
  458. else
  459. {
  460. ShowWindow(GetDlgItem(hWndDlg, item_id), SW_HIDE);
  461. EnableWindow(GetDlgItem(hWndDlg, item_id), FALSE);
  462. }
  463. }