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.

680 lines
16 KiB

  1. //***************************************************************************
  2. //
  3. // (c) 1999-2001 by Microsoft Corp. All Rights Reserved.
  4. //
  5. // reputils.cpp
  6. //
  7. // cvadai 6-May-1999 created.
  8. //
  9. //***************************************************************************
  10. #define _REPUTILS_CPP_
  11. #include "precomp.h"
  12. #include <wbemcli.h>
  13. #include <wbemutil.h>
  14. #include <comutil.h>
  15. #include <stdio.h>
  16. #include <reputils.h>
  17. #include <flexarry.h>
  18. #include <wstring.h>
  19. #include <wqllex.h>
  20. #include <genutils.h>
  21. //***************************************************************************
  22. void SetBoolQualifier(IWbemQualifierSet *pQS, LPCWSTR lpQName, long lFlavor)
  23. {
  24. VARIANT var;
  25. VariantInit(&var);
  26. var.vt = VT_BOOL;
  27. var.boolVal = 1;
  28. pQS->Put(lpQName, &var, lFlavor);
  29. VariantClear(&var);
  30. }
  31. //***************************************************************************
  32. LPWSTR StripEscapes (LPWSTR lpIn)
  33. {
  34. WCHAR szTmp[450];
  35. wchar_t *pRet = NULL;
  36. int i, iPos = 0;
  37. bool bInit = false;
  38. int iLen = wcslen(lpIn);
  39. if (iLen)
  40. {
  41. for (i = 0; i < iLen; i++)
  42. {
  43. WCHAR t = lpIn[i];
  44. if (!bInit && t == '\\')
  45. {
  46. bInit = true;
  47. continue;
  48. }
  49. bInit = false;
  50. szTmp[iPos] = t;
  51. iPos++;
  52. }
  53. }
  54. szTmp[iPos] = '\0';
  55. pRet = new wchar_t [wcslen(szTmp)+1];
  56. if (pRet)
  57. wcscpy(pRet, szTmp);
  58. return pRet;
  59. }
  60. //***************************************************************************
  61. LPWSTR GetStr (DWORD dwValue)
  62. {
  63. wchar_t *pTemp = new wchar_t [30];
  64. if (pTemp)
  65. swprintf(pTemp, L"%ld", dwValue);
  66. return pTemp;
  67. }
  68. //***************************************************************************
  69. LPWSTR GetStr(double dValue)
  70. {
  71. wchar_t *pTemp = new wchar_t [30];
  72. if (pTemp)
  73. swprintf(pTemp, L"%lG", dValue);
  74. return pTemp;
  75. }
  76. //***************************************************************************
  77. LPWSTR GetStr (SQL_ID dValue)
  78. {
  79. wchar_t *pTemp = new wchar_t [30];
  80. if (pTemp)
  81. swprintf(pTemp, L"%I64d", dValue);
  82. return pTemp;
  83. }
  84. //***************************************************************************
  85. LPWSTR GetStr (float dValue)
  86. {
  87. wchar_t *pTemp = new wchar_t [30];
  88. if (pTemp)
  89. swprintf(pTemp, L"%g", dValue);
  90. return pTemp;
  91. }
  92. //***************************************************************************
  93. LPWSTR GetStr (VARIANT &vValue)
  94. {
  95. IWbemClassObject *pTemp;
  96. VARIANT vTemp;
  97. CIMTYPE cimtype;
  98. long lTemp;
  99. wchar_t *pRet = NULL;
  100. switch( vValue.vt)
  101. {
  102. case VT_I1:
  103. pRet = GetStr((DWORD)vValue.cVal);
  104. break;
  105. case VT_UI1:
  106. pRet = GetStr((DWORD)vValue.bVal);
  107. break;
  108. case VT_I2:
  109. pRet = GetStr((DWORD)vValue.iVal);
  110. break;
  111. case VT_I4:
  112. pRet = GetStr((DWORD)vValue.lVal);
  113. break;
  114. case VT_BOOL:
  115. pRet = GetStr((DWORD)vValue.boolVal);
  116. break;
  117. case VT_R4:
  118. pRet = GetStr((float)vValue.fltVal);
  119. break;
  120. case VT_R8:
  121. pRet = GetStr((double)vValue.dblVal);
  122. break;
  123. case VT_NULL:
  124. case VT_EMPTY:
  125. pRet = NULL;
  126. break;
  127. case VT_BSTR:
  128. pRet = StripQuotes(vValue.bstrVal);
  129. break;
  130. case VT_UNKNOWN:
  131. VariantInit(&vTemp);
  132. pTemp = (IWbemClassObject *)V_UNKNOWN(&vValue);
  133. if (pTemp)
  134. {
  135. pTemp->Get(L"__RelPath", 0, &vTemp, &cimtype, &lTemp);
  136. if (vTemp.vt == VT_BSTR)
  137. pRet = StripQuotes(vTemp.bstrVal);
  138. }
  139. VariantClear(&vTemp);
  140. break;
  141. case VT_UI1|VT_ARRAY:
  142. pRet = new wchar_t [1];
  143. if (pRet)
  144. pRet[0] = L'\0';
  145. break;
  146. default:
  147. pRet = new wchar_t [1];
  148. if (pRet)
  149. pRet[0] = L'\0';
  150. }
  151. return pRet;
  152. }
  153. //***************************************************************************
  154. LPWSTR GetPropertyVal (LPWSTR lpProp, IWbemClassObject *pObj)
  155. {
  156. LPWSTR lpRet = NULL;
  157. VARIANT vVal;
  158. VariantInit(&vVal);
  159. if (SUCCEEDED(pObj->Get(lpProp, 0, &vVal, NULL, NULL)))
  160. {
  161. lpRet = GetStr(vVal);
  162. VariantClear(&vVal);
  163. }
  164. return lpRet;
  165. }
  166. //***************************************************************************
  167. DWORD GetQualifierFlag (LPWSTR lpQfrName, IWbemQualifierSet *pQS)
  168. {
  169. DWORD dwRet = 0;
  170. VARIANT vVal;
  171. VariantInit(&vVal);
  172. if (SUCCEEDED(pQS->Get(lpQfrName, 0, &vVal, NULL)))
  173. dwRet = vVal.lVal;
  174. VariantClear(&vVal);
  175. return dwRet;
  176. }
  177. //***************************************************************************
  178. DWORD GetStorageType (CIMTYPE cimtype, bool bArray)
  179. {
  180. DWORD dwRet = 0;
  181. if (cimtype == CIM_STRING || cimtype == CIM_DATETIME)
  182. dwRet = WMIDB_STORAGE_STRING;
  183. else if (cimtype == CIM_REAL32 || cimtype == CIM_REAL64)
  184. dwRet = WMIDB_STORAGE_REAL;
  185. else if (cimtype == CIM_REFERENCE || cimtype == CIM_OBJECT)
  186. dwRet = WMIDB_STORAGE_REFERENCE;
  187. else if ((cimtype == CIM_UINT8) && bArray)
  188. dwRet = WMIDB_STORAGE_IMAGE;
  189. else dwRet = WMIDB_STORAGE_NUMERIC;
  190. return dwRet;
  191. }
  192. //***************************************************************************
  193. HRESULT GetVariantFromArray (SAFEARRAY *psaArray, long iPos, long vt, VARIANT &vTemp)
  194. {
  195. HRESULT hr = 0;
  196. BOOL bTemp;
  197. long lTemp;
  198. double dTemp;
  199. BSTR sTemp;
  200. IUnknown *pUnk = NULL;
  201. switch(vt)
  202. {
  203. case VT_BOOL:
  204. SafeArrayGetElement(psaArray, &iPos, &bTemp);
  205. vTemp.vt = (unsigned short)vt;
  206. vTemp.boolVal = (BOOL)bTemp;
  207. break;
  208. case VT_UI1:
  209. case VT_I1:
  210. SafeArrayGetElement(psaArray, &iPos, &lTemp);
  211. vTemp.vt = VT_UI1;
  212. vTemp.bVal = (BYTE)lTemp;
  213. break;
  214. case VT_UI2:
  215. case VT_I2:
  216. SafeArrayGetElement(psaArray, &iPos, &lTemp);
  217. vTemp.vt = VT_I2;
  218. vTemp.iVal = (int)lTemp;
  219. case VT_UI4:
  220. case VT_I4:
  221. SafeArrayGetElement(psaArray, &iPos, &lTemp);
  222. vTemp.vt = VT_I4;
  223. vTemp.lVal = lTemp;
  224. break;
  225. case VT_R4:
  226. SafeArrayGetElement(psaArray, &iPos, &dTemp);
  227. vTemp.vt = (unsigned short)vt;
  228. vTemp.fltVal = (float)dTemp;
  229. break;
  230. case VT_UI8:
  231. case VT_I8:
  232. case VT_R8:
  233. SafeArrayGetElement(psaArray, &iPos, &dTemp);
  234. vTemp.vt = VT_R8;
  235. vTemp.dblVal = dTemp;
  236. break;
  237. case VT_BSTR:
  238. SafeArrayGetElement(psaArray, &iPos, &sTemp);
  239. vTemp.vt = (unsigned short)vt;
  240. vTemp.bstrVal = sTemp;
  241. break;
  242. case VT_UNKNOWN:
  243. SafeArrayGetElement(psaArray, &iPos, &pUnk);
  244. vTemp.vt = (unsigned short)vt;
  245. V_UNKNOWN(&vTemp) = pUnk;
  246. break;
  247. default:
  248. hr = E_NOTIMPL;
  249. break;
  250. }
  251. return hr;
  252. }
  253. //***************************************************************************
  254. void GetByteBuffer (VARIANT *pValue, BYTE **ppInBuff, DWORD &dwLen)
  255. {
  256. long lUBound;
  257. dwLen = 0;
  258. SAFEARRAY *psaArray = V_ARRAY(pValue);
  259. if (psaArray)
  260. {
  261. HRESULT hr = SafeArrayGetUBound(psaArray, 1, &lUBound);
  262. lUBound += 1;
  263. *ppInBuff = new BYTE[lUBound];
  264. BYTE *pCurr = *ppInBuff;
  265. if (pCurr)
  266. {
  267. for (long i = 0; i < lUBound; i++)
  268. {
  269. BYTE byte;
  270. hr = SafeArrayGetElement(psaArray, &i, &byte);
  271. if (FAILED(hr))
  272. break;
  273. //if (!byte)
  274. // break;
  275. *pCurr = byte;
  276. pCurr++;
  277. dwLen++;
  278. }
  279. }
  280. }
  281. }
  282. //***************************************************************************
  283. DWORD GetMaxByte (DWORD One, DWORD Two)
  284. {
  285. DWORD dwRet = 0;
  286. if (One > Two)
  287. dwRet = One;
  288. else
  289. dwRet = Two;
  290. return dwRet;
  291. }
  292. //***************************************************************************
  293. DWORD GetMaxBytes(DWORD One, DWORD Two)
  294. {
  295. DWORD dwRet = 0;
  296. dwRet |= GetMaxByte(One&0xF, Two&0xF);
  297. dwRet |= GetMaxByte(One&0xF0, Two&0xF0);
  298. dwRet |= GetMaxByte(One&0xF00, Two&0xF00);
  299. dwRet |= GetMaxByte(One&0xF000, Two&0xF000);
  300. dwRet |= GetMaxByte(One&0xF0000, Two&0xF0000);
  301. dwRet |= GetMaxByte(One&0xF00000, Two&0xF00000);
  302. dwRet |= GetMaxByte(One&0xF000000, Two&0xF000000);
  303. dwRet |= GetMaxByte(One&0xF0000000, Two&0xF0000000);
  304. return dwRet;
  305. }
  306. //***************************************************************************
  307. LPWSTR TruncateLongText(const wchar_t *pszData, long lMaxLen, bool &bChg, int iTruncLen, BOOL bAppend)
  308. {
  309. LPWSTR lpRet = NULL;
  310. bChg = false;
  311. if (!pszData)
  312. return Macro_CloneLPWSTR(L"");
  313. long lLen = wcslen(pszData);
  314. if (lLen <= lMaxLen)
  315. return Macro_CloneLPWSTR(pszData);
  316. wchar_t *wTemp = new wchar_t [iTruncLen+1];
  317. if (wTemp)
  318. {
  319. if (bAppend)
  320. {
  321. wcsncpy(wTemp, pszData, iTruncLen-3);
  322. wTemp[iTruncLen-3] = L'\0';
  323. wcscat(wTemp, L"...\0");
  324. }
  325. else
  326. {
  327. wcsncpy(wTemp, pszData, iTruncLen);
  328. wTemp[iTruncLen] = L'\0';
  329. }
  330. bChg = true;
  331. lpRet = wTemp;
  332. }
  333. return lpRet;
  334. }
  335. BOOL IsTruncated(LPCWSTR lpData, int iCompLen)
  336. {
  337. BOOL bRet = FALSE;
  338. int iLen = wcslen(lpData);
  339. if (iLen == iCompLen)
  340. {
  341. if (lpData[iCompLen-1] == L'.' &&
  342. lpData[iCompLen-2] == L'.' &&
  343. lpData[iCompLen-3] == L'.')
  344. bRet = TRUE;
  345. }
  346. return bRet;
  347. }
  348. //***************************************************************************
  349. HRESULT PutVariantInArray (SAFEARRAY **ppsaArray, long iPos, VARIANT *vTemp)
  350. {
  351. HRESULT hr = 0;
  352. long lTemp;
  353. double dTemp;
  354. BOOL bTemp;
  355. BSTR sTemp;
  356. SAFEARRAY *psaArray = *ppsaArray;
  357. IUnknown *pTemp = NULL;
  358. long why[1];
  359. why[0] = iPos;
  360. switch(vTemp->vt & 0xFF)
  361. {
  362. case VT_BOOL:
  363. bTemp = vTemp->boolVal;
  364. hr = SafeArrayPutElement(psaArray, why, &bTemp);
  365. break;
  366. case VT_I1:
  367. case VT_UI1:
  368. lTemp = vTemp->bVal;
  369. hr = SafeArrayPutElement(psaArray, why, &lTemp);
  370. break;
  371. case VT_I2:
  372. case VT_UI2:
  373. lTemp = vTemp->iVal;
  374. hr = SafeArrayPutElement(psaArray, why, &lTemp);
  375. break;
  376. case VT_I4:
  377. case VT_UI4:
  378. lTemp = vTemp->lVal;
  379. hr = SafeArrayPutElement(psaArray, why, &lTemp);
  380. break;
  381. case VT_R4:
  382. dTemp = vTemp->fltVal;
  383. hr = SafeArrayPutElement(psaArray, why, &dTemp);
  384. break;
  385. case VT_I8:
  386. case VT_UI8:
  387. case VT_R8:
  388. dTemp = vTemp->dblVal;
  389. hr = SafeArrayPutElement(psaArray, why, &dTemp);
  390. break;
  391. case VT_BSTR:
  392. sTemp = vTemp->bstrVal;
  393. hr = SafeArrayPutElement(psaArray, why, sTemp);
  394. break;
  395. case VT_UNKNOWN:
  396. pTemp = V_UNKNOWN(vTemp);
  397. hr = SafeArrayPutElement(psaArray, why, pTemp);
  398. break;
  399. default:
  400. hr = E_NOTIMPL;
  401. break;
  402. }
  403. return hr;
  404. }
  405. LPWSTR GetOperator (DWORD dwOp)
  406. {
  407. LPWSTR lpRet = new wchar_t [15];
  408. if (lpRet)
  409. {
  410. switch (dwOp)
  411. {
  412. case WQL_TOK_LE:
  413. wcscpy(lpRet, L" <= ");
  414. break;
  415. case WQL_TOK_LT:
  416. wcscpy(lpRet, L" < ");
  417. break;
  418. case WQL_TOK_GE:
  419. wcscpy(lpRet, L" >= ");
  420. break;
  421. case WQL_TOK_GT:
  422. wcscpy(lpRet, L" > ");
  423. break;
  424. case WQL_TOK_EQ:
  425. wcscpy(lpRet, L" = ");
  426. break;
  427. case WQL_TOK_NE:
  428. wcscpy(lpRet, L" <> ");
  429. break;
  430. case WQL_TOK_LIKE:
  431. wcscpy(lpRet, L" like ");
  432. break;
  433. case WQL_TOK_NOT_LIKE:
  434. wcscpy(lpRet, L" not like ");
  435. break;
  436. case WQL_TOK_IS:
  437. wcscpy(lpRet, L" = ");
  438. break;
  439. case WQL_TOK_BEFORE:
  440. wcscpy(lpRet, L" < ");
  441. break;
  442. case WQL_TOK_AFTER:
  443. wcscpy(lpRet, L" > ");
  444. break;
  445. case WQL_TOK_NOT_BEFORE:
  446. wcscpy(lpRet, L" >= ");
  447. break;
  448. case WQL_TOK_NOT_AFTER:
  449. wcscpy(lpRet, L" <= ");
  450. break;
  451. default:
  452. wcscpy(lpRet, L"");
  453. }
  454. }
  455. return lpRet;
  456. }
  457. LPWSTR StripQuotes(LPWSTR lpText, WCHAR tIn)
  458. {
  459. wchar_t *pszTemp = new wchar_t [wcslen(lpText)*2+1];
  460. if (pszTemp)
  461. {
  462. int iPos = 0;
  463. int iLen = wcslen(lpText);
  464. if (iLen)
  465. {
  466. for (int i = 0; i < iLen; i++)
  467. {
  468. WCHAR t = lpText[i];
  469. if (t == tIn)
  470. {
  471. pszTemp[iPos] = t;
  472. iPos++;
  473. }
  474. pszTemp[iPos] = t;
  475. iPos++;
  476. }
  477. }
  478. pszTemp[iPos] = '\0';
  479. }
  480. return pszTemp;
  481. }
  482. //***************************************************************************
  483. //
  484. // GENUTILS so we don't have to build the entire file
  485. //
  486. //***************************************************************************
  487. POLARITY BOOL IsNT(void)
  488. {
  489. OSVERSIONINFO os;
  490. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  491. if(!GetVersionEx(&os))
  492. return FALSE; // should never happen
  493. return os.dwPlatformId == VER_PLATFORM_WIN32_NT;
  494. }
  495. POLARITY BOOL IsWinMgmt(void)
  496. {
  497. //
  498. // Retrieve EXE path
  499. //
  500. static BOOL bRet = FALSE;
  501. static BOOL bInit = FALSE;
  502. if (!bInit)
  503. {
  504. WCHAR wszExePath[MAX_PATH+1];
  505. if(GetModuleFileNameW(NULL, wszExePath, MAX_PATH+1) == 0)
  506. return FALSE;
  507. //
  508. // Extract the file-name portion
  509. //
  510. WCHAR* pwcFileName = wcsrchr(wszExePath, L'\\');
  511. if(pwcFileName == NULL)
  512. pwcFileName = wszExePath;
  513. else
  514. pwcFileName++;
  515. if(_wcsnicmp(pwcFileName, FILENAME_PREFIX_EXE_W, wcslen(FILENAME_PREFIX_EXE_W)))
  516. bRet = FALSE;
  517. else
  518. bRet = TRUE;
  519. bInit = TRUE;
  520. }
  521. return bRet;
  522. }
  523. POLARITY BOOL VariantCompare(VARIANT *vComp1, VARIANT *vComp2)
  524. {
  525. IWbemClassObject *pTemp = NULL;
  526. VARIANT vTemp1, vTemp2;
  527. BOOL bRet = FALSE;
  528. if (vComp1->vt != vComp2->vt)
  529. bRet = FALSE;
  530. else
  531. {
  532. switch(vComp1->vt)
  533. {
  534. case VT_I1:
  535. bRet = (vComp1->cVal == vComp2->cVal) ? TRUE : FALSE;
  536. break;
  537. case VT_UI1:
  538. bRet = (vComp1->bVal == vComp2->bVal) ? TRUE : FALSE;
  539. break;
  540. case VT_I2:
  541. bRet = (vComp1->iVal == vComp2->iVal) ? TRUE : FALSE;
  542. break;
  543. case VT_I4:
  544. bRet = (vComp1->lVal == vComp2->lVal) ? TRUE : FALSE;
  545. break;
  546. case VT_BOOL:
  547. bRet = (vComp1->boolVal == vComp2->boolVal) ? TRUE : FALSE;
  548. break;
  549. case VT_R4:
  550. bRet = (vComp1->fltVal == vComp2->fltVal) ? TRUE : FALSE;
  551. break;
  552. case VT_R8:
  553. bRet = (vComp1->dblVal == vComp2->dblVal) ? TRUE : FALSE;
  554. break;
  555. case VT_NULL:
  556. case VT_EMPTY:
  557. bRet = TRUE;
  558. break;
  559. case VT_BSTR:
  560. bRet = (!_wcsicmp(vComp1->bstrVal, vComp2->bstrVal)) ? TRUE : FALSE;
  561. break;
  562. case VT_UNKNOWN:
  563. VariantInit(&vTemp1), VariantInit(&vTemp2);
  564. bRet = FALSE;
  565. pTemp = (IWbemClassObject *)V_UNKNOWN(vComp1);
  566. if (pTemp)
  567. {
  568. pTemp->Get(L"__RelPath", 0, &vTemp1, NULL, NULL);
  569. pTemp = (IWbemClassObject *)V_UNKNOWN(vComp2);
  570. if (pTemp)
  571. {
  572. pTemp->Get(L"__RelPath", 0, &vTemp2, NULL, NULL);
  573. bRet = (!_wcsicmp(vTemp1.bstrVal, vTemp2.bstrVal)) ? TRUE : FALSE;
  574. }
  575. }
  576. VariantClear(&vTemp1), VariantClear(&vTemp2);
  577. break;
  578. case VT_UI1|VT_ARRAY:
  579. bRet = FALSE; // not supported
  580. break;
  581. }
  582. }
  583. bRet = (bRet == TRUE ? FALSE: TRUE);
  584. return bRet;
  585. }
  586. char * GetAnsiString (wchar_t *pStr)
  587. {
  588. char * pRet = new char [(wcslen(pStr)*2)+2];
  589. if (pRet)
  590. {
  591. sprintf(pRet, "%S", pStr);
  592. }
  593. return pRet;
  594. }