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.

1501 lines
33 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. //***************************************************************************
  6. #include "precomp.h"
  7. #include "wmicom.h"
  8. #include "wmimap.h"
  9. #include <stdlib.h>
  10. #include <CWbemTime.h>
  11. /*
  12. These are the types we support
  13. CIM_SINT8 = 16,
  14. CIM_UINT8 = 17,
  15. CIM_SINT16 = 2,
  16. CIM_UINT16 = 18,
  17. CIM_SINT32 = 3,
  18. CIM_UINT32 = 19,
  19. CIM_SINT64 = 20,
  20. CIM_UINT64 = 21,
  21. CIM_REAL32 = 4,
  22. CIM_REAL64 = 5,
  23. CIM_BOOLEAN = 11,
  24. CIM_STRING = 8,
  25. CIM_DATETIME = 101,
  26. CIM_REFERENCE = 102,
  27. CIM_CHAR16 = 103,
  28. CIM_OBJECT = 13,
  29. CIM_FLAG_ARRAY = 0x2000
  30. */
  31. BOOL ValidateDateTime(WCHAR * wcsValue)
  32. {
  33. BOOL fRc = FALSE;
  34. // Pre-test
  35. // ========
  36. CAnsiUnicode XLate;
  37. char * psz = NULL;
  38. XLate.UnicodeToAnsi(wcsValue,psz);
  39. if( psz )
  40. {
  41. if(strlen(psz) == 25){
  42. if(psz[14] != '.' && psz[14] != '*'){
  43. goto ExitValidateTime;
  44. }
  45. if(psz[21] != ':' && psz[21] != '-' && psz[21] != '+' && psz[21] != '*'){
  46. goto ExitValidateTime;
  47. }
  48. for(int i = 0; i < 25; i++){
  49. if(i == 21 || i == 14)
  50. continue;
  51. if(psz[i] != '*' && !isdigit(psz[i])){
  52. goto ExitValidateTime;
  53. }
  54. }
  55. // Passed pre-test. Check if any stars were specified
  56. // ==================================================
  57. if(strchr(psz, '*')) {
  58. // No further checking
  59. fRc = TRUE;
  60. goto ExitValidateTime;
  61. }
  62. if(psz[21] == ':'){
  63. // Interval -- no checking
  64. fRc = TRUE;
  65. goto ExitValidateTime;
  66. }
  67. WCHAR wcsTmp[SIZEOFWBEMDATETIME+2];
  68. wcscpy(wcsTmp,wcsValue);
  69. CWbemTime Time;
  70. fRc = Time.SetDMTF(wcsTmp);
  71. }
  72. }
  73. ExitValidateTime:
  74. SAFE_DELETE_ARRAY( psz );
  75. return fRc;
  76. }
  77. //=============================================================
  78. BOOL CWMIDataTypeMap::ConvertDWORDToWMIDataTypeAndWriteItToBlock(DWORD dwLong, int nSize )
  79. {
  80. BOOL fRc = FALSE;
  81. if( NaturallyAlignData( nSize, WRITE_IT)){
  82. if( nSize == sizeof(BYTE) ){
  83. m_pWMIBlockInfo->SetByte((BYTE)dwLong);
  84. }
  85. else if( nSize == sizeof(short)){
  86. m_pWMIBlockInfo->SetWord((WORD)dwLong);
  87. }
  88. else{
  89. m_pWMIBlockInfo->SetDWORD(dwLong);
  90. }
  91. fRc = TRUE;
  92. }
  93. return fRc;
  94. }
  95. //=============================================================
  96. DWORD CWMIDataTypeMap::ConvertWMIDataTypeToDWORD(int nSize)
  97. {
  98. DWORD dwLong = 0L;
  99. if( nSize == sizeof(BYTE) ){
  100. BYTE bByte;
  101. m_pWMIBlockInfo->GetByte(bByte);
  102. dwLong = (DWORD) bByte;
  103. }
  104. else if( nSize == sizeof(short)){
  105. WORD nInt;
  106. m_pWMIBlockInfo->GetWord(nInt);
  107. dwLong = (DWORD) nInt;
  108. }
  109. else{
  110. m_pWMIBlockInfo->GetDWORD(dwLong);
  111. }
  112. return dwLong;
  113. }
  114. //=============================================================
  115. BOOL CWMIDataTypeMap::SetDefaultMissingQualifierValue( CVARIANT & v, long lType, CVARIANT & vToSet )
  116. {
  117. BOOL fRc = TRUE;
  118. CAnsiUnicode XLate;
  119. switch (lType){
  120. // CIM_SINT16 = 2,
  121. // CIM_CHAR16
  122. case CIM_CHAR16:
  123. break;
  124. // CIM_SINT8 = 16,
  125. case VT_I1:
  126. case VT_I2:
  127. vToSet.Clear();
  128. vToSet.SetShort(v.GetShort());
  129. break;
  130. // CIM_SINT32 = 3,
  131. // CIM_UINT32 = 19,
  132. // CIM_UINT16 = 18,
  133. case VT_UI2:
  134. case VT_I4:
  135. case VT_UI4:
  136. vToSet.Clear();
  137. vToSet.SetLONG(v.GetLONG());
  138. break;
  139. // CIM_REAL32 = 4,
  140. case VT_R4:
  141. case VT_R8:
  142. vToSet.Clear();
  143. vToSet.SetDouble(v.GetDouble());
  144. break;
  145. // CIM_SINT64 = 20,
  146. // CIM_UINT64 = 21,
  147. case VT_I8:
  148. case VT_UI8:
  149. vToSet.Clear();
  150. vToSet.SetStr(v.GetStr());
  151. break;
  152. // CIM_DATETIME = 101,
  153. case CIM_DATETIME:
  154. break;
  155. // CIM_STRING = 8,
  156. // CIM_REFERENCE = 102,
  157. // CIM_OBJECT = 13,
  158. // CIM_FLAG_ARRAY = 0x2000
  159. case VT_BSTR:
  160. break;
  161. // CIM_BOOLEAN = 11,
  162. case VT_BOOL:
  163. vToSet.Clear();
  164. vToSet.SetBool(v.GetBool());
  165. break;
  166. // CIM_UINT8 = 17,
  167. case VT_UI1:
  168. vToSet.Clear();
  169. vToSet.SetByte(v.GetByte());
  170. break;
  171. default:
  172. fRc = FALSE;
  173. }
  174. return fRc;
  175. }
  176. //=============================================================
  177. BOOL CWMIDataTypeMap::MissingQualifierValueMatches( CSAFEARRAY * pSafe, long i, CVARIANT & v, long lType, CVARIANT & vToCompare )
  178. {
  179. BOOL fRc = FALSE;
  180. CAnsiUnicode XLate;
  181. switch (lType){
  182. // CIM_SINT16 = 2,
  183. // CIM_CHAR16
  184. case CIM_CHAR16:
  185. break;
  186. // CIM_SINT8 = 16,
  187. case VT_I1:
  188. case VT_I2:
  189. {
  190. short v1 = 0;
  191. if( pSafe )
  192. {
  193. if( S_OK != pSafe->Get(i, &v1))
  194. {
  195. return FALSE;
  196. }
  197. }
  198. else
  199. {
  200. v1 = (short) v.GetShort();
  201. }
  202. short v2 = (short)vToCompare.GetShort();
  203. if( v1 == v2){
  204. fRc = TRUE;
  205. }
  206. break;
  207. }
  208. // CIM_SINT32 = 3,
  209. // CIM_UINT32 = 19,
  210. // CIM_UINT16 = 18,
  211. case VT_UI2:
  212. case VT_I4:
  213. case VT_UI4:
  214. {
  215. long v1 = 0;
  216. if( pSafe )
  217. {
  218. if( S_OK != pSafe->Get(i, &v1))
  219. {
  220. return FALSE;
  221. }
  222. }
  223. else
  224. {
  225. v1 = (long)v.GetLONG();
  226. }
  227. long v2 = (long)vToCompare.GetLONG();
  228. if( v1 == v2){
  229. fRc = TRUE;
  230. }
  231. break;
  232. }
  233. // CIM_REAL32 = 4,
  234. case VT_R4:
  235. case VT_R8:
  236. {
  237. double v1 = 0;
  238. if( pSafe )
  239. {
  240. if( S_OK != pSafe->Get(i, &v1))
  241. {
  242. return FALSE;
  243. }
  244. }
  245. else
  246. {
  247. v1 = (double)v.GetDouble();
  248. }
  249. double v2 = (double)vToCompare.GetDouble();
  250. if( v1 == v2 ){
  251. fRc = TRUE;
  252. }
  253. break;
  254. }
  255. // CIM_SINT64 = 20,
  256. // CIM_UINT64 = 21,
  257. case VT_UI8:
  258. {
  259. unsigned __int64 I1 = 0L, I2 = 0L;
  260. char * pChar1 = NULL, *pChar2 =NULL;
  261. CBSTR bstr2;
  262. CBSTR bstr1;
  263. if( pSafe )
  264. {
  265. if( S_OK != pSafe->Get(i, &bstr1))
  266. {
  267. return FALSE;
  268. }
  269. }
  270. else
  271. {
  272. bstr1.SetStr(v.GetStr());
  273. }
  274. bstr2.SetStr(vToCompare.GetStr());
  275. if( bstr1 && bstr2)
  276. {
  277. if( SUCCEEDED(XLate.UnicodeToAnsi(bstr1, pChar1 ) && XLate.UnicodeToAnsi(bstr2,pChar2)))
  278. {
  279. I1 =(unsigned __int64) _atoi64(pChar1);
  280. I2 =(unsigned __int64)_atoi64(pChar2);
  281. if( I1 == I2 ){
  282. fRc = TRUE;
  283. }
  284. }
  285. SAFE_DELETE_ARRAY( pChar1 );
  286. SAFE_DELETE_ARRAY( pChar2 );
  287. }
  288. break;
  289. }
  290. case VT_I8:
  291. {
  292. __int64 I1 = 0L, I2 = 0L;
  293. char * pChar1 = NULL, * pChar2 =NULL;
  294. CBSTR bstr2;
  295. CBSTR bstr1;
  296. if( pSafe )
  297. {
  298. if( S_OK != pSafe->Get(i, &bstr1))
  299. {
  300. return FALSE;
  301. }
  302. }
  303. else
  304. {
  305. bstr1.SetStr(v.GetStr());
  306. }
  307. bstr2.SetStr(vToCompare.GetStr());
  308. if( bstr1 && bstr2)
  309. {
  310. if( SUCCEEDED(XLate.UnicodeToAnsi(bstr1, pChar1 ) && XLate.UnicodeToAnsi(bstr2,pChar2)))
  311. {
  312. I1 = _atoi64(pChar1);
  313. I2 = _atoi64(pChar2);
  314. if( I1 == I2 ){
  315. fRc = TRUE;
  316. }
  317. }
  318. SAFE_DELETE_ARRAY( pChar1 );
  319. SAFE_DELETE_ARRAY( pChar2 );
  320. }
  321. break;
  322. }
  323. // CIM_DATETIME = 101,
  324. case CIM_DATETIME:
  325. break;
  326. // CIM_STRING = 8,
  327. // CIM_REFERENCE = 102,
  328. // CIM_OBJECT = 13,
  329. // CIM_FLAG_ARRAY = 0x2000
  330. case VT_BSTR:
  331. break;
  332. // CIM_BOOLEAN = 11,
  333. case VT_BOOL:
  334. {
  335. BOOL v1 = 0;
  336. if( pSafe )
  337. {
  338. if( S_OK != pSafe->Get(i, &v1))
  339. {
  340. return FALSE;
  341. }
  342. }
  343. else
  344. {
  345. v1 = (BOOL)v.GetBool();
  346. }
  347. BOOL v2 = (BOOL)vToCompare.GetBool();
  348. if( v1 == v2 )
  349. {
  350. fRc = TRUE;
  351. }
  352. break;
  353. }
  354. // CIM_UINT8 = 17,
  355. case VT_UI1:
  356. {
  357. BYTE v1 = 0;
  358. if( pSafe )
  359. {
  360. if( S_OK != pSafe->Get(i, &v1))
  361. {
  362. return FALSE;
  363. }
  364. }
  365. else
  366. {
  367. v1 = (BYTE) v.GetByte();
  368. }
  369. BYTE v2 = (BYTE)vToCompare.GetByte();
  370. if( v1 == v2){
  371. fRc = TRUE;
  372. }
  373. break;
  374. }
  375. default:
  376. fRc = FALSE;
  377. }
  378. return fRc;
  379. }
  380. //=============================================================
  381. DWORD CWMIDataTypeMap::ArraySize(long lType,CVARIANT & var)
  382. {
  383. DWORD dwCount = 0;
  384. switch( lType ){
  385. case VT_I2:
  386. case VT_UI2:
  387. dwCount = (DWORD) var.GetShort();
  388. break;
  389. case VT_I4:
  390. case VT_UI4:
  391. case VT_R4:
  392. dwCount = (DWORD) var.GetLONG();
  393. break;
  394. case VT_UI1:
  395. case VT_I1:
  396. dwCount = (DWORD) var.GetByte();
  397. break;
  398. }
  399. return dwCount;
  400. }
  401. //=============================================================
  402. BOOL CWMIDataTypeMap::SetDataInDataBlock(CSAFEARRAY * pSafe, int i, CVARIANT & v, long lType, int nSize)
  403. {
  404. BOOL fRc = TRUE;
  405. CAnsiUnicode XLate;
  406. switch (lType){
  407. // CIM_SINT16 = 2,
  408. // CIM_CHAR16
  409. case CIM_CHAR16:
  410. case VT_I2:
  411. {
  412. SHORT iShort;
  413. if( pSafe ){
  414. if( S_OK != pSafe->Get(i, &iShort)){
  415. return FALSE;
  416. }
  417. }
  418. else{
  419. iShort = v.GetShort();
  420. }
  421. if( NaturallyAlignData( sizeof(WORD), WRITE_IT)){
  422. m_pWMIBlockInfo->SetWord(iShort);
  423. }
  424. break;
  425. }
  426. // CIM_SINT32 = 3,
  427. // CIM_UINT32 = 19,
  428. // CIM_UINT16 = 18,
  429. case VT_UI2:
  430. case VT_I4:
  431. case VT_UI4:
  432. {
  433. DWORD dwLong = 0L;
  434. if( pSafe ){
  435. if( S_OK != pSafe->Get(i, &dwLong)){
  436. return FALSE;
  437. }
  438. }
  439. else{
  440. dwLong = v.GetLONG();
  441. }
  442. ConvertDWORDToWMIDataTypeAndWriteItToBlock(dwLong,nSize);
  443. break;
  444. }
  445. // CIM_REAL32 = 4,
  446. case VT_R4:
  447. {
  448. float fFloat;
  449. if( pSafe ){
  450. if( S_OK != pSafe->Get(i, &fFloat)){
  451. return FALSE;
  452. }
  453. }
  454. else{
  455. fFloat =(float) v.GetDouble();
  456. }
  457. if( NaturallyAlignData( sizeof(WORD), WRITE_IT )){
  458. m_pWMIBlockInfo->SetFloat(fFloat);
  459. }
  460. break;
  461. }
  462. // CIM_REAL64 = 5,
  463. case VT_R8:
  464. {
  465. DOUBLE dDouble;
  466. if( pSafe ){
  467. if( S_OK != pSafe->Get(i, &dDouble)){
  468. return FALSE;
  469. }
  470. }
  471. else{
  472. dDouble = v.GetDouble();
  473. }
  474. if( NaturallyAlignData( sizeof(DOUBLE),WRITE_IT)){
  475. m_pWMIBlockInfo->SetDouble(dDouble);
  476. }
  477. break;
  478. }
  479. // CIM_SINT64 = 20,
  480. case VT_I8:
  481. {
  482. __int64 Int64 = 0L;
  483. char * pChar = NULL;
  484. CBSTR bstr;
  485. if( pSafe )
  486. {
  487. if( S_OK != pSafe->Get(i, &bstr))
  488. {
  489. return FALSE;
  490. }
  491. }
  492. else
  493. {
  494. bstr.SetStr(v.GetStr());
  495. }
  496. if( bstr )
  497. {
  498. if( SUCCEEDED(XLate.UnicodeToAnsi(bstr, pChar )))
  499. {
  500. Int64 = _atoi64(pChar);
  501. SAFE_DELETE_ARRAY(pChar);
  502. }
  503. }
  504. if( NaturallyAlignData( sizeof(__int64), WRITE_IT ))
  505. {
  506. m_pWMIBlockInfo->SetSInt64(Int64);
  507. }
  508. break;
  509. }
  510. // CIM_UINT64 = 21,
  511. case VT_UI8:
  512. {
  513. unsigned __int64 Int64 = 0L;
  514. char * pChar = NULL;
  515. CBSTR bstr;
  516. if( pSafe )
  517. {
  518. if( S_OK != pSafe->Get(i, &bstr))
  519. {
  520. return FALSE;
  521. }
  522. }
  523. else
  524. {
  525. bstr.SetStr(v.GetStr());
  526. }
  527. if( bstr )
  528. {
  529. if( SUCCEEDED(XLate.UnicodeToAnsi(bstr, pChar )))
  530. {
  531. Int64 = (unsigned __int64) _atoi64(pChar);
  532. SAFE_DELETE_ARRAY(pChar);
  533. }
  534. }
  535. if( NaturallyAlignData( sizeof(unsigned __int64),WRITE_IT))
  536. {
  537. m_pWMIBlockInfo->SetUInt64(Int64);
  538. }
  539. break;
  540. }
  541. // CIM_DATETIME = 101,
  542. case CIM_DATETIME:
  543. {
  544. WORD wCount=0;
  545. CBSTR bstr;
  546. WCHAR wDateTime[SIZEOFWBEMDATETIME+2];
  547. memset( wDateTime,NULL,SIZEOFWBEMDATETIME+2 );
  548. BOOL fContinue = TRUE;
  549. if( pSafe )
  550. {
  551. if( S_OK != pSafe->Get(i, &bstr))
  552. {
  553. return FALSE;
  554. }
  555. }
  556. else
  557. {
  558. bstr.SetStr(v.GetStr());
  559. }
  560. //=========================================================
  561. // Initialize buffer
  562. //=========================================================
  563. if( bstr != NULL )
  564. {
  565. if( ValidateDateTime(bstr))
  566. {
  567. wcscpy(wDateTime,v.GetStr());
  568. }
  569. else
  570. {
  571. fContinue = FALSE;
  572. }
  573. }
  574. else{
  575. wcscpy(wDateTime,L"00000000000000.000000:000");
  576. }
  577. if( fContinue ){
  578. if( S_OK != m_pWMIBlockInfo->GetBufferReady(SIZEOFWBEMDATETIME )){
  579. return FALSE;
  580. }
  581. if( NaturallyAlignData( SIZEOFWBEMDATETIME, WRITE_IT)){
  582. m_pWMIBlockInfo->SetString(wDateTime,SIZEOFWBEMDATETIME);
  583. }
  584. }
  585. break;
  586. }
  587. // CIM_STRING = 8,
  588. // CIM_REFERENCE = 102,
  589. // CIM_OBJECT = 13,
  590. // CIM_FLAG_ARRAY = 0x2000
  591. case VT_BSTR:
  592. {
  593. WORD wCount=0;
  594. CBSTR bstr;
  595. if( pSafe )
  596. {
  597. if( S_OK != pSafe->Get(i, &bstr))
  598. {
  599. return FALSE;
  600. }
  601. }
  602. else{
  603. bstr.SetStr(v.GetStr());
  604. }
  605. if( bstr != NULL )
  606. {
  607. wCount = (wcslen(bstr))* sizeof(WCHAR);
  608. }
  609. if( S_OK != m_pWMIBlockInfo->GetBufferReady(wCount)){
  610. return FALSE;
  611. }
  612. if( NaturallyAlignData( sizeof(WORD),WRITE_IT)){
  613. m_pWMIBlockInfo->SetWord(wCount);
  614. }
  615. if( bstr )
  616. {
  617. m_pWMIBlockInfo->SetString(bstr,wCount);
  618. *m_pdwAccumulativeSizeOfBlock += wCount;
  619. }
  620. break;
  621. }
  622. // CIM_BOOLEAN = 11,
  623. case VT_BOOL:
  624. {
  625. BYTE bByte;
  626. if( pSafe ){
  627. BOOL bTmp;
  628. if( S_OK != pSafe->Get(i, &bTmp)){
  629. return FALSE;
  630. }
  631. bByte = (BYTE) bTmp;
  632. }
  633. else{
  634. bByte =(BYTE) v.GetBool();
  635. }
  636. if( NaturallyAlignData( sizeof(BYTE), WRITE_IT )){
  637. m_pWMIBlockInfo->SetByte(bByte);
  638. }
  639. break;
  640. }
  641. // CIM_UINT8 = 17,
  642. case VT_UI1:
  643. {
  644. BYTE bByte;
  645. if( pSafe ){
  646. if( S_OK != pSafe->Get(i, &bByte)){
  647. return FALSE;
  648. }
  649. }
  650. else{
  651. bByte = v.GetByte();
  652. }
  653. if( NaturallyAlignData( 1, WRITE_IT )){
  654. m_pWMIBlockInfo->SetByte(bByte);
  655. }
  656. break;
  657. }
  658. // CIM_SINT8 = 16,
  659. case VT_I1:
  660. {
  661. short tmpShort;
  662. if( pSafe ){
  663. if( S_OK != pSafe->Get(i, &tmpShort)){
  664. return FALSE;
  665. }
  666. }
  667. else{
  668. tmpShort = v.GetShort();
  669. }
  670. if( NaturallyAlignData( 1, WRITE_IT )){
  671. BYTE bByte = (signed char)tmpShort;
  672. m_pWMIBlockInfo->SetByte(bByte);
  673. }
  674. break;
  675. }
  676. default:
  677. fRc = FALSE;
  678. }
  679. return fRc;
  680. }
  681. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  682. HRESULT CWMIDataTypeMap::GetDataFromDataBlock(IWbemObjectAccess * p, long lHandle, long lType, int nSize)
  683. {
  684. HRESULT hr = S_OK;
  685. switch (lType){
  686. // CIM_SINT32 = 3,
  687. // CIM_UINT32 = 19,
  688. case VT_I4:
  689. case VT_UI4:
  690. if( NaturallyAlignData( nSize, READ_IT )){
  691. hr = p->WriteDWORD(lHandle,ConvertWMIDataTypeToDWORD(nSize));
  692. }
  693. break;
  694. case VT_I8:
  695. case VT_UI8:
  696. if( NaturallyAlignData( nSize, READ_IT ))
  697. {
  698. unsigned __int64 uInt64;
  699. m_pWMIBlockInfo->GetQWORD(uInt64);
  700. hr = p->WriteQWORD(lHandle,uInt64);
  701. }
  702. break;
  703. // CIM_UINT16 = 18,
  704. // CIM_SINT16 = 2,
  705. // CIM_CHAR16 = 103,
  706. case VT_I2:
  707. case VT_UI2:
  708. case CIM_CHAR16:
  709. if( NaturallyAlignData( nSize, READ_IT )){
  710. WORD wWord;
  711. m_pWMIBlockInfo->GetWord(wWord);
  712. // Read but don't assign to anything
  713. }
  714. break;
  715. // CIM_REAL32 = 4,
  716. case VT_R4:
  717. if( NaturallyAlignData( nSize, READ_IT)){
  718. float fFloat;
  719. m_pWMIBlockInfo->GetFloat(fFloat);
  720. // Read but don't assign to anything
  721. }
  722. break;
  723. // CIM_REAL64 = 5,
  724. case VT_R8:
  725. if( NaturallyAlignData( nSize, READ_IT )){
  726. DOUBLE dDouble;
  727. m_pWMIBlockInfo->GetDouble(dDouble);
  728. // Read but don't assign to anything
  729. }
  730. break;
  731. // CIM_DATETIME = 101, which is 25 WCHARS
  732. case CIM_DATETIME:
  733. if( NaturallyAlignData( SIZEOFWBEMDATETIME, READ_IT )){
  734. WORD wSize = SIZEOFWBEMDATETIME + 4;
  735. WCHAR Buffer[SIZEOFWBEMDATETIME + 4];
  736. m_pWMIBlockInfo->GetString(Buffer,SIZEOFWBEMDATETIME,wSize);
  737. // Read but don't assign to anything
  738. }
  739. break;
  740. // CIM_REFERENCE = 102,
  741. // CIM_STRING = 8,
  742. case VT_BSTR:
  743. if( NaturallyAlignData( 2, READ_IT )){
  744. WORD wCount=0;
  745. WCHAR * pBuffer=NULL;
  746. // Get the size of the string
  747. m_pWMIBlockInfo->GetWord(wCount);
  748. if( wCount > 0 )
  749. {
  750. if( m_pWMIBlockInfo->CurrentPtrOk((ULONG)(wCount)) )
  751. {
  752. WORD wSize = wCount + 4;
  753. pBuffer = new WCHAR[wSize];
  754. if( pBuffer )
  755. {
  756. try
  757. {
  758. m_pWMIBlockInfo->GetString(pBuffer,wCount,wSize);
  759. // Read but don't assign to anything
  760. SAFE_DELETE_ARRAY(pBuffer);
  761. *m_pdwAccumulativeSizeOfBlock += wCount;
  762. }
  763. catch(...)
  764. {
  765. hr = WBEM_E_UNEXPECTED;
  766. SAFE_DELETE_ARRAY(pBuffer);
  767. throw;
  768. }
  769. }
  770. }
  771. else
  772. {
  773. hr = WBEM_E_INVALID_OBJECT;
  774. }
  775. }
  776. }
  777. break;
  778. // CIM_BOOLEAN = 11,
  779. case VT_BOOL:
  780. if( NaturallyAlignData( nSize, READ_IT )){
  781. BYTE bByte=0;
  782. m_pWMIBlockInfo->GetByte(bByte);
  783. // Read but don't assign to anything
  784. }
  785. break;
  786. // CIM_SINT8 = 16,
  787. // CIM_UINT8 = 17,
  788. case VT_UI1:
  789. case VT_I1:
  790. if( NaturallyAlignData( nSize, READ_IT )){
  791. BYTE bByte=0;
  792. m_pWMIBlockInfo->GetByte(bByte);
  793. // Read but don't assign to anything
  794. }
  795. break;
  796. default:
  797. return WBEM_E_INVALID_OBJECT;
  798. }
  799. return hr;
  800. }
  801. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  802. HRESULT CWMIDataTypeMap::GetDataFromDataBlock(CVARIANT & v, long lType, int nSize )
  803. {
  804. HRESULT hr = WBEM_E_INVALID_OBJECT;
  805. switch (lType){
  806. // CIM_SINT16 = 2,
  807. // CIM_CHAR16 = 103,
  808. case VT_I2:
  809. case CIM_CHAR16:
  810. if( NaturallyAlignData( nSize, READ_IT )){
  811. hr = WBEM_S_NO_ERROR;
  812. WORD wWord;
  813. m_pWMIBlockInfo->GetWord(wWord);
  814. v.SetShort(wWord);
  815. }
  816. break;
  817. // CIM_SINT32 = 3,
  818. // CIM_UINT16 = 18,
  819. // CIM_UINT32 = 19,
  820. case VT_I4:
  821. case VT_UI2:
  822. case VT_UI4:
  823. if( NaturallyAlignData( nSize, READ_IT )){
  824. hr = WBEM_S_NO_ERROR;
  825. v.SetLONG(ConvertWMIDataTypeToDWORD(nSize));
  826. }
  827. break;
  828. // CIM_REAL32 = 4,
  829. case VT_R4:
  830. if( NaturallyAlignData( nSize, READ_IT)){
  831. hr = WBEM_S_NO_ERROR;
  832. float fFloat;
  833. m_pWMIBlockInfo->GetFloat(fFloat);
  834. v.SetDouble(fFloat);
  835. }
  836. break;
  837. // CIM_REAL64 = 5,
  838. case VT_R8:
  839. if( NaturallyAlignData( nSize, READ_IT )){
  840. hr = WBEM_S_NO_ERROR;
  841. DOUBLE dDouble;
  842. m_pWMIBlockInfo->GetDouble(dDouble);
  843. v.SetDouble(dDouble);
  844. }
  845. break;
  846. // CIM_SINT64 = 20,
  847. case VT_I8:
  848. if( NaturallyAlignData( nSize, READ_IT )){
  849. hr = WBEM_S_NO_ERROR;
  850. WCHAR pwcsBuffer[256];
  851. memset(pwcsBuffer,NULL,256);
  852. m_pWMIBlockInfo->GetSInt64(pwcsBuffer);
  853. v.SetStr(pwcsBuffer);
  854. }
  855. break;
  856. // CIM_UINT64 = 21,
  857. case VT_UI8:
  858. if( NaturallyAlignData( nSize, READ_IT )){
  859. hr = WBEM_S_NO_ERROR;
  860. WCHAR pwcsBuffer[256];
  861. memset(pwcsBuffer,NULL,256);
  862. m_pWMIBlockInfo->GetUInt64(pwcsBuffer);
  863. v.SetStr(pwcsBuffer);
  864. }
  865. break;
  866. // CIM_DATETIME = 101, which is 25 WCHARS
  867. case CIM_DATETIME:
  868. v.SetStr(NULL);
  869. if( NaturallyAlignData( SIZEOFWBEMDATETIME, READ_IT )){
  870. hr = WBEM_S_NO_ERROR;
  871. WORD wSize = SIZEOFWBEMDATETIME + 4;
  872. WCHAR Buffer[SIZEOFWBEMDATETIME + 4];
  873. m_pWMIBlockInfo->GetString(Buffer,SIZEOFWBEMDATETIME,wSize);
  874. if( _wcsicmp(Buffer,L"00000000000000.000000:000") != 0 ){
  875. v.SetStr(Buffer);
  876. }
  877. }
  878. break;
  879. // CIM_REFERENCE = 102,
  880. // CIM_STRING = 8,
  881. case VT_BSTR:
  882. v.SetStr(NULL);
  883. if( NaturallyAlignData( 2, READ_IT )){
  884. WORD wCount=0;
  885. WCHAR * pBuffer=NULL;
  886. // Get the size of the string
  887. m_pWMIBlockInfo->GetWord(wCount);
  888. hr = WBEM_S_NO_ERROR;
  889. if( wCount > 0 ){
  890. if( m_pWMIBlockInfo->CurrentPtrOk((ULONG)(wCount)) ){
  891. WORD wSize = wCount + 4;
  892. pBuffer = new WCHAR[wSize];
  893. if( pBuffer )
  894. {
  895. try
  896. {
  897. m_pWMIBlockInfo->GetString(pBuffer,wCount,wSize);
  898. v.SetStr(pBuffer);
  899. SAFE_DELETE_ARRAY(pBuffer);
  900. *m_pdwAccumulativeSizeOfBlock += wCount;
  901. }
  902. catch(...)
  903. {
  904. hr = WBEM_E_UNEXPECTED;
  905. SAFE_DELETE_ARRAY(pBuffer);
  906. throw;
  907. }
  908. }
  909. }
  910. else{
  911. hr = WBEM_E_INVALID_OBJECT;
  912. }
  913. }
  914. else{
  915. v.SetStr(NULL);
  916. }
  917. }
  918. break;
  919. // CIM_BOOLEAN = 11,
  920. case VT_BOOL:
  921. if( NaturallyAlignData( nSize, READ_IT )){
  922. hr = WBEM_S_NO_ERROR;
  923. BYTE bByte=0;
  924. m_pWMIBlockInfo->GetByte(bByte);
  925. v.SetBool((BOOL)bByte);
  926. }
  927. break;
  928. // CIM_SINT8 = 16,
  929. // CIM_UINT8 = 17,
  930. case VT_UI1:
  931. case VT_I1:
  932. if( NaturallyAlignData( nSize, READ_IT )){
  933. hr = WBEM_S_NO_ERROR;
  934. BYTE bByte=0;
  935. m_pWMIBlockInfo->GetByte(bByte);
  936. if( lType == VT_I1 ){
  937. v.SetShort((signed char)bByte);
  938. }
  939. else{
  940. v.SetByte(bByte);
  941. }
  942. }
  943. break;
  944. default:
  945. return WBEM_E_INVALID_OBJECT;
  946. }
  947. return hr;
  948. }
  949. //////////////////////////////////////////////////////////////////////
  950. int CWMIDataTypeMap::GetWMISize(long lType)
  951. {
  952. int nWMISize = 0;
  953. switch(lType){
  954. // CIM_SINT8 = 16,
  955. // CIM_UINT8 = 17,
  956. case VT_I1:
  957. case VT_UI1:
  958. nWMISize = sizeof(BYTE);
  959. break;
  960. // CIM_SINT16 = 2,
  961. // CIM_UINT16 = 18,
  962. case VT_I2:
  963. case CIM_CHAR16:
  964. case VT_UI2:
  965. nWMISize = sizeof(short);
  966. break;
  967. // CIM_SINT32 = 3,
  968. // CIM_UINT32 = 19,
  969. case VT_I4:
  970. case VT_UI4:
  971. nWMISize = sizeof(DWORD);
  972. break;
  973. // CIM_SINT64 = 20,
  974. // CIM_UINT64 = 21,
  975. case VT_I8:
  976. case VT_UI8:
  977. nWMISize = sizeof(__int64);
  978. break;
  979. // CIM_REAL32 = 4,
  980. case VT_R4:
  981. nWMISize = sizeof(float);
  982. break;
  983. // CIM_REAL64 = 5,
  984. case VT_R8:
  985. nWMISize = sizeof(double);
  986. break;
  987. // CIM_BOOLEAN = 11,
  988. case VT_BOOL:
  989. nWMISize = sizeof(BYTE);
  990. break;
  991. case CIM_DATETIME:
  992. nWMISize = SIZEOFWBEMDATETIME;
  993. break;
  994. case CIM_STRING:
  995. nWMISize = 2;
  996. break;
  997. default:
  998. // CIM_STRING = 8,
  999. // CIM_REFERENCE = 102,
  1000. // CIM_OBJECT = 13,
  1001. // CIM_FLAG_ARRAY = 0x2000
  1002. nWMISize = 0;
  1003. }
  1004. return nWMISize;
  1005. }
  1006. ///////////////////////////////////////////////////////////////
  1007. long CWMIDataTypeMap::GetVariantType(WCHAR * wcsType)
  1008. {
  1009. long lType;
  1010. // CIM_SINT8 = 16,
  1011. if( 0 == _wcsicmp( L"sint8",wcsType) ){
  1012. lType = VT_I1;
  1013. }
  1014. // CIM_UINT8 = 17,
  1015. else if( 0 == _wcsicmp( L"uint8",wcsType) ){
  1016. lType = VT_UI1;
  1017. }
  1018. // CIM_CHAR16 = 103,
  1019. else if( 0 == _wcsicmp( L"char16",wcsType) ){
  1020. lType = VT_I2;
  1021. }
  1022. // CIM_SINT16 = 2,
  1023. else if( 0 == _wcsicmp( L"sint16",wcsType) ){
  1024. lType = VT_I2;
  1025. }
  1026. // CIM_UINT16 = 18,
  1027. else if( 0 == _wcsicmp( L"uint16",wcsType) ){
  1028. lType = VT_UI2;
  1029. }
  1030. // CIM_SINT32 = 3,
  1031. else if( 0 == _wcsicmp( L"sint32",wcsType) ){
  1032. lType = VT_I4;
  1033. }
  1034. // CIM_UINT32 = 19,
  1035. else if( 0 == _wcsicmp( L"uint32",wcsType) ){
  1036. lType = VT_UI4;
  1037. }
  1038. // CIM_SINT64 = 20,
  1039. else if( 0 == _wcsicmp( L"sint64",wcsType) ){
  1040. lType = VT_I8;
  1041. }
  1042. // CIM_UINT64 = 21,
  1043. else if( 0 == _wcsicmp( L"uint64",wcsType) ){
  1044. lType = VT_UI8;
  1045. }
  1046. // CIM_REAL32 = 4,
  1047. else if( 0 == _wcsicmp( L"real32",wcsType) ){
  1048. lType = VT_R4;
  1049. }
  1050. // CIM_REAL64 = 5,
  1051. else if( 0 == _wcsicmp( L"real64",wcsType) ){
  1052. lType = VT_R8;
  1053. }
  1054. // CIM_BOOLEAN = 11,
  1055. else if( 0 == _wcsicmp( L"boolean",wcsType) ){
  1056. lType = VT_BOOL;
  1057. }
  1058. // CIM_DATETIME = 101,
  1059. else if( 0 == _wcsicmp( L"datetime",wcsType) ){
  1060. lType = CIM_DATETIME;
  1061. }
  1062. // CIM_STRING = 8,
  1063. // CIM_REFERENCE = 102,
  1064. // CIM_OBJECT = 13,
  1065. // CIM_FLAG_ARRAY = 0x2000
  1066. else{
  1067. lType = VT_BSTR;
  1068. }
  1069. return lType;
  1070. }
  1071. ///////////////////////////////////////////////////////////////
  1072. WCHAR * CWMIDataTypeMap::SetVariantType(long lType)
  1073. {
  1074. if( lType & CIM_FLAG_ARRAY ){
  1075. lType = lType &~ CIM_FLAG_ARRAY;
  1076. }
  1077. switch(lType){
  1078. // CIM_SINT8 = 16,
  1079. case VT_I1:
  1080. return L"sint8";
  1081. // CIM_UINT8 = 17,
  1082. case VT_UI1:
  1083. return L"uint8";
  1084. // CIM_SINT16 = 2,
  1085. case VT_I2:
  1086. return L"sint16";
  1087. // CIM_UINT16 = 18,
  1088. case VT_UI2:
  1089. return L"uint16";
  1090. // CIM_SINT32 = 3,
  1091. case VT_I4:
  1092. return L"sint32";
  1093. // CIM_UINT32 = 19,
  1094. case VT_UI4:
  1095. return L"uint32";
  1096. // CIM_SINT64 = 20,
  1097. case VT_I8:
  1098. return L"sint64";
  1099. // CIM_UINT64 = 21,
  1100. case VT_UI8:
  1101. return L"uint64";
  1102. // CIM_REAL32 = 4,
  1103. case VT_R4:
  1104. return L"real32";
  1105. // CIM_REAL64 = 5,
  1106. case VT_R8:
  1107. return L"real64";
  1108. // CIM_BOOLEAN = 11,
  1109. case VT_BOOL:
  1110. return L"boolean";
  1111. // CIM_STRING = 8,
  1112. case VT_BSTR:
  1113. return L"string";
  1114. // CIM_CHAR16 = 103,
  1115. case CIM_CHAR16:
  1116. return L"char16";
  1117. // CIM_OBJECT = 13,
  1118. case CIM_OBJECT:
  1119. return L"object";
  1120. // CIM_REFERENCE = 102,
  1121. case CIM_REFERENCE:
  1122. return L"ref";
  1123. // CIM_DATETIME = 101,
  1124. case CIM_DATETIME:
  1125. return L"datetime";
  1126. default: return NULL;
  1127. }
  1128. }
  1129. ////////////////////////////////////////////////////////////////
  1130. void CWMIDataTypeMap::GetSizeAndType( WCHAR * wcsType, IDOrder * p, long & lType, int & nWMISize )
  1131. {
  1132. BOOL fArray = FALSE;
  1133. if( lType & CIM_FLAG_ARRAY ){
  1134. fArray = TRUE;
  1135. }
  1136. if( 0 == _wcsnicmp( L"object:",wcsType,wcslen(L"object:"))){
  1137. //============================================
  1138. // Extract out the object name
  1139. //============================================
  1140. WCHAR * pName = new WCHAR[wcslen(wcsType)];
  1141. if( pName )
  1142. {
  1143. swscanf( wcsType,L"object:%s",pName);
  1144. p->SetEmbeddedName(pName);
  1145. SAFE_DELETE_ARRAY(pName);
  1146. lType = VT_UNKNOWN;
  1147. nWMISize = 0;
  1148. }
  1149. }
  1150. else{
  1151. lType = GetVariantType(wcsType);
  1152. nWMISize = GetWMISize(lType);
  1153. }
  1154. if( fArray ){
  1155. lType |= CIM_FLAG_ARRAY;
  1156. }
  1157. }
  1158. /////////////////////////////////////////////////////////////////////
  1159. long CWMIDataTypeMap::ConvertType(long lType )
  1160. {
  1161. long lConvert = lType;
  1162. switch (lType){
  1163. // CIM_SINT16 = 2,
  1164. // CIM_UINT16 = 18,
  1165. // CIM_SINT8
  1166. case VT_I1:
  1167. case VT_I2:
  1168. lConvert = VT_I2;
  1169. break;
  1170. case VT_UI2:
  1171. lConvert = VT_I4;
  1172. break;
  1173. // CIM_SINT32 = 3,
  1174. // CIM_UINT32 = 19,
  1175. case VT_I4:
  1176. case VT_UI4:
  1177. lConvert = VT_I4;
  1178. break;
  1179. // CIM_REAL32 = 4,
  1180. // CIM_REAL64 = 5,
  1181. case VT_R4:
  1182. case VT_R8:
  1183. lConvert = VT_R8;
  1184. break;
  1185. // CIM_SINT64 = 20,
  1186. // CIM_UINT64 = 21,
  1187. // CIM_STRING = 8,
  1188. // CIM_DATETIME = 101,
  1189. // CIM_REFERENCE = 102,
  1190. // CIM_CHAR16 = 103,
  1191. // CIM_OBJECT = 13,
  1192. // CIM_FLAG_ARRAY = 0x2000
  1193. case VT_I8:
  1194. case VT_UI8:
  1195. case VT_BSTR:
  1196. case CIM_DATETIME:
  1197. lConvert = VT_BSTR;
  1198. break;
  1199. // CIM_BOOLEAN = 11,
  1200. case VT_BOOL:
  1201. lConvert = VT_BOOL;
  1202. break;
  1203. // CIM_UINT8 = 17,
  1204. case VT_UI1:
  1205. lConvert = VT_UI1;
  1206. break;
  1207. case VT_UNKNOWN:
  1208. lConvert = VT_UNKNOWN;
  1209. break;
  1210. default:
  1211. break;
  1212. }
  1213. return lConvert;
  1214. }
  1215. /////////////////////////////////////////////////////////////////////
  1216. HRESULT CWMIDataTypeMap::PutInArray(SAFEARRAY * & psa,long * pi, long & lType, VARIANT * pVar)
  1217. {
  1218. HRESULT hr = WBEM_E_INVALID_OBJECT;
  1219. VARIANT v = *pVar;
  1220. switch (lType){
  1221. // CIM_SINT16 = 2,
  1222. // CIM_UINT16 = 18,
  1223. // CIM_SINT8
  1224. case VT_I1:
  1225. case VT_I2:
  1226. lType = V_VT(&v) = VT_I2;
  1227. hr = SafeArrayPutElement(psa,pi,&V_I2(&v));
  1228. break;
  1229. case VT_UI2:
  1230. lType = V_VT(&v) = VT_I4;
  1231. hr = SafeArrayPutElement(psa,pi,&V_I4(&v));
  1232. break;
  1233. // CIM_SINT32 = 3,
  1234. // CIM_UINT32 = 19,
  1235. case VT_I4:
  1236. case VT_UI4:
  1237. lType = V_VT(&v) = VT_I4;
  1238. hr = SafeArrayPutElement(psa,pi,&V_I4(&v));
  1239. break;
  1240. // CIM_REAL32 = 4,
  1241. // CIM_REAL64 = 5,
  1242. case VT_R4:
  1243. case VT_R8:
  1244. lType = V_VT(&v) = VT_R8;
  1245. hr = SafeArrayPutElement(psa,pi,&V_R8(&v));
  1246. break;
  1247. // CIM_SINT64 = 20,
  1248. // CIM_UINT64 = 21,
  1249. // CIM_STRING = 8,
  1250. // CIM_DATETIME = 101,
  1251. // CIM_REFERENCE = 102,
  1252. // CIM_CHAR16 = 103,
  1253. // CIM_OBJECT = 13,
  1254. // CIM_FLAG_ARRAY = 0x2000
  1255. case VT_I8:
  1256. case VT_UI8:
  1257. case VT_BSTR:
  1258. case CIM_DATETIME:
  1259. lType = V_VT(&v) = VT_BSTR;
  1260. hr = SafeArrayPutElement(psa,pi,V_BSTR(&v));
  1261. break;
  1262. // CIM_BOOLEAN = 11,
  1263. case VT_BOOL:
  1264. lType = V_VT(&v) = VT_BOOL;
  1265. hr = SafeArrayPutElement(psa,pi,&V_BOOL(&v));
  1266. break;
  1267. // CIM_UINT8 = 17,
  1268. case VT_UI1:
  1269. lType = V_VT(&v) = VT_UI1;
  1270. hr = SafeArrayPutElement(psa,pi,&V_UI1(&v));
  1271. break;
  1272. case VT_UNKNOWN:
  1273. lType = V_VT(&v) = VT_UNKNOWN;
  1274. hr = SafeArrayPutElement(psa,pi,V_UNKNOWN(&v));
  1275. break;
  1276. default:
  1277. break;
  1278. }
  1279. return hr;
  1280. }
  1281. //////////////////////////////////////////////////////////////////////
  1282. BOOL CWMIDataTypeMap::NaturallyAlignData( int nSize, BOOL fRead )
  1283. {
  1284. BOOL fRc = FALSE;
  1285. DWORD dwBytesToPad = 0;
  1286. if( *m_pdwAccumulativeSizeOfBlock != 0 ){
  1287. DWORD dwMod;
  1288. int nNewSize = nSize;
  1289. if( nSize == SIZEOFWBEMDATETIME ){
  1290. nNewSize = 2;
  1291. }
  1292. dwMod = *m_pdwAccumulativeSizeOfBlock % nNewSize;
  1293. if( dwMod > 0 ){
  1294. dwBytesToPad = (DWORD)nNewSize - dwMod;
  1295. }
  1296. }
  1297. if( fRead ){
  1298. if( m_pWMIBlockInfo->CurrentPtrOk((ULONG)(dwBytesToPad+nSize)) ){
  1299. fRc = TRUE;
  1300. }
  1301. else{
  1302. dwBytesToPad = 0;
  1303. }
  1304. }
  1305. else{
  1306. fRc = TRUE;
  1307. }
  1308. m_pWMIBlockInfo->AddPadding(dwBytesToPad);
  1309. *m_pdwAccumulativeSizeOfBlock += nSize + dwBytesToPad;
  1310. return fRc;
  1311. }