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.

616 lines
13 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((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. if (!bstrArray.Add(bstrItem))
  226. {
  227. hr = E_FAIL;
  228. CNTDMUtils::ErrorHandler(NULL, hr, FALSE);
  229. goto error;
  230. }
  231. bstrItem = NULL;
  232. pszItem = _tcstok(NULL, pcszToken);
  233. }
  234. NTDM_END_METHOD()
  235. // cleanup
  236. NTDM_FREE_BSTR(bstrTemp);
  237. NTDM_FREE_BSTR(bstrItem);
  238. return hr;
  239. }
  240. //-------------------------------------------------------------------------
  241. STDMETHODIMP CNTDMUtils::GetValuesInList(long lResID, CSimpleArray<BSTR>&bstrArray, LPCTSTR pcszToken)
  242. {
  243. HRESULT hr;
  244. CComBSTR bstrTemp;
  245. NTDM_BEGIN_METHOD()
  246. bstrTemp.LoadString(_Module.GetResourceInstance(), lResID);
  247. GetValuesInList(bstrTemp, bstrArray, pcszToken);
  248. NTDM_END_METHOD()
  249. // cleanup
  250. return hr;
  251. }
  252. //-------------------------------------------------------------------------
  253. STDMETHODIMP CNTDMUtils::GetCIMTypeFromString(LPCTSTR pcszCIMType, long *pCimTYPE)
  254. {
  255. HRESULT hr;
  256. long i;
  257. NTDM_BEGIN_METHOD()
  258. for(i=0; i<g_bstrCIMTypes.GetSize(); i++)
  259. {
  260. if(_tcscmp(pcszCIMType, g_bstrCIMTypes[i]) == 0)
  261. {
  262. *pCimTYPE = g_CIMTypes[i];
  263. break;
  264. }
  265. }
  266. if(i >= g_bstrCIMTypes.GetSize())
  267. {
  268. NTDM_EXIT(E_FAIL);
  269. }
  270. hr = NOERROR;
  271. NTDM_END_METHOD()
  272. // cleanup
  273. return hr;
  274. }
  275. //-------------------------------------------------------------------------
  276. STDMETHODIMP CNTDMUtils::GetVariantTypeFromString(LPCTSTR pcszCIMType, long *pVariantType)
  277. {
  278. HRESULT hr;
  279. long i;
  280. NTDM_BEGIN_METHOD()
  281. for(i=0; i<g_bstrCIMTypes.GetSize(); i++)
  282. {
  283. if(_tcscmp(pcszCIMType, g_bstrCIMTypes[i]) == 0)
  284. {
  285. *pVariantType = g_CIMToVARIANTTypes[i];
  286. break;
  287. }
  288. }
  289. if(i >= g_bstrCIMTypes.GetSize())
  290. {
  291. NTDM_EXIT(E_FAIL);
  292. }
  293. hr = NOERROR;
  294. NTDM_END_METHOD()
  295. // cleanup
  296. return hr;
  297. }
  298. //-------------------------------------------------------------------------
  299. STDMETHODIMP CNTDMUtils::SetStringProperty(IWbemClassObject* pIWbemClassObject, LPCTSTR pszPropName, HWND hwnd, long lResID)
  300. {
  301. HRESULT hr;
  302. CComVariant vValue;
  303. CComBSTR bstrTemp;
  304. long lLength;
  305. TCHAR *pszTemp = NULL;
  306. HWND m_hWnd = hwnd;
  307. NTDM_BEGIN_METHOD()
  308. // Set the Query language
  309. lLength = GetWindowTextLength(GetDlgItem(hwnd, lResID));
  310. if(lLength < 0)
  311. {
  312. NTDM_EXIT(E_FAIL);
  313. }
  314. else if(0 == lLength)
  315. {
  316. bstrTemp = _T("");
  317. }
  318. else
  319. {
  320. pszTemp = new TCHAR[lLength+1];
  321. if(!pszTemp)
  322. {
  323. NTDM_EXIT(E_OUTOFMEMORY);
  324. }
  325. NTDM_ERR_GETLASTERROR_IF_NULL(GetDlgItemText(hwnd, lResID, pszTemp, lLength+1));
  326. bstrTemp = pszTemp;
  327. delete [] pszTemp;
  328. pszTemp = NULL;
  329. }
  330. vValue = bstrTemp;
  331. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Put(pszPropName, 0, &vValue, CIM_STRING));
  332. NTDM_END_METHOD()
  333. // cleanup
  334. delete [] pszTemp;
  335. return hr;
  336. }
  337. //-------------------------------------------------------------------------
  338. STDMETHODIMP CNTDMUtils::GetDlgItemString(HWND hwnd, long lResID, CComBSTR &bstrValue)
  339. {
  340. HRESULT hr;
  341. CComBSTR bstrTemp;
  342. long lLength;
  343. TCHAR *pszTemp = NULL;
  344. HWND m_hWnd = hwnd;
  345. NTDM_BEGIN_METHOD()
  346. // Set the Query language
  347. lLength = GetWindowTextLength(GetDlgItem(hwnd, lResID));
  348. if(lLength < 0)
  349. {
  350. NTDM_EXIT(E_FAIL);
  351. }
  352. else if(0 == lLength)
  353. {
  354. bstrTemp = _T("");
  355. }
  356. else
  357. {
  358. pszTemp = new TCHAR[lLength+1];
  359. if(!pszTemp)
  360. {
  361. NTDM_EXIT(E_OUTOFMEMORY);
  362. }
  363. NTDM_ERR_GETLASTERROR_IF_NULL(GetDlgItemText(hwnd, lResID, pszTemp, lLength+1));
  364. pszTemp[lLength] = L'\0';
  365. bstrValue = pszTemp;
  366. delete [] pszTemp;
  367. pszTemp = NULL;
  368. }
  369. NTDM_END_METHOD()
  370. // cleanup
  371. delete [] pszTemp;
  372. return hr;
  373. }
  374. //-------------------------------------------------------------------------
  375. STDMETHODIMP CNTDMUtils::GetStringProperty(IWbemClassObject* pIWbemClassObject, LPCTSTR pszPropName, HWND hwnd, long lResID)
  376. {
  377. HRESULT hr;
  378. HWND m_hWnd = hwnd;
  379. CComVariant vValue;
  380. CIMTYPE cimType;
  381. NTDM_BEGIN_METHOD()
  382. NTDM_ERR_MSG_IF_FAIL(pIWbemClassObject->Get(pszPropName, 0, &vValue, &cimType, NULL));
  383. SetDlgItemText(hwnd, lResID, V_BSTR(&vValue));
  384. NTDM_END_METHOD()
  385. return hr;
  386. }
  387. //-----------------------------------------------------------------------------------------
  388. BOOL CNTDMUtils::SaveFileNameDlg(LPCTSTR szFilter, LPCTSTR extension, HWND hwnd, LPTSTR pszFile)
  389. {
  390. OPENFILENAME ofn;
  391. BOOL fRet;
  392. HANDLE hFile = NULL;
  393. memset(&ofn, 0, sizeof(OPENFILENAME));
  394. ofn.lStructSize =sizeof(OPENFILENAME);
  395. ofn.hwndOwner =hwnd;
  396. ofn.lpstrFilter =szFilter;
  397. ofn.nFilterIndex =1L;
  398. ofn.lpstrFile =pszFile;
  399. ofn.nMaxFile =MAX_PATH;
  400. ofn.lpstrDefExt =TEXT("*");
  401. ofn.Flags =OFN_HIDEREADONLY;
  402. fRet=GetSaveFileName(&ofn);
  403. if(fRet == 0)
  404. {
  405. if(FNERR_INVALIDFILENAME == CommDlgExtendedError())
  406. {
  407. (void) StringCchCopy(pszFile, MAX_PATH, TEXT(""));
  408. fRet=GetSaveFileName(&ofn);
  409. }
  410. }
  411. return fRet;
  412. }
  413. //-----------------------------------------------------------------------------------------
  414. BOOL CNTDMUtils::OpenFileNameDlg(LPCTSTR szFilter, LPCTSTR extension, HWND hwnd, LPTSTR pszFile)
  415. {
  416. OPENFILENAME ofn;
  417. BOOL fRet;
  418. HANDLE hFile = NULL;
  419. memset(&ofn, 0, sizeof(OPENFILENAME));
  420. ofn.lStructSize =sizeof(OPENFILENAME);
  421. ofn.hwndOwner =hwnd;
  422. ofn.lpstrFilter =szFilter;
  423. ofn.nFilterIndex =1L;
  424. ofn.lpstrFile =pszFile;
  425. ofn.nMaxFile =MAX_PATH;
  426. ofn.lpstrDefExt =TEXT("*");
  427. ofn.Flags =OFN_HIDEREADONLY;
  428. fRet=GetOpenFileName(&ofn);
  429. if(fRet == 0)
  430. {
  431. if(FNERR_INVALIDFILENAME == CommDlgExtendedError())
  432. {
  433. (void) StringCchCopy(pszFile, MAX_PATH, TEXT(""));
  434. fRet=GetOpenFileName(&ofn);
  435. }
  436. }
  437. return fRet;
  438. }
  439. //-----------------------------------------------------------------------------------------
  440. void CNTDMUtils::ReplaceCharacter(TCHAR * string_val, const TCHAR replace_old, const TCHAR replace_new)
  441. {
  442. TCHAR * p_cur = string_val;
  443. while(*p_cur)
  444. {
  445. if(replace_old == *p_cur)
  446. *p_cur = replace_new;
  447. p_cur++;
  448. }
  449. }
  450. //-------------------------------------------------------------------------
  451. long CNTDMUtils::DisplayMessage(HWND hParent, long iMsgID, long iTitleID, long iType)
  452. {
  453. CComBSTR bstrMsg, bstrTitle;
  454. bstrMsg.LoadString(_Module.GetResourceInstance(), iMsgID);
  455. bstrTitle.LoadString(_Module.GetResourceInstance(), iTitleID);
  456. return MessageBox(hParent, bstrMsg, bstrTitle, iType);
  457. }
  458. //-----------------------------------------------------------------------------------------
  459. void CNTDMUtils::DisplayDlgItem(HWND hWndDlg, long item_id, BOOL show)
  460. {
  461. if(show)
  462. {
  463. ShowWindow(GetDlgItem(hWndDlg, item_id), SW_SHOW);
  464. EnableWindow(GetDlgItem(hWndDlg, item_id), TRUE);
  465. }
  466. else
  467. {
  468. ShowWindow(GetDlgItem(hWndDlg, item_id), SW_HIDE);
  469. EnableWindow(GetDlgItem(hWndDlg, item_id), FALSE);
  470. }
  471. }