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.

502 lines
12 KiB

  1. //***************************************************************************
  2. //
  3. // (c) 1997 by Microsoft Corporation
  4. //
  5. // test.c
  6. //
  7. // a-davj 14-April-97 Created.
  8. //
  9. // Demonstration program for dumping out Binary Managed Object Format (BMOF)
  10. // data.
  11. //
  12. //***************************************************************************
  13. #include <windows.h>
  14. #include <ole2.h>
  15. #include <oleauto.h>
  16. #include <stdio.h>
  17. #include <io.h>
  18. #include <fcntl.h>
  19. #include <stdio.h>
  20. #include <sys/stat.h>
  21. #include <stdlib.h>
  22. #include "bmof.h"
  23. #include "mrcicode.h"
  24. void DisplayObject(CBMOFObj * po);
  25. //***************************************************************************
  26. //
  27. // void * BMOFAlloc
  28. //
  29. // DESCRIPTION:
  30. //
  31. // Provides allocation service for BMOF.C. This allows users to choose
  32. // the allocation method that is used.
  33. //
  34. // PARAMETERS:
  35. //
  36. // Size Input. Size of allocation in bytes.
  37. //
  38. // RETURN VALUE:
  39. //
  40. // pointer to new data. NULL if allocation failed.
  41. //
  42. //***************************************************************************
  43. void * BMOFAlloc(size_t Size)
  44. {
  45. return malloc(Size);
  46. }
  47. //***************************************************************************
  48. //
  49. // void BMOFFree
  50. //
  51. // DESCRIPTION:
  52. //
  53. // Provides allocation service for BMOF.C. This frees what ever was
  54. // allocated via BMOFAlloc.
  55. //
  56. // PARAMETERS:
  57. //
  58. // pointer to memory to be freed.
  59. //
  60. //***************************************************************************
  61. void BMOFFree(void * pFree)
  62. {
  63. free(pFree);
  64. }
  65. //***************************************************************************
  66. //
  67. // void DisplayVariant
  68. //
  69. // DESCRIPTION:
  70. //
  71. // Displays VARIANT data. Note that this is only done in this sample for
  72. // convienence, and using OLE is NOT necessary.
  73. //
  74. // PARAMETERS:
  75. //
  76. // pvar Input. Pointer to VARIANT which is to be displayed.
  77. // It is assumed that the variant type is simple, I.E.
  78. // neither the VT_ARRAY or VT_BYREF bits are set.
  79. //
  80. //***************************************************************************
  81. void DisplayVariant(VARIANT * pvar)
  82. {
  83. SCODE sc;
  84. VARIANT vTemp;
  85. // Uninitialized data will have a VT_NULL type.
  86. if(pvar->vt == VT_NULL)
  87. {
  88. printf(" data is NULL");
  89. return;
  90. }
  91. // String types can just be dumped.
  92. if(pvar->vt == VT_BSTR)
  93. {
  94. printf("value is %S",pvar->bstrVal);
  95. return;
  96. }
  97. else if(pvar->vt == VT_UNKNOWN)
  98. {
  99. CBMOFObj * pObj;
  100. printf(" got an embedded object");
  101. pObj = (CBMOFObj *)pvar->bstrVal;
  102. DisplayObject(pObj);
  103. return;
  104. }
  105. // For non string data, convert the infomation to a bstr and display it.
  106. VariantInit(&vTemp);
  107. sc = VariantChangeTypeEx(&vTemp, pvar,0,0, VT_BSTR);
  108. if(sc == S_OK)
  109. {
  110. printf("value is %S",vTemp.bstrVal);
  111. }
  112. else
  113. printf(" Couldnt convert type 0x%x, error code 0x%x", pvar->vt, sc);
  114. VariantClear(&vTemp);
  115. }
  116. //***************************************************************************
  117. //
  118. // void DisplayData
  119. //
  120. // DESCRIPTION:
  121. //
  122. // Displays the data held in a data item. Note that Ole is use just for
  123. // ease of use and is optional.
  124. //
  125. // PARAMETERS:
  126. //
  127. // pItem Input. Item to be displayed.
  128. //
  129. //***************************************************************************
  130. void DisplayData(CBMOFDataItem * pItem)
  131. {
  132. DWORD dwType, dwSimpleType;
  133. long lNumDim, lCnt;
  134. long lFirstDim;
  135. VARIANT var;
  136. // Determine the data type and clear out the variant
  137. dwType = pItem->m_dwType;
  138. printf("\nData type is 0x%x ", dwType);
  139. dwSimpleType = dwType & ~VT_ARRAY & ~VT_BYREF;
  140. memset((void *)&var.lVal, 0, 8);
  141. lNumDim = GetNumDimensions(pItem);
  142. if(lNumDim == 0)
  143. {
  144. // handle the simple scalar case. Note that uninitialized properties
  145. // will not have data.
  146. if(GetData(pItem, (BYTE *)&(var.lVal), NULL))
  147. {
  148. var.vt = (VARTYPE)dwSimpleType;
  149. DisplayVariant(&var);
  150. // Note the GetData does not use OLE to allocate BSTRs
  151. // and so we need to use our own freeing routine here.
  152. if(var.vt == VT_BSTR)
  153. BMOFFree(var.bstrVal);
  154. }
  155. else
  156. printf(" NULL ");
  157. }
  158. else if(lNumDim == 1)
  159. {
  160. // For the array case, just loop getting each element.
  161. // Start by getting the number of elements
  162. lFirstDim = GetNumElements(pItem, 0);
  163. if(lFirstDim < 1)
  164. {
  165. printf("\n CANT DISPLAY, BOGUS DIMENSION");
  166. return;
  167. }
  168. printf("\n");
  169. for(lCnt = 0; lCnt < lFirstDim; lCnt++)
  170. {
  171. if(GetData(pItem, (BYTE *)&(var.lVal), &lCnt))
  172. {
  173. var.vt = (VARTYPE)dwSimpleType;
  174. DisplayVariant(&var);
  175. // Note the GetData does not use OLE to allocate BSTRs
  176. // and so we need to use our own freeing routine here.
  177. if(var.vt == VT_BSTR)
  178. BMOFFree(var.bstrVal);
  179. printf("\n");
  180. }
  181. else
  182. printf(" NULL ");
  183. }
  184. }
  185. else if(lNumDim == -1)
  186. {
  187. printf("\n Undefined array");
  188. return;
  189. }
  190. else
  191. {
  192. // Currently multidimension arrays are not supported.
  193. printf("\n CANT DISPLAY, TOO MANY DIMEMSIONS");
  194. return;
  195. }
  196. return;
  197. }
  198. //***************************************************************************
  199. //
  200. // void DisplayQualList
  201. //
  202. // DESCRIPTION:
  203. //
  204. // Helper routine for displaying a qualifier list.
  205. //
  206. // PARAMETERS:
  207. //
  208. // pql Input. Pointer to structure which wraps the
  209. // qualifier list.
  210. //
  211. //***************************************************************************
  212. void DisplayQualList(CBMOFQualList * pql)
  213. {
  214. WCHAR * pName = NULL;
  215. CBMOFDataItem Item;
  216. ResetQualList(pql);
  217. printf("\nDisplaying qual list");
  218. while(NextQual(pql, &pName, &Item))
  219. {
  220. printf("\nQualifier name is -%S- ",pName);
  221. DisplayData(&Item);
  222. BMOFFree(pName);
  223. }
  224. }
  225. //***************************************************************************
  226. //
  227. // void DisplayObject
  228. //
  229. // DESCRIPTION:
  230. //
  231. // Helper routine for displaying a class or instance.
  232. //
  233. // PARAMETERS:
  234. //
  235. // po Input. Pointer to structure that wraps the object.
  236. //
  237. //***************************************************************************
  238. void DisplayObject(CBMOFObj * po)
  239. {
  240. CBMOFQualList * pql;
  241. CBMOFDataItem Item;
  242. WCHAR * pName = NULL;
  243. BOOL bfirstmethod = TRUE;
  244. // Display the objects name, its type (Is it a class or instance), and
  245. // display the qualifier set which is attached to the object.
  246. if(GetName(po, &pName))
  247. {
  248. printf("\n\nLooking at object %S",pName);
  249. BMOFFree(pName);
  250. }
  251. printf("\nThe objects type is 0x%x", GetType(po));
  252. pql = GetQualList(po);
  253. if(pql)
  254. {
  255. DisplayQualList(pql);
  256. BMOFFree(pql);
  257. pql = NULL;
  258. }
  259. // Display each property and it associated qualifier list
  260. ResetObj(po);
  261. printf("\nDisplaying prop list");
  262. while(NextProp(po, &pName, &Item))
  263. {
  264. printf("\n\nProperty name is -%S- type is 0x%x",pName, Item.m_dwType);
  265. DisplayData(&Item);
  266. pql = GetPropQualList(po, pName);
  267. if(pql)
  268. {
  269. DisplayQualList(pql);
  270. BMOFFree(pql);
  271. pql = NULL;
  272. }
  273. BMOFFree(pName);
  274. }
  275. while(NextMeth(po, &pName, &Item))
  276. {
  277. if(bfirstmethod)
  278. printf("\nDisplaying method list");
  279. bfirstmethod = FALSE;
  280. printf("\n\nMethod name is -%S- type is 0x%x",pName, Item.m_dwType);
  281. DisplayData(&Item);
  282. pql = GetMethQualList(po, pName);
  283. if(pql)
  284. {
  285. DisplayQualList(pql);
  286. BMOFFree(pql);
  287. pql = NULL;
  288. }
  289. BMOFFree(pName);
  290. }
  291. }
  292. //***************************************************************************
  293. //
  294. // BYTE * ReadBMOFFile
  295. //
  296. // DESCRIPTION:
  297. //
  298. // Opens and decompresses the binary mof file
  299. //
  300. // PARAMETERS:
  301. //
  302. // pFileName Input. Pointer to structure that wraps the object.
  303. //
  304. // RETURN VALUE:
  305. //
  306. // pointer to the binary mof data. This should be freed using "free". Note that
  307. // NULL is returned for all errors.
  308. //
  309. //***************************************************************************
  310. BYTE * ReadBMOFFile(char * pFileName)
  311. {
  312. int fh1 = -1;
  313. int iRet;
  314. DWORD dwCompType, dwCompressedSize, dwExpandedSize, dwSig, dwResSize;
  315. BYTE * pCompressed = NULL;
  316. BYTE * pExpanded = NULL;
  317. fh1 = _open(pFileName, _O_BINARY | _O_RDONLY);
  318. if(fh1 == -1)
  319. {
  320. printf("\nCould not open the file %s", pFileName);
  321. return NULL;
  322. }
  323. // get the signature, compression type, and the sizes
  324. iRet = _read(fh1, &dwSig, sizeof(DWORD));
  325. if((DWORD)iRet != sizeof(DWORD))
  326. {
  327. printf("\nError reading file");
  328. _close(fh1);
  329. return NULL;
  330. }
  331. iRet = _read(fh1, &dwCompType, sizeof(DWORD));
  332. iRet = _read(fh1, &dwCompressedSize, sizeof(DWORD));
  333. iRet = _read(fh1, &dwExpandedSize, sizeof(DWORD));
  334. // make sure the signature is valid and that the compression type is one
  335. // we understand!
  336. if(dwSig != BMOF_SIG ||dwCompType != 1)
  337. {
  338. _close(fh1);
  339. return NULL;
  340. }
  341. // Allocate storage for the compressed data and
  342. // expanded data
  343. pCompressed = malloc(dwCompressedSize);
  344. pExpanded = malloc(dwExpandedSize);
  345. if(pCompressed == NULL || pExpanded == NULL)
  346. {
  347. _close(fh1);
  348. return NULL;
  349. }
  350. // Read the compressed data.
  351. iRet = _read(fh1, pCompressed, dwCompressedSize);
  352. if((DWORD)iRet != dwCompressedSize)
  353. {
  354. printf("\nError reading data");
  355. free(pExpanded);
  356. free(pCompressed);
  357. return NULL;
  358. }
  359. _close(fh1);
  360. // Decompress the data
  361. dwResSize = Mrci1Decompress(pCompressed, dwCompressedSize, pExpanded, dwExpandedSize);
  362. free(pCompressed);
  363. if(dwResSize != dwExpandedSize)
  364. {
  365. printf("\nError expanding data!!!");
  366. free(pExpanded);
  367. return NULL;
  368. }
  369. return pExpanded;
  370. }
  371. //***************************************************************************
  372. //
  373. // int main
  374. //
  375. // DESCRIPTION:
  376. //
  377. // Entry point.
  378. //
  379. // COMMAND LINE ARGUMENT:
  380. //
  381. // This program should be launced with a single argument which is the
  382. // name of the file which contains the BMOF.
  383. //
  384. // RETURN VALUE:
  385. //
  386. // 0 if OK, 1 if error.
  387. //
  388. //***************************************************************************
  389. int _cdecl main(int argc, char ** argv)
  390. {
  391. BYTE * pTest;
  392. CBMOFObjList * pol;
  393. CBMOFObj * po;
  394. // check the command line
  395. if(argc < 2)
  396. {
  397. printf("\nusage: test BMOFFILE\nwhere BMOFFILE is the binary mof file to dump");
  398. return 1;
  399. }
  400. pTest = ReadBMOFFile(argv[1]);
  401. if(pTest == NULL)
  402. {
  403. printf("\nterminating abnormally, could not read binary mof");
  404. return 1;
  405. }
  406. // Now use the helper functions to dump out the file. Create an object
  407. // list structure and use it to enumerate the objects.
  408. pol = CreateObjList(pTest);
  409. if(pol == NULL)
  410. {
  411. return 1;
  412. }
  413. printf("\nThe number of objects is %d",pol->m_pol->dwNumberOfObjects);
  414. ResetObjList (pol);
  415. while(po = NextObj(pol))
  416. {
  417. DisplayObject(po);
  418. BMOFFree(po);
  419. }
  420. BMOFFree(pol);
  421. free(pTest);
  422. printf("\nTerminating normally\n");
  423. return 0;
  424. }
  425.