dispatchCallback.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #ifndef NULLSOFT_DISPATCHCALLBACK_H
  2. #define NULLSOFT_DISPATCHCALLBACK_H
  3. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  4. #pragma once
  5. #endif
  6. #include <windows.h>
  7. #include <oleauto.h>
  8. #include <vector>
  9. typedef void (*DispatchCallbackNotifyFunc)(IDispatch* /*dispatch*/, void* /*param*/);
  10. typedef void (*DispatchCallbackFreeFunc)(void* /*param*/);
  11. class DispatchCallback
  12. {
  13. protected:
  14. DispatchCallback();
  15. ~DispatchCallback();
  16. public:
  17. static HRESULT CreateInstance(IDispatch *dispatch,
  18. DispatchCallback **instance);
  19. public:
  20. unsigned long AddRef();
  21. unsigned long Release();
  22. IDispatch *GetDispatch();
  23. unsigned long GetThreadId();
  24. HANDLE GetThreadHandle();
  25. protected:
  26. unsigned long ref;
  27. IDispatch *dispatch;
  28. unsigned long threadId;
  29. HANDLE threadHandle;
  30. };
  31. class DispatchCallbackEnum
  32. {
  33. protected:
  34. DispatchCallbackEnum();
  35. ~DispatchCallbackEnum();
  36. public:
  37. static HRESULT CreateInstance(DispatchCallback **objects,
  38. size_t count,
  39. DispatchCallbackEnum **instance);
  40. public:
  41. unsigned long AddRef();
  42. unsigned long Release();
  43. public:
  44. HRESULT Next(DispatchCallback **buffer, size_t bufferMax, size_t *fetched);
  45. HRESULT Reset(void);
  46. HRESULT Skip(size_t count);
  47. HRESULT GetCount(size_t *count);
  48. HRESULT Notify(DispatchCallbackNotifyFunc notifyCb, DispatchCallbackFreeFunc freeCb, void *param);
  49. protected:
  50. unsigned long ref;
  51. DispatchCallback **buffer;
  52. size_t size;
  53. size_t cursor;
  54. };
  55. class DispatchCallbackStore
  56. {
  57. public:
  58. DispatchCallbackStore();
  59. ~DispatchCallbackStore();
  60. public:
  61. void Lock();
  62. void Unlock();
  63. CRITICAL_SECTION *GetLock();
  64. HRESULT Register(IDispatch *dispatch);
  65. HRESULT Unregister(IDispatch *dispatch);
  66. void UnregisterAll();
  67. HRESULT Enumerate(DispatchCallbackEnum **enumerator);
  68. /* Helpers*/
  69. HRESULT RegisterFromDispParam(DISPPARAMS *pdispparams, unsigned int position, unsigned int *puArgErr);
  70. HRESULT UnregisterFromDispParam(DISPPARAMS *pdispparams, unsigned int position, unsigned int *puArgErr);
  71. HRESULT Notify(DispatchCallbackNotifyFunc notifyCb, DispatchCallbackFreeFunc freeCb, void *param);
  72. protected:
  73. typedef std::vector<DispatchCallback*> CallbackList;
  74. protected:
  75. CRITICAL_SECTION lock;
  76. CallbackList list;
  77. };
  78. /* Internals */
  79. class DispatchCallbackApc
  80. {
  81. protected:
  82. DispatchCallbackApc();
  83. ~DispatchCallbackApc();
  84. public:
  85. static HRESULT CreateInstance(DispatchCallbackNotifyFunc notifyCb,
  86. DispatchCallbackFreeFunc freeCb,
  87. void *param,
  88. DispatchCallbackApc **instance);
  89. public:
  90. unsigned long AddRef();
  91. unsigned long Release();
  92. HRESULT Call(IDispatch *dispatch);
  93. HRESULT Queue(HANDLE threadHandle, IDispatch *dispatch);
  94. private:
  95. static void CALLBACK QueueApcCallback(ULONG_PTR user);
  96. protected:
  97. unsigned long ref;
  98. DispatchCallbackNotifyFunc notifyCb;
  99. DispatchCallbackFreeFunc freeCb;
  100. void *param;
  101. };
  102. class DispatchCallbackApcParam
  103. {
  104. public:
  105. DispatchCallbackApcParam(IDispatch *dispatch, DispatchCallbackApc *apc);
  106. ~DispatchCallbackApcParam();
  107. public:
  108. IDispatch *GetDispatch();
  109. DispatchCallbackApc *GetApc();
  110. protected:
  111. IDispatch *dispatch;
  112. DispatchCallbackApc *apc;
  113. };
  114. #endif