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.

711 lines
15 KiB

  1. /*++
  2. Copyright (c) 1997-1999 Microsoft Corporation
  3. Revision History:
  4. --*/
  5. /************STARTDOC*********************************************************
  6. *
  7. * print.c
  8. *
  9. * This file contains the functions print specific WMI structures
  10. * out to stdout.
  11. *
  12. *
  13. * Modification History:
  14. * --------------------
  15. *
  16. * Drew McDaniel (drewm) - Sept. 16, 1997 - Original Source
  17. *
  18. *
  19. *************ENDDOC**********************************************************/
  20. #include <windows.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <ole2.h>
  24. #include <wmium.h>
  25. #include <tchar.h>
  26. #include "print.h"
  27. // Defined constants
  28. //
  29. // Modular Data
  30. //
  31. // Function Prototypes
  32. //
  33. //+----------------------------------------------------------
  34. //
  35. // Function: PrintGuid
  36. //
  37. // Descrip: Prints a singe guid. Does not print the end
  38. // new line or cariage return
  39. //
  40. // Returns: VOID
  41. //
  42. // Notes: Does not print end cariage return or new line
  43. //
  44. // History: 09/16/97 drewm Created
  45. //-----------------------------------------------------------
  46. VOID
  47. PrintGuid(
  48. LPGUID lpGuid
  49. )
  50. {
  51. printf("0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
  52. lpGuid->Data1,
  53. lpGuid->Data2,
  54. lpGuid->Data3,
  55. lpGuid->Data4[0],
  56. lpGuid->Data4[1],
  57. lpGuid->Data4[2],
  58. lpGuid->Data4[3],
  59. lpGuid->Data4[4],
  60. lpGuid->Data4[5],
  61. lpGuid->Data4[6],
  62. lpGuid->Data4[7]);
  63. }
  64. /*
  65. VOID
  66. PrintClassInfoHeader(
  67. PMOFCLASSINFO pMofClassInfo
  68. )
  69. {
  70. printf("Guid: ");
  71. PrintGuid(&(pMofClassInfo->Guid));
  72. printf("\n");
  73. _tprintf(__T("Name: %s\n"), pMofClassInfo->Name);
  74. _tprintf(__T("Description: %s\n"), pMofClassInfo->Description);
  75. printf("Language ID: %u\n", pMofClassInfo->Language);
  76. printf("Flags: 0x%x\n", pMofClassInfo->Flags);
  77. printf("Version: %u\n", pMofClassInfo->Version);
  78. printf("\n");
  79. }
  80. VOID
  81. PrintDataItem(
  82. PMOFDATAITEM lpDataItem
  83. )
  84. {
  85. _tprintf(__T("Name: %s\n"), lpDataItem->Name);
  86. _tprintf(__T("Description: %s\n"), lpDataItem->Description);
  87. printf("Data Type: ");
  88. PrintDataType(lpDataItem->DataType);
  89. printf("\n");
  90. printf("Version: %u\n", lpDataItem->Version);
  91. printf("Size: 0x%x\n", lpDataItem->SizeInBytes);
  92. printf("Flags: 0x%x\n", lpDataItem->Flags);
  93. printf("Embedded Class Guid: ");
  94. PrintGuid(&(lpDataItem->EmbeddedClassGuid));
  95. printf("\n");
  96. if (lpDataItem->Flags & MOFDI_FLAG_FIXED_ARRAY)
  97. {
  98. printf("Fixed array elements: %u\n", lpDataItem->FixedArrayElements);
  99. }
  100. if (lpDataItem->Flags & MOFDI_FLAG_VARIABLE_ARRAY)
  101. {
  102. printf("Variable length size ID: %u\n", lpDataItem->VariableArraySizeId);
  103. }
  104. }
  105. VOID
  106. PrintClassQualifier(
  107. LPTSTR lpQualifier,
  108. MOFHANDLE hMofHandle
  109. )
  110. {
  111. BYTE Buffer[MAX_NAME_LENGTH];
  112. DWORD dwBufferSize = MAX_NAME_LENGTH;
  113. DWORD dwRet;
  114. MOFDATATYPE MofDataType;
  115. dwRet = WmiMofQueryClassQualifier( hMofHandle,
  116. lpQualifier,
  117. &MofDataType,
  118. &dwBufferSize,
  119. Buffer);
  120. if (dwRet != ERROR_SUCCESS)
  121. {
  122. printf("WmiMofQueryClassQualifier failed. (%u)\n", dwRet);
  123. exit (0);
  124. }
  125. switch( MofDataType )
  126. {
  127. case MOFInt32:
  128. case MOFUInt32:
  129. case MOFInt64:
  130. case MOFUInt64:
  131. case MOFInt16:
  132. case MOFUInt16:
  133. _tprintf(__T("%s: %u\n"), lpQualifier, Buffer);
  134. break;
  135. case MOFChar:
  136. case MOFString:
  137. case MOFWChar:
  138. _tprintf(__T("%s: %s\n"), lpQualifier, Buffer);
  139. break;
  140. case MOFByte:
  141. _tprintf(__T("%s: Unsupported type MOFByte\n"), lpQualifier);
  142. break;
  143. case MOFDate:
  144. _tprintf(__T("%s: Unsupported type MOFDate\n"), lpQualifier);
  145. break;
  146. case MOFBoolean:
  147. if (*Buffer == TRUE)
  148. {
  149. _tprintf(__T("%s: TRUE\n"), lpQualifier);
  150. }
  151. else
  152. {
  153. _tprintf(__T("%s: FALSE\n"), lpQualifier);
  154. }
  155. break;
  156. case MOFEmbedded:
  157. _tprintf(__T("%s: Unsupported type MOFEmbedded\n"), lpQualifier);
  158. break;
  159. case MOFUnknown:
  160. _tprintf(__T("%s: Unsupported type MOFUnknown\n"), lpQualifier);
  161. break;
  162. default:
  163. printf("Invalid Data Type\n");
  164. }
  165. }
  166. VOID
  167. PrintDataType(
  168. MOFDATATYPE E_DataType
  169. )
  170. {
  171. switch( E_DataType )
  172. {
  173. case MOFInt32:
  174. printf("MOFInt32");
  175. break;
  176. case MOFUInt32:
  177. printf("MOFUInt32");
  178. break;
  179. case MOFInt64:
  180. printf("MOFInt64");
  181. break;
  182. case MOFUInt64:
  183. printf("MOFUInt64");
  184. break;
  185. case MOFInt16:
  186. printf("MOFInt16");
  187. break;
  188. case MOFUInt16:
  189. printf("MOFUInt16");
  190. break;
  191. case MOFChar:
  192. printf("MOFChar");
  193. break;
  194. case MOFByte:
  195. printf("MOFByte");
  196. break;
  197. case MOFWChar:
  198. printf("MOFWChar");
  199. break;
  200. case MOFDate:
  201. printf("MOFDate");
  202. break;
  203. case MOFBoolean:
  204. printf("MOFBoolean");
  205. break;
  206. case MOFEmbedded:
  207. printf("MOFEmbedded");
  208. break;
  209. case MOFString:
  210. printf("MOFString");
  211. break;
  212. case MOFUnknown:
  213. printf("Unknown");
  214. break;
  215. default:
  216. printf("Invalid Data Type\n");
  217. }
  218. }
  219. */
  220. VOID ClearScreen()
  221. {
  222. UINT i;
  223. for (i = 0; i < 40; i++)
  224. {
  225. printf("\n");
  226. }
  227. }
  228. VOID
  229. WaitForUser()
  230. {
  231. printf("Hit Enter to Continue\n");
  232. getchar ();
  233. getchar ();
  234. }
  235. /*
  236. //+----------------------------------------------------------
  237. //
  238. // Function: PrintDescription
  239. //
  240. // Descrip: Prints the description of the selected guid
  241. //
  242. // Returns: VOID
  243. //
  244. // Notes: If there is an error in opening or printing
  245. // the description, an error message an error
  246. // code will be printed instead of the description.
  247. //
  248. // History: 09/16/97 drewm Created
  249. //-----------------------------------------------------------
  250. VOID PrintDescription(
  251. LPGUID lpGuid
  252. )
  253. {
  254. DWORD dwRet;
  255. USHORT LangID;
  256. PMOFCLASSINFO pMofClassInfo;
  257. MOFHANDLE MofClassHandle;
  258. // Make the language ID
  259. //
  260. // LangID = MAKELANGID( LANG_ENGLISH, SUBLANG_ENGLISH_US);
  261. LangID = MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL);
  262. // Get the class ID
  263. //
  264. dwRet = WmiMofOpenClassInfo( lpGuid,
  265. LangID,
  266. &(pMofClassInfo),
  267. &MofClassHandle);
  268. if (dwRet != ERROR_SUCCESS)
  269. {
  270. printf("Unable to open MofClassInfo handle (%u)\n", dwRet);
  271. return;
  272. }
  273. _tprintf(__T("%s\n"), pMofClassInfo->Description);
  274. WmiMofCloseClassInfo( MofClassHandle, pMofClassInfo );
  275. }
  276. */
  277. //+----------------------------------------------------------
  278. //
  279. // Function: PrintHeader
  280. //
  281. // Descrip: Prints a wnode header structure.
  282. //
  283. // Returns: VOID
  284. //
  285. // Notes: None
  286. //
  287. // History: 04/08/97 drewm Created
  288. //-----------------------------------------------------------
  289. VOID
  290. PrintHeader(
  291. IN WNODE_HEADER Header
  292. )
  293. {
  294. SYSTEMTIME sysTime;
  295. FILETIME fileTime;
  296. FILETIME localFileTime;
  297. // Convert the file time
  298. //
  299. fileTime.dwLowDateTime = Header.TimeStamp.LowPart;
  300. fileTime.dwHighDateTime = Header.TimeStamp.HighPart;
  301. FileTimeToLocalFileTime( &fileTime,
  302. &localFileTime );
  303. FileTimeToSystemTime( &localFileTime,
  304. &sysTime);
  305. // Print the info
  306. //
  307. printf("Buffer Size: 0x%x\n"
  308. "Provider Id: 0x%x\n"
  309. "Version : %u\n"
  310. "Linkage : 0x%x\n"
  311. "Time Stamp : %u:%02u %u\\%u\\%u\n"
  312. "Guid : 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n"
  313. "Flags : 0x%02x\n",
  314. Header.BufferSize,
  315. Header.ProviderId,
  316. Header.Version,
  317. Header.Linkage,
  318. sysTime.wHour,
  319. sysTime.wMinute,
  320. sysTime.wMonth,
  321. sysTime.wDay,
  322. sysTime.wYear,
  323. Header.Guid.Data1,
  324. Header.Guid.Data2,
  325. Header.Guid.Data3,
  326. Header.Guid.Data4[0],
  327. Header.Guid.Data4[1],
  328. Header.Guid.Data4[2],
  329. Header.Guid.Data4[3],
  330. Header.Guid.Data4[4],
  331. Header.Guid.Data4[5],
  332. Header.Guid.Data4[6],
  333. Header.Guid.Data4[7],
  334. Header.Flags );
  335. // Print readable flags
  336. //
  337. if (Header.Flags & WNODE_FLAG_ALL_DATA)
  338. {
  339. printf("WNODE_FLAG_ALL_DATA\n");
  340. }
  341. if (Header.Flags & WNODE_FLAG_SINGLE_INSTANCE)
  342. {
  343. printf("WNODE_FLAG_SINGLE_INSTANCE\n");
  344. }
  345. if (Header.Flags & WNODE_FLAG_SINGLE_ITEM)
  346. {
  347. printf("WNODE_FLAG_SINGLE_ITEM\n");
  348. }
  349. if (Header.Flags & WNODE_FLAG_EVENT_ITEM)
  350. {
  351. printf("WNODE_FLAG_EVENT_ITEM\n");
  352. }
  353. if (Header.Flags & WNODE_FLAG_FIXED_INSTANCE_SIZE)
  354. {
  355. printf("WNODE_FLAG_FIXED_INSTANCE_SIZE\n");
  356. }
  357. if (Header.Flags & WNODE_FLAG_TOO_SMALL)
  358. {
  359. printf("WNODE_FLAG_TOO_SMALL\n");
  360. }
  361. if (Header.Flags & WNODE_FLAG_INSTANCES_SAME)
  362. {
  363. printf("WNODE_FLAG_INSTANCES_SAME\n");
  364. }
  365. if (Header.Flags & WNODE_FLAG_INTERNAL)
  366. {
  367. printf("WNODE_FLAG_INTERNAL\n");
  368. }
  369. if (Header.Flags & WNODE_FLAG_USE_TIMESTAMP)
  370. {
  371. printf("WNODE_FLAG_USE_TIMESTAMP\n");
  372. }
  373. if (Header.Flags & WNODE_FLAG_TRACED_GUID)
  374. {
  375. printf("WNODE_FLAG_TRACED_GUID\n");
  376. }
  377. if (Header.Flags & WNODE_FLAG_EVENT_REFERENCE)
  378. {
  379. printf("WNODE_FLAG_EVENT_REFERENCE\n");
  380. }
  381. if (Header.Flags & WNODE_FLAG_ANSI_INSTANCENAMES)
  382. {
  383. printf("WNODE_FLAG_ANSI_INSTANCENAMES\n");
  384. }
  385. if (Header.Flags & WNODE_FLAG_METHOD_ITEM)
  386. {
  387. printf("WNODE_FLAG_METHOD_ITEM\n");
  388. }
  389. if (Header.Flags & WNODE_FLAG_PDO_INSTANCE_NAMES)
  390. {
  391. printf("WNODE_FLAG_PDO_INSTANCE_NAMES\n");
  392. }
  393. printf("\n");
  394. }
  395. //+----------------------------------------------------------
  396. //
  397. // Function: PrintAllData
  398. //
  399. // Descrip: Prints a WNODE_ALL_DATA structure.
  400. //
  401. // Returns: VOID
  402. //
  403. // Notes: None
  404. //
  405. // History: 04/08/97 drewm Created
  406. //-----------------------------------------------------------
  407. VOID
  408. PrintAllData(
  409. IN PWNODE_ALL_DATA Wnode
  410. )
  411. {
  412. DWORD dwInstanceNum;
  413. DWORD dwByteCount;
  414. DWORD dwFlags;
  415. DWORD dwStructureNum = 1;
  416. DWORD dwTemp;
  417. DWORD dwInstanceSize;
  418. LPDWORD lpdwNameOffsets;
  419. PBYTE lpbyteData;
  420. BOOL bFixedSize = FALSE;
  421. USHORT usNameLength;
  422. WCHAR lpNameW[MAX_NAME_LENGTH];
  423. CHAR lpName[MAX_NAME_LENGTH];
  424. printf("\n\n");
  425. do{
  426. printf("\n\nPrinting WNODE_ALL_DATA structure %d.\n", dwStructureNum++);
  427. PrintHeader(Wnode->WnodeHeader);
  428. dwFlags = Wnode->WnodeHeader.Flags;
  429. if ( ! (dwFlags & WNODE_FLAG_ALL_DATA)) {
  430. printf("Not a WNODE_ALL_DATA structure\n");
  431. return;
  432. }
  433. // Check for fixed instance size
  434. //
  435. if ( dwFlags & WNODE_FLAG_FIXED_INSTANCE_SIZE )
  436. {
  437. dwInstanceSize = Wnode->FixedInstanceSize;
  438. bFixedSize = TRUE;
  439. lpbyteData = OffsetToPtr((PBYTE)Wnode, Wnode->DataBlockOffset);
  440. printf("Fixed size: 0x%x\n", dwInstanceSize);
  441. }
  442. // Get a pointer to the array of offsets to the instance names
  443. //
  444. lpdwNameOffsets = (LPDWORD) OffsetToPtr(Wnode, Wnode->OffsetInstanceNameOffsets);
  445. // Print out each instance name and data. The name will always be
  446. // in UNICODE so it needs to be translated to ASCII before it can be
  447. // printed out.
  448. //
  449. for ( dwInstanceNum = 0; dwInstanceNum < Wnode->InstanceCount; dwInstanceNum++)
  450. {
  451. printf("Instance %d\n", 1 + dwInstanceNum);
  452. PrintCountedString( (LPTSTR)
  453. OffsetToPtr( Wnode,
  454. lpdwNameOffsets[dwInstanceNum]) );
  455. // Length and offset for variable data
  456. //
  457. if ( !bFixedSize)
  458. {
  459. dwInstanceSize = Wnode->OffsetInstanceDataAndLength[dwInstanceNum].
  460. LengthInstanceData;
  461. printf("Data size 0x%x\n", dwInstanceSize);
  462. lpbyteData = (PBYTE) OffsetToPtr(
  463. (PBYTE)Wnode,
  464. Wnode->OffsetInstanceDataAndLength[dwInstanceNum].
  465. OffsetInstanceData);
  466. }
  467. printf("Data:");
  468. for ( dwByteCount = 0; dwByteCount < dwInstanceSize;)
  469. {
  470. // Print data in groups of DWORDS but allow for single bytes.
  471. //
  472. if ( (dwByteCount % 16) == 0)
  473. {
  474. printf("\n");
  475. }
  476. if ( (dwByteCount % 4) == 0)
  477. {
  478. printf(" 0x");
  479. }
  480. dwTemp = *((LPDWORD)lpbyteData);
  481. printf("%08x", dwTemp );
  482. lpbyteData += sizeof(DWORD);
  483. dwByteCount += sizeof(DWORD);
  484. } // for cByteCount
  485. printf("\n\n");
  486. } // for cInstanceNum
  487. // Update Wnode to point to next node
  488. //
  489. if ( Wnode->WnodeHeader.Linkage != 0)
  490. {
  491. Wnode = (PWNODE_ALL_DATA) OffsetToPtr( Wnode, Wnode->WnodeHeader.Linkage);
  492. }
  493. else
  494. {
  495. Wnode = 0;
  496. }
  497. }while(Wnode != 0);
  498. }
  499. //+----------------------------------------------------------
  500. //
  501. // Function: PrintSingleInstance
  502. //
  503. // Descrip: Prints a WNODE_SINGLE_INSTANCE structure.
  504. //
  505. // Returns: VOID
  506. //
  507. // Notes: None
  508. //
  509. // History: 04/08/97 drewm Created
  510. //-----------------------------------------------------------
  511. VOID PrintSingleInstance(
  512. IN PWNODE_SINGLE_INSTANCE Wnode
  513. )
  514. {
  515. DWORD dwByteCount;
  516. DWORD dwFlags;
  517. LPDWORD lpdwData;
  518. USHORT usNameLength;
  519. WCHAR lpNameW[MAX_NAME_LENGTH];
  520. CHAR lpName[MAX_NAME_LENGTH];
  521. dwFlags = Wnode->WnodeHeader.Flags;
  522. if ( ! (dwFlags & WNODE_FLAG_SINGLE_INSTANCE))
  523. {
  524. printf("Not a WNODE_SINGLE_INSTANCE structure\n");
  525. return;
  526. }
  527. printf("\nPrinting WNODE_SINGLE_INSTANCE.\n");
  528. PrintHeader(Wnode->WnodeHeader);
  529. // Print name or index number
  530. //
  531. if ( dwFlags & WNODE_FLAG_STATIC_INSTANCE_NAMES )
  532. {
  533. printf("Instance index: %d\n", Wnode->InstanceIndex);
  534. }
  535. usNameLength = * (USHORT *) OffsetToPtr(Wnode, Wnode->OffsetInstanceName);
  536. printf("Name length 0x%x\n", usNameLength);
  537. usNameLength /= 2;
  538. PrintCountedString( (LPTSTR) OffsetToPtr( Wnode,
  539. Wnode->OffsetInstanceName) );
  540. // wcscpy(lpNameW, (LPWSTR) (OffsetToPtr(Wnode, Wnode->OffsetInstanceName )
  541. // + sizeof(USHORT)));
  542. // wcsncpy( lpNameW + usNameLength, L" ", 2);
  543. // wcstombs( lpName, lpNameW, 300);
  544. // printf("%s\n", lpName);
  545. // Print out the Data
  546. //
  547. printf("Data:\n");
  548. printf("Data Size: 0x%x\n", Wnode->SizeDataBlock);
  549. lpdwData = (PULONG) OffsetToPtr(Wnode, Wnode->DataBlockOffset);
  550. for ( dwByteCount = 0; dwByteCount < Wnode->SizeDataBlock; dwByteCount+= sizeof(ULONG))
  551. {
  552. if ( (dwByteCount % 16) == 0)
  553. {
  554. printf("\n");
  555. }
  556. printf("0x%08x ", *lpdwData );
  557. lpdwData++;
  558. }
  559. printf("\n");
  560. } // PrintSingleInstance
  561. VOID
  562. PrintCountedString(
  563. LPTSTR lpString
  564. )
  565. {
  566. SHORT usNameLength;
  567. LPTSTR lpStringPlusNull;
  568. usNameLength = * (USHORT *) lpString;
  569. lpStringPlusNull = (LPTSTR) malloc ( usNameLength + sizeof(TCHAR) );
  570. if (lpStringPlusNull != NULL)
  571. {
  572. lpString = (LPTSTR) ((PBYTE)lpString + sizeof(USHORT));
  573. if (MyIsTextUnicode(lpString))
  574. {
  575. usNameLength /= 2;
  576. }
  577. _tcsncpy( lpStringPlusNull, lpString, usNameLength );
  578. _tcscpy( lpStringPlusNull + usNameLength, __T("") );
  579. _tprintf(__T("%s\n"), lpStringPlusNull);
  580. free(lpStringPlusNull);
  581. }
  582. }
  583.