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.

61 lines
1.6 KiB

  1. /*
  2. * registrar.hxx
  3. *
  4. *
  5. * Copyright (c) 1998 Microsoft Corporation
  6. *
  7. * PURPOSE: Defines the CCoCreateInstanceRef class
  8. *
  9. *
  10. * OWNER: vivekj
  11. */
  12. /* template CCoCreateInstanceRef
  13. *
  14. * PURPOSE: Encapsulates calling CoCreateInstance to create a pointer.
  15. * The first time the object is referenced, CoCreateInstance is
  16. * called. Subsequent references return the pointer.
  17. *
  18. *
  19. * PARAMETERS: T: The interface class, eg IRegistrar
  20. * rclsid: The class ID, eg CLSID_Registrar
  21. * riid: The interface ID eg IID_IRegistrar
  22. *
  23. */
  24. #ifndef _REGISTRAR
  25. #define _REGISTRAR
  26. template<class T, const IID* pclsid, const IID* piid>
  27. class CCoCreateInstanceRef
  28. {
  29. typedef CComQIPtr<T, piid> t_ptr;
  30. t_ptr m_ip;
  31. public:
  32. operator T &()
  33. {
  34. SC sc;
  35. if(!m_ip)
  36. {
  37. sc = CoCreateInstance(*pclsid, NULL, CLSCTX_INPROC_SERVER, *piid, (void **)&m_ip);
  38. if(sc)
  39. goto Error;
  40. }
  41. Cleanup:
  42. return *(static_cast<T *>(m_ip));
  43. Error:
  44. throw(sc);
  45. goto Cleanup;
  46. }
  47. };
  48. #define DEFINE_COCREATEINSTANCEREF(_a, _b) \
  49. template CCoCreateInstanceRef<I##_a, &_b, &IID_I##_a>; \
  50. typedef CCoCreateInstanceRef<I##_a, &_b, &IID_I##_a> C##_a;
  51. // Create the CRegistrar class.
  52. DEFINE_COCREATEINSTANCEREF(Registrar, CLSID_Registrar)
  53. #endif // _REGISTRAR