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.

542 lines
12 KiB

  1. //-----------------------------------------------------------------------------
  2. //
  3. // File: IMPRESOB.CPP
  4. //
  5. // Implementation of the ICreateRecObj class
  6. //
  7. // Copyright (c) 1995 - 1997, Microsoft Corporation. All rights reserved.
  8. //
  9. //-----------------------------------------------------------------------------
  10. #include "stdafx.h"
  11. #include "dllvars.h"
  12. #include "resource.h"
  13. #include "impbin.h"
  14. #include "win32sub.h"
  15. #include "impresob.h"
  16. #include "sampres.h"
  17. #include "samplver.h"
  18. #include "misc.h"
  19. #ifdef _DEBUG
  20. #undef THIS_FILE
  21. static char BASED_CODE THIS_FILE[] = __FILE__;
  22. #endif
  23. #define new DEBUG_NEW
  24. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  25. //
  26. // Constructor and member init
  27. //
  28. //------------------------------------------------------------------------------
  29. CLocImpResObj::CLocImpResObj()
  30. {
  31. m_ulRefCount = 0;
  32. m_IBinary.m_pParent = this;
  33. m_IParser.m_pParent = this;
  34. AddRef();
  35. IncrementClassCount();
  36. }
  37. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  38. //
  39. // Destructor and member clean up
  40. //
  41. //------------------------------------------------------------------------------
  42. CLocImpResObj::~CLocImpResObj()
  43. {
  44. DEBUGONLY(AssertValid());
  45. LTASSERT(m_ulRefCount == 0);
  46. DecrementClassCount();
  47. }
  48. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  49. //
  50. // Add to the reference count on the object. Return the new count
  51. //
  52. //------------------------------------------------------------------------------
  53. STDMETHODIMP_(ULONG)
  54. CLocImpResObj::AddRef(void)
  55. {
  56. DEBUGONLY(AssertValid());
  57. return ++m_ulRefCount;
  58. }
  59. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  60. //
  61. // Subtract from the reference count
  62. //
  63. //------------------------------------------------------------------------------
  64. STDMETHODIMP_(ULONG)
  65. CLocImpResObj::Release(void)
  66. {
  67. DEBUGONLY(AssertValid());
  68. LTASSERT(m_ulRefCount != 0);
  69. m_ulRefCount--;
  70. if (m_ulRefCount == 0)
  71. {
  72. delete this;
  73. return 0;
  74. }
  75. return m_ulRefCount;
  76. }
  77. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  78. //
  79. // Query for the passed IID. Interfaces not implemented on this object
  80. // are implemented as embedded objects. If the interface is found
  81. // the reference count in increased.
  82. //
  83. //------------------------------------------------------------------------------
  84. STDMETHODIMP
  85. CLocImpResObj::QueryInterface(
  86. REFIID iid,
  87. LPVOID *ppvObj)
  88. {
  89. DEBUGONLY(AssertValid());
  90. SCODE scResult = E_NOINTERFACE;
  91. *ppvObj = NULL;
  92. if (iid == IID_IUnknown)
  93. {
  94. *ppvObj = (IUnknown *)this;
  95. scResult = S_OK;
  96. }
  97. else if (iid == IID_ICreateResObj2)
  98. {
  99. *ppvObj = (ICreateResObj2 *)this;
  100. scResult = S_OK;
  101. }
  102. else if (iid == IID_ILocBinary)
  103. {
  104. LTASSERT(NULL != m_IBinary.m_pParent);
  105. *ppvObj = &m_IBinary;
  106. scResult = S_OK;
  107. }
  108. else if (iid == IID_ILocParser)
  109. {
  110. LTASSERT(NULL != m_IParser.m_pParent);
  111. *ppvObj = &m_IParser;
  112. scResult = S_OK;
  113. }
  114. else if (iid == IID_ILocVersion)
  115. {
  116. *ppvObj = (ILocVersion *) new CLocSamplVersion(this);
  117. scResult = S_OK;
  118. }
  119. if (scResult == S_OK)
  120. {
  121. AddRef();
  122. }
  123. return ResultFromScode(scResult);
  124. }
  125. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  126. //
  127. // Assert this interface
  128. //
  129. //------------------------------------------------------------------------------
  130. STDMETHODIMP_(void)
  131. CLocImpResObj::AssertValidInterface(void)
  132. const
  133. {
  134. DEBUGONLY(AssertValid());
  135. }
  136. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  137. //
  138. // Create a CResObj from the CLocItem passed.
  139. //
  140. // Inspect the CLocTypeId and CLocResId of the CLocItem to determine
  141. // if this is a CLocItem for this sub parser. The file pointer for
  142. // p32File is positioned at the beginning of the resource if you need
  143. // to read some of the reasource to decide.
  144. //
  145. //------------------------------------------------------------------------------
  146. STDMETHODIMP_(CResObj*)
  147. CLocImpResObj::CreateResObj(
  148. C32File * p32File,
  149. CLocItem * pLocItem,
  150. DWORD dwSize,
  151. void* pvHeader)
  152. {
  153. UNREFERENCED_PARAMETER(p32File);
  154. UNREFERENCED_PARAMETER(dwSize);
  155. UNREFERENCED_PARAMETER(pvHeader);
  156. CResObj* pObj = NULL;
  157. try
  158. {
  159. //TODO: Add compare code to decide if this item
  160. // is for you.
  161. //The TypeID and ResID are set in the LocItem
  162. //Type ID is the resource type and ResId is the ID
  163. //from the image or resource file.
  164. // For this sample resource type "INIFILE" has
  165. // a ini file in it that we will parse.
  166. CPascalString pasType;
  167. if (pLocItem->GetUniqueId().GetTypeId().GetId(pasType))
  168. {
  169. // There is a string type.
  170. // If you wanted to look for a numeric type, you would
  171. // pass a DWORD to the GetId call above.
  172. if (L"MOFDATA" == pasType)
  173. {
  174. pObj = new CSampleResObj(pLocItem, dwSize, pvHeader);
  175. }
  176. }
  177. }
  178. catch (CException* pE)
  179. {
  180. LTTRACE("%s building a CResObj",
  181. pE->GetRuntimeClass()->m_lpszClassName);
  182. pObj = NULL;
  183. pE->Delete();
  184. }
  185. return pObj;
  186. }
  187. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  188. //
  189. // Build any data objects used over the life of the file object.
  190. // Sub parsers don't create the files so this function and
  191. // OnDestroyWin32File gives the sub parsers a chance to scope data
  192. // on the life of the file.
  193. //
  194. //------------------------------------------------------------------------------
  195. STDMETHODIMP_(void)
  196. CLocImpResObj::OnCreateWin32File(C32File* p32File)
  197. {
  198. // Force our entry in the sub data storage to be null.
  199. // This is used in the SetParent function of the sample
  200. // resource object
  201. p32File->SetSubData(pidBMOF, NULL); //TODO: change to the real
  202. //parser ID
  203. }
  204. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  205. //
  206. // The C32File object is about to be destroyed.
  207. //
  208. //------------------------------------------------------------------------------
  209. STDMETHODIMP_(void)
  210. CLocImpResObj::OnDestroyWin32File(C32File* p32File)
  211. {
  212. // Free the pointer if it is there
  213. //TODO: change to use the real parser ID
  214. CLocItem* pItem = (CLocItem*)p32File->GetSubData(pidBMOF);
  215. if (NULL != pItem)
  216. {
  217. LTASSERTONLY(pItem->AssertValid());
  218. delete pItem;
  219. p32File->SetSubData(pidBMOF, NULL); //TODO: change to the real
  220. //parser ID
  221. }
  222. }
  223. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  224. //
  225. // Called before enumeration
  226. //
  227. //------------------------------------------------------------------------------
  228. STDMETHODIMP_(BOOL)
  229. CLocImpResObj::OnBeginEnumerate(C32File*)
  230. {
  231. return TRUE;
  232. }
  233. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  234. //
  235. // Called after enumeration. The BOOL is TRUE on successful enumerate
  236. //
  237. //------------------------------------------------------------------------------
  238. STDMETHODIMP_(BOOL)
  239. CLocImpResObj::OnEndEnumerate(C32File*, BOOL)
  240. {
  241. return TRUE;
  242. }
  243. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  244. //
  245. // Called before Generate
  246. //
  247. //------------------------------------------------------------------------------
  248. STDMETHODIMP_(BOOL)
  249. CLocImpResObj::OnBeginGenerate(C32File*)
  250. {
  251. return TRUE;
  252. }
  253. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  254. //
  255. // Called after generate. The BOOL is TRUE if the generate
  256. // was successful
  257. //
  258. //------------------------------------------------------------------------------
  259. STDMETHODIMP_(BOOL)
  260. CLocImpResObj::OnEndGenerate(C32File*, BOOL)
  261. {
  262. return TRUE;
  263. }
  264. #ifdef _DEBUG
  265. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  266. //
  267. // Assert this object is valid
  268. //
  269. //------------------------------------------------------------------------------
  270. void
  271. CLocImpResObj::AssertValid(void)
  272. const
  273. {
  274. CLObject::AssertValid();
  275. //Bump up this check if needed.
  276. LTASSERT(m_ulRefCount >= 0 || m_ulRefCount < 100);
  277. LTASSERT(NULL != m_IBinary.m_pParent);
  278. LTASSERT(NULL != m_IParser.m_pParent);
  279. //TODO: ASSERT any other member data objects
  280. //Note: use LTASSERT instead of ASSERT
  281. }
  282. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  283. //
  284. // Dump the contents of this object
  285. //
  286. //------------------------------------------------------------------------------
  287. void
  288. CLocImpResObj::Dump(
  289. CDumpContext &dc)
  290. const
  291. {
  292. CLObject::Dump(dc);
  293. dc << _T("CLocImpResObj\n");
  294. }
  295. #endif // _DEBUG
  296. // ILocBinary interface
  297. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  298. //
  299. // Constructor and member init
  300. //
  301. //------------------------------------------------------------------------------
  302. CLocImpResObj::CLocImpBinary::CLocImpBinary()
  303. {
  304. m_pParent = NULL;
  305. }
  306. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  307. //
  308. // Destructor and clean up
  309. //
  310. //------------------------------------------------------------------------------
  311. CLocImpResObj::CLocImpBinary::~CLocImpBinary()
  312. {
  313. DEBUGONLY(AssertValid());
  314. }
  315. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  316. //
  317. // Add to the reference count
  318. //
  319. //------------------------------------------------------------------------------
  320. STDMETHODIMP_(ULONG)
  321. CLocImpResObj::CLocImpBinary::AddRef(void)
  322. {
  323. DEBUGONLY(AssertValid());
  324. return m_pParent->AddRef();
  325. }
  326. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  327. //
  328. // subtract from the reference count
  329. //
  330. //------------------------------------------------------------------------------
  331. STDMETHODIMP_(ULONG)
  332. CLocImpResObj::CLocImpBinary::Release(void)
  333. {
  334. DEBUGONLY(AssertValid());
  335. return m_pParent->Release();
  336. }
  337. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  338. //
  339. // Query for the requested interface
  340. //
  341. //------------------------------------------------------------------------------
  342. STDMETHODIMP
  343. CLocImpResObj::CLocImpBinary::QueryInterface(
  344. REFIID iid,
  345. LPVOID *ppvObj)
  346. {
  347. DEBUGONLY(AssertValid());
  348. return m_pParent->QueryInterface(iid, ppvObj);
  349. }
  350. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  351. //
  352. // Assert this interface is valie
  353. //
  354. //------------------------------------------------------------------------------
  355. STDMETHODIMP_(void)
  356. CLocImpResObj::CLocImpBinary::AssertValidInterface()
  357. CONST_METHOD
  358. {
  359. DEBUGONLY(AssertValid());
  360. DEBUGONLY(m_pParent->AssertValid());
  361. }
  362. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  363. //
  364. // Create a Binary object.
  365. // Sub parser IDs are only in the LOWORD
  366. //
  367. //-----------------------------------------------------------------------------
  368. STDMETHODIMP_(BOOL)
  369. CLocImpResObj::CLocImpBinary::CreateBinaryObject(BinaryId ID,
  370. CLocBinary *REFERENCE pBinary)
  371. {
  372. DEBUGONLY(AssertValid());
  373. BOOL bRet = FALSE;
  374. pBinary = NULL;
  375. try
  376. {
  377. switch (LOWORD(ID))
  378. {
  379. case btBMOF: //TODO: change to the real ID
  380. pBinary = new CSampleBinary;
  381. bRet = TRUE;
  382. break;
  383. default:
  384. LTASSERT(0 && "Unknown binary ID");
  385. break;
  386. }
  387. }
  388. catch (CException* pE)
  389. {
  390. LTTRACE("%s in CreateBinaryObject",
  391. pE->GetRuntimeClass()->m_lpszClassName);
  392. if (NULL != pBinary)
  393. {
  394. delete pBinary;
  395. }
  396. bRet = FALSE;
  397. pE->Delete();
  398. }
  399. catch (...)
  400. {
  401. LTTRACE("Unknown Exception in CreateBinaryObject");
  402. if (NULL != pBinary)
  403. {
  404. delete pBinary;
  405. }
  406. bRet = FALSE;
  407. }
  408. return bRet;
  409. }
  410. #ifdef _DEBUG
  411. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  412. //
  413. // Assert that the object is valid
  414. //
  415. //------------------------------------------------------------------------------
  416. void
  417. CLocImpResObj::CLocImpBinary::AssertValid(void)
  418. const
  419. {
  420. CLObject::AssertValid();
  421. LTASSERT(NULL != m_pParent);
  422. }
  423. //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  424. //
  425. // Dump the contents of the object
  426. //
  427. //------------------------------------------------------------------------------
  428. void
  429. CLocImpResObj::CLocImpBinary::Dump(
  430. CDumpContext &dc)
  431. const
  432. {
  433. CLObject::Dump(dc);
  434. dc << _T("CLocImpResObj::CLocImpBinary\n");
  435. dc << _T("m_pParent=");
  436. dc << (void*)m_pParent;
  437. dc << _T("\n");
  438. }
  439. #endif // _DEBUG