JSAPI_CallbackParameters.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #include <dispex.h>
  3. #include <map>
  4. #include <string>
  5. /*
  6. This is just a generic property map
  7. wchar_t * -> VARIANT
  8. Since it stores added properties consecutively (no sorting on property name)
  9. it should be safe to add properties after-the-fact, but it's not recommended
  10. not thread safe, so don't add properties on a different than than whomever is reading them via IDispatch
  11. */
  12. namespace JSAPI
  13. {
  14. class CallbackParameters : public IDispatchEx
  15. {
  16. public:
  17. CallbackParameters();
  18. ~CallbackParameters();
  19. STDMETHOD(QueryInterface)(REFIID riid, PVOID *ppvObject);
  20. STDMETHOD_(ULONG, AddRef)(void);
  21. STDMETHOD_(ULONG, Release)(void);
  22. /** Call this to add a new property
  23. ** On object destruction, ~CallbackParameters() will call
  24. **** Release() on any VT_DISPATCH variants
  25. **** SysFreeString() on any VT_BSTR variants
  26. **** TODO on any VT_ARRAY variants
  27. ** so be sure to prepare your data ahead of time (e.g. call AddRef)
  28. ** if you call the helper functions (below) they will do
  29. ** SysAllocString, AddRef, etc for you.
  30. */
  31. void AddProperty(const wchar_t *name, const VARIANT &property);
  32. /** helper functions if you don't want to build your own VARIANT
  33. **/
  34. void AddString(const wchar_t *name, const wchar_t *value);
  35. void AddDispatch(const wchar_t *name, IDispatch *disp); // note: calls AddRef() on your object;
  36. void AddLong(const wchar_t *name, LONG value);
  37. void AddBoolean(const wchar_t *name, bool value);
  38. typedef enum
  39. {
  40. typeBool = 0,
  41. typeString = 1,
  42. typeLong = 2,
  43. typeDispatch = 3,
  44. } PtopertyType;
  45. typedef struct __PropertyTemplate
  46. {
  47. unsigned char type;
  48. const wchar_t *name;
  49. ULONG_PTR value;
  50. } PropertyTemplate;
  51. size_t AddPropertyIndirect(const PropertyTemplate *entries, size_t count);
  52. private:
  53. // *** IDispatch Methods ***
  54. STDMETHOD (GetIDsOfNames)(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid);
  55. STDMETHOD (GetTypeInfo)(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo);
  56. STDMETHOD (GetTypeInfoCount)(unsigned int FAR * pctinfo);
  57. STDMETHOD (Invoke)(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr);
  58. // *** IDispatchEx Methods ***
  59. STDMETHOD (GetDispID)(BSTR bstrName, DWORD grfdex, DISPID *pid);
  60. STDMETHOD (InvokeEx)(DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp, VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller);
  61. STDMETHOD (DeleteMemberByName)(BSTR bstrName, DWORD grfdex) ;
  62. STDMETHOD (DeleteMemberByDispID)(DISPID id);
  63. STDMETHOD (GetMemberProperties)(DISPID id, DWORD grfdexFetch, DWORD *pgrfdex);
  64. STDMETHOD (GetMemberName)(DISPID id, BSTR *pbstrName);
  65. STDMETHOD (GetNextDispID)(DWORD grfdex, DISPID id, DISPID *pid);
  66. STDMETHOD (GetNameSpaceParent)(IUnknown **ppunk);
  67. private:
  68. typedef std::map<std::wstring, VARIANT> ParameterList;
  69. ParameterList params;
  70. volatile LONG refCount;
  71. };
  72. // helper function which calls Invoke(0) with 1 parameter (VT_DISPATCH of your CallbackParameters object)
  73. HRESULT InvokeEvent(JSAPI::CallbackParameters *parameters, IDispatch *invokee);
  74. }