Leaked source code of windows server 2003
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.

454 lines
16 KiB

  1. // Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1993 Microsoft Corporation,
  3. // All rights reserved.
  4. // This source code is only intended as a supplement to the
  5. // Microsoft Foundation Classes Reference and Microsoft
  6. // QuickHelp and/or WinHelp documentation provided with the library.
  7. // See these sources for detailed information regarding the
  8. // Microsoft Foundation Classes product.
  9. #ifndef __AFXDISP_H__
  10. #define __AFXDISP_H__
  11. #ifndef __AFXWIN_H__
  12. #include <afxwin.h>
  13. #endif
  14. // include OLE 2.0 headers
  15. #define FARSTRUCT
  16. #include <compobj.h>
  17. #include <scode.h>
  18. #include <storage.h>
  19. #include <dispatch.h>
  20. #include <stddef.h>
  21. /////////////////////////////////////////////////////////////////////////////
  22. // AFXDISP - MFC IDispatch & ClassFactory support
  23. // Classes declared in this file
  24. //CException
  25. class COleException; // caught by client or server
  26. class COleDispatchException; // special exception for IDispatch calls
  27. //CCmdTarget
  28. class COleObjectFactory; // glue for IClassFactory -> runtime class
  29. class COleTemplateServer; // server documents using CDocTemplate
  30. class COleDispatchDriver; // helper class to call IDispatch
  31. /////////////////////////////////////////////////////////////////////////////
  32. // AFXDLL support
  33. #undef AFXAPP_DATA
  34. #define AFXAPP_DATA AFXAPIEX_DATA
  35. /////////////////////////////////////////////////////////////////////////////
  36. // OLE 2.0 COM (Component Object Model) implementation infastructure
  37. // - data driven QueryInterface
  38. // - standard implementation of aggregate AddRef and Release)
  39. // (see CCmdTarget in AFXWIN.H for more information)
  40. #define METHOD_PROLOGUE(theClass, localClass) \
  41. theClass* pThis = ((theClass*)((char*)_AfxGetPtrFromFarPtr(this) - \
  42. offsetof(theClass, m_x##localClass))); \
  43. #define BEGIN_INTERFACE_PART(localClass, iface) \
  44. class FAR X##localClass : public iface \
  45. { \
  46. public: \
  47. STDMETHOD_(ULONG, AddRef)(); \
  48. STDMETHOD_(ULONG, Release)(); \
  49. STDMETHOD(QueryInterface)(REFIID iid, LPVOID far* ppvObj); \
  50. // Note: Inserts the rest of OLE functionality between these two macros,
  51. // depending upon the interface that is being implemented. It is not
  52. // necessary to include AddRef, Release, and QueryInterface since those
  53. // member functions are declared by the macro.
  54. #define END_INTERFACE_PART(localClass) \
  55. } m_x##localClass; \
  56. friend class X##localClass; \
  57. #define BEGIN_INTERFACE_MAP(theClass, theBase) \
  58. AFX_INTERFACEMAP FAR* theClass::GetInterfaceMap() const \
  59. { return &theClass::interfaceMap; } \
  60. AFX_INTERFACEMAP BASED_CODE theClass::interfaceMap = \
  61. { &(theBase::interfaceMap), theClass::_interfaceEntries, }; \
  62. AFX_INTERFACEMAP_ENTRY BASED_CODE theClass::_interfaceEntries[] = \
  63. {
  64. #define INTERFACE_PART(theClass, iid, localClass) \
  65. { &iid, offsetof(theClass, m_x##localClass) }, \
  66. #define INTERFACE_AGGREGATE(theClass, theAggr) \
  67. { NULL, offsetof(theClass, theAggr) }, \
  68. #define END_INTERFACE_MAP() \
  69. { NULL, (size_t)-1 } \
  70. }; \
  71. /////////////////////////////////////////////////////////////////////////////
  72. // COleException - unexpected or rare OLE error returned
  73. class COleException : public CException
  74. {
  75. DECLARE_DYNAMIC(COleException)
  76. public:
  77. SCODE m_sc;
  78. static SCODE PASCAL Process(const CException* pAnyException);
  79. // Implementation (use AfxThrowOleException to create)
  80. COleException();
  81. };
  82. void AFXAPI AfxThrowOleException(SCODE sc);
  83. void AFXAPI AfxThrowOleException(HRESULT hr);
  84. /////////////////////////////////////////////////////////////////////////////
  85. // IDispatch specific exception
  86. class COleDispatchException : public CException
  87. {
  88. DECLARE_DYNAMIC(COleDispatchException)
  89. public:
  90. // Attributes
  91. WORD m_wCode; // error code (specific to IDispatch implementation)
  92. CString m_strDescription; // human readable description of the error
  93. DWORD m_dwHelpContext; // help context for error
  94. // usually empty in application which creates it (eg. servers)
  95. CString m_strHelpFile; // help file to use with m_dwHelpContext
  96. CString m_strSource; // source of the error (name of server)
  97. // Implementation
  98. public:
  99. COleDispatchException(LPCSTR lpszDescription, UINT nHelpID, WORD wCode);
  100. static void PASCAL Process(
  101. EXCEPINFO FAR* pInfo, const CException* pAnyException);
  102. SCODE m_scError; // SCODE describing the error
  103. };
  104. void AFXAPI AfxThrowOleDispatchException(WORD wCode, LPCSTR lpszDescription,
  105. UINT nHelpID = 0);
  106. void AFXAPI AfxThrowOleDispatchException(WORD wCode, UINT nDescriptionID,
  107. UINT nHelpID = -1);
  108. /////////////////////////////////////////////////////////////////////////////
  109. // Macros for CCmdTarget IDispatchable classes
  110. #define BEGIN_DISPATCH_MAP(theClass, baseClass) \
  111. AFX_DISPMAP FAR* theClass::GetDispatchMap() const \
  112. { return &theClass::dispatchMap; } \
  113. AFX_DISPMAP BASED_CODE theClass::dispatchMap = \
  114. { &(baseClass::dispatchMap), theClass::_dispatchEntries }; \
  115. AFX_DISPMAP_ENTRY BASED_CODE theClass::_dispatchEntries[] = \
  116. {
  117. #define END_DISPATCH_MAP() \
  118. { VTS_NONE, DISPID_UNKNOWN, VTS_NONE, VT_VOID, \
  119. (AFX_PMSG)NULL, (AFX_PMSG)NULL, (size_t)-1 } }; \
  120. // parameter types: by value VTs
  121. #define VTS_I2 "\x02" // a 'short'
  122. #define VTS_I4 "\x03" // a 'long'
  123. #define VTS_R4 "\x04" // a 'float'
  124. #define VTS_R8 "\x05" // a 'double'
  125. #define VTS_CY "\x06" // a 'const CY FAR&'
  126. #define VTS_DATE "\x07" // a 'DATE'
  127. #define VTS_BSTR "\x08" // an 'LPCSTR'
  128. #define VTS_DISPATCH "\x09" // an 'IDispatch FAR*'
  129. #define VTS_SCODE "\x0A" // an 'SCODE'
  130. #define VTS_BOOL "\x0B" // a 'BOOL'
  131. #define VTS_VARIANT "\x0C" // a 'const VARIANT FAR&'
  132. #define VTS_UNKNOWN "\x0D" // an 'IUnknown FAR*'
  133. // parameter types: by reference VTs
  134. #define VTS_PI2 "\x42" // a 'short FAR*'
  135. #define VTS_PI4 "\x43" // a 'long FAR*'
  136. #define VTS_PR4 "\x44" // a 'float FAR*'
  137. #define VTS_PR8 "\x45" // a 'double FAR*'
  138. #define VTS_PCY "\x46" // a 'CY FAR*'
  139. #define VTS_PDATE "\x47" // a 'DATE FAR*'
  140. #define VTS_PBSTR "\x48" // a 'BSTR FAR*'
  141. #define VTS_PDISPATCH "\x49" // an 'IDispatch FAR* FAR*'
  142. #define VTS_PSCODE "\x4A" // an 'SCODE FAR*'
  143. #define VTS_PBOOL "\x4B" // a 'BOOL FAR*'
  144. #define VTS_PVARIANT "\x4C" // a 'VARIANT FAR*'
  145. #define VTS_PUNKNOWN "\x4D" // an 'IUnknown FAR* FAR*'
  146. // special VT_ and VTS_ values
  147. #define VTS_NONE "" // used for members with 0 params
  148. #define VT_MFCVALUE 0xFFF // special value for DISPID_VALUE
  149. #define VT_MFCBYREF 0x40 // indicates VT_BYREF type
  150. #define VT_MFCMARKER 0xFF // delimits named parameters
  151. // these DISP_ macros cause the framework to generate the DISPID
  152. #define DISP_FUNCTION(theClass, szExternalName, pfnMember, vtRetVal, vtsParams) \
  153. { szExternalName, DISPID_UNKNOWN, vtsParams, vtRetVal, \
  154. (AFX_PMSG)(void (theClass::*)(void))pfnMember, (AFX_PMSG)0, 0 }, \
  155. #define DISP_PROPERTY(theClass, szExternalName, memberName, vtPropType) \
  156. { szExternalName, DISPID_UNKNOWN, "", vtPropType, (AFX_PMSG)0, (AFX_PMSG)0, \
  157. offsetof(theClass, memberName) }, \
  158. #define DISP_PROPERTY_NOTIFY(theClass, szExternalName, memberName, pfnAfterSet, vtPropType) \
  159. { szExternalName, DISPID_UNKNOWN, "", vtPropType, (AFX_PMSG)0, \
  160. (AFX_PMSG)(void (theClass::*)(void))pfnAfterSet, \
  161. offsetof(theClass, memberName) }, \
  162. #define DISP_PROPERTY_EX(theClass, szExternalName, pfnGet, pfnSet, vtPropType) \
  163. { szExternalName, DISPID_UNKNOWN, "", vtPropType, \
  164. (AFX_PMSG)(void (theClass::*)(void))pfnGet, \
  165. (AFX_PMSG)(void (theClass::*)(void))pfnSet, 0 }, \
  166. #define DISP_PROPERTY_PARAM(theClass, szExternalName, pfnGet, pfnSet, vtPropType, vtsParams) \
  167. { szExternalName, DISPID_UNKNOWN, vtsParams, vtPropType, \
  168. (AFX_PMSG)(void (theClass::*)(void))pfnGet, \
  169. (AFX_PMSG)(void (theClass::*)(void))pfnSet, 0 }, \
  170. // these DISP_ macros allow the app to determine the DISPID
  171. #define DISP_FUNCTION_ID(theClass, szExternalName, dispid, pfnMember, vtRetVal, vtsParams) \
  172. { szExternalName, dispid, vtsParams, vtRetVal, \
  173. (AFX_PMSG)(void (theClass::*)(void))pfnMember, (AFX_PMSG)0, 0 }, \
  174. #define DISP_PROPERTY_ID(theClass, szExternalName, dispid, memberName, vtPropType) \
  175. { szExternalName, dispid, "", vtPropType, (AFX_PMSG)0, (AFX_PMSG)0, \
  176. offsetof(theClass, memberName) }, \
  177. #define DISP_PROPERTY_NOTIFY_ID(theClass, szExternalName, dispid, memberName, pfnAfterSet, vtPropType) \
  178. { szExternalName, dispid, "", vtPropType, (AFX_PMSG)0, \
  179. (AFX_PMSG)(void (theClass::*)(void))pfnAfterSet, \
  180. offsetof(theClass, memberName) }, \
  181. #define DISP_PROPERTY_EX_ID(theClass, szExternalName, dispid, pfnGet, pfnSet, vtPropType) \
  182. { szExternalName, dispid, "", vtPropType, \
  183. (AFX_PMSG)(void (theClass::*)(void))pfnGet, \
  184. (AFX_PMSG)(void (theClass::*)(void))pfnSet, 0 }, \
  185. #define DISP_PROPERTY_PARAM_ID(theClass, szExternalName, dispid, pfnGet, pfnSet, vtPropType, vtsParams) \
  186. { szExternalName, dispid, vtsParams, vtPropType, \
  187. (AFX_PMSG)(void (theClass::*)(void))pfnGet, \
  188. (AFX_PMSG)(void (theClass::*)(void))pfnSet, 0 }, \
  189. // the DISP_DEFVALUE is a special case macro that creates an alias for DISPID_VALUE
  190. #define DISP_DEFVALUE(theClass, szExternalName) \
  191. { szExternalName, DISPID_UNKNOWN, "", VT_MFCVALUE, \
  192. (AFX_PMSG)0, (AFX_PMSG)0, 0 }, \
  193. #define DISP_DEFVALUE_ID(theClass, dispid) \
  194. { "", dispid, "", VT_MFCVALUE, (AFX_PMSG)0, (AFX_PMSG)0, 0 }, \
  195. /////////////////////////////////////////////////////////////////////////////
  196. // Macros for creating "creatable" automation classes.
  197. #define DECLARE_OLECREATE(class_name) \
  198. protected: \
  199. static COleObjectFactory NEAR factory; \
  200. static const GUID CDECL BASED_CODE guid;
  201. #define IMPLEMENT_OLECREATE(class_name, external_name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
  202. static const char BASED_CODE _szProgID_##class_name[] = external_name; \
  203. COleObjectFactory NEAR class_name::factory(class_name::guid, \
  204. RUNTIME_CLASS(class_name), FALSE, _szProgID_##class_name); \
  205. const GUID CDECL BASED_CODE class_name::guid = \
  206. { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } };
  207. /////////////////////////////////////////////////////////////////////////////
  208. // Helper class for driving IDispatch
  209. class COleDispatchDriver
  210. {
  211. // Constructors
  212. public:
  213. COleDispatchDriver();
  214. // Operations
  215. BOOL CreateDispatch(REFCLSID clsid, COleException* pError = NULL);
  216. BOOL CreateDispatch(LPCSTR lpszProgID, COleException* pError = NULL);
  217. void AttachDispatch(LPDISPATCH lpDispatch, BOOL bAutoRelease = TRUE);
  218. LPDISPATCH DetachDispatch();
  219. // detach and get ownership of m_lpDispatch
  220. void ReleaseDispatch();
  221. // helpers for IDispatch::Invoke
  222. void InvokeHelper(DISPID dwDispID, WORD wFlags,
  223. VARTYPE vtRet, void* pvRet, const BYTE FAR* pbParamInfo, ...);
  224. void SetProperty(DISPID dwDispID, VARTYPE vtProp, ...);
  225. void GetProperty(DISPID dwDispID, VARTYPE vtProp, void* pvProp) const;
  226. // Implementation
  227. public:
  228. LPDISPATCH m_lpDispatch;
  229. ~COleDispatchDriver();
  230. void InvokeHelperV(DISPID dwDispID, WORD wFlags, VARTYPE vtRet,
  231. void* pvRet, const BYTE FAR* pbParamInfo, va_list argList);
  232. protected:
  233. BOOL m_bAutoRelease; // TRUE if destructor should call Release
  234. private:
  235. // Disable the copy constructor and assignment by default so you will get
  236. // compiler errors instead of unexpected behaviour if you pass objects
  237. // by value or assign objects.
  238. COleDispatchDriver(const COleDispatchDriver&); // no implementation
  239. void operator=(const COleDispatchDriver&); // no implementation
  240. };
  241. /////////////////////////////////////////////////////////////////////////////
  242. // Class Factory implementation (binds OLE class factory -> runtime class)
  243. // (all specific class factories derive from this class factory)
  244. class COleObjectFactory : public CCmdTarget
  245. {
  246. DECLARE_DYNAMIC(COleObjectFactory)
  247. // Construction
  248. public:
  249. COleObjectFactory(REFCLSID clsid, CRuntimeClass* pRuntimeClass,
  250. BOOL bMultiInstance, LPCSTR lpszProgID);
  251. // Attributes
  252. BOOL IsRegistered() const;
  253. REFCLSID GetClassID() const;
  254. // Operations
  255. BOOL Register();
  256. void Revoke();
  257. void UpdateRegistry(LPCSTR lpszProgID = NULL);
  258. // default uses m_lpszProgID if not NULL
  259. static BOOL PASCAL RegisterAll();
  260. static void PASCAL RevokeAll();
  261. static void PASCAL UpdateRegistryAll();
  262. // Overridables
  263. protected:
  264. virtual CCmdTarget* OnCreateObject();
  265. // Implementation
  266. public:
  267. virtual ~COleObjectFactory();
  268. #ifdef _DEBUG
  269. void AssertValid() const;
  270. void Dump(CDumpContext& dc) const;
  271. #endif
  272. protected:
  273. COleObjectFactory* m_pNextFactory; // list of factories maintained
  274. DWORD m_dwRegister; // registry identifier
  275. CLSID m_clsid; // registered class ID
  276. CRuntimeClass* m_pRuntimeClass; // runtime class of CCmdTarget derivative
  277. BOOL m_bMultiInstance; // multiple instance?
  278. LPCSTR m_lpszProgID; // human readable class ID
  279. // Interface Maps
  280. protected:
  281. BEGIN_INTERFACE_PART(ClassFactory, IClassFactory)
  282. STDMETHOD(CreateInstance)(LPUNKNOWN, REFIID, LPVOID FAR*);
  283. STDMETHOD(LockServer)(BOOL);
  284. END_INTERFACE_PART(ClassFactory)
  285. DECLARE_INTERFACE_MAP()
  286. #ifdef _AFXCTL
  287. friend HRESULT AfxDllGetClassObject(REFCLSID, REFIID, LPVOID FAR*);
  288. #endif
  289. friend HRESULT STDAPICALLTYPE
  290. DllGetClassObject(REFCLSID, REFIID, LPVOID FAR*);
  291. };
  292. //////////////////////////////////////////////////////////////////////////////
  293. // COleTemplateServer - COleObjectFactory using CDocTemplates
  294. // This enumeration is used in AfxOleRegisterServerClass to pick the
  295. // correct registration entries given the application type.
  296. enum OLE_APPTYPE
  297. {
  298. OAT_INPLACE_SERVER = 0, // server has full server user-interface
  299. OAT_SERVER = 1, // server supports only embedding
  300. OAT_CONTAINER = 2, // container supports links to embeddings
  301. OAT_DISPATCH_OBJECT = 3, // IDispatch capable object
  302. };
  303. class COleTemplateServer : public COleObjectFactory
  304. {
  305. // Constructors
  306. public:
  307. COleTemplateServer();
  308. // Operations
  309. void ConnectTemplate(REFCLSID clsid, CDocTemplate* pDocTemplate,
  310. BOOL bMultiInstance);
  311. // set doc template after creating it in InitInstance
  312. void UpdateRegistry(OLE_APPTYPE nAppType = OAT_INPLACE_SERVER,
  313. LPCSTR FAR* rglpszRegister = NULL, LPCSTR FAR* rglpszOverwrite = NULL);
  314. // may want to UpdateRegistry if not run with /Embedded
  315. // Implementation
  316. protected:
  317. virtual CCmdTarget* OnCreateObject();
  318. CDocTemplate* m_pDocTemplate;
  319. private:
  320. void UpdateRegistry(LPCSTR lpszProgID);
  321. // hide base class version of UpdateRegistry
  322. };
  323. /////////////////////////////////////////////////////////////////////////////
  324. // helper functions for system registry maintenance
  325. // Helper to register server in case of no .REG file loaded
  326. BOOL AFXAPI AfxOleRegisterServerClass(
  327. REFCLSID clsid, LPCSTR lpszClassName,
  328. LPCSTR lpszShortTypeName, LPCSTR lpszLongTypeName,
  329. OLE_APPTYPE nAppType = OAT_SERVER,
  330. LPCSTR FAR* rglpszRegister = NULL, LPCSTR FAR* rglpszOverwrite = NULL);
  331. // AfxOleRegisterHelper is a worker function used by AfxOleRegisterServerClass
  332. // (available for advanced registry work)
  333. BOOL AFXAPI AfxOleRegisterHelper(LPCSTR FAR* rglpszRegister,
  334. LPCSTR FAR* rglpszSymbols, int nSymbols, BOOL bReplace);
  335. /////////////////////////////////////////////////////////////////////////////
  336. // Init & Term helpers
  337. BOOL AFXAPI AfxOleInit();
  338. void CALLBACK AfxOleTerm();
  339. /////////////////////////////////////////////////////////////////////////////
  340. // Global functions (may be overridden by replacing module OLELOCK?.CPP)
  341. void AFXAPI AfxOleOnReleaseAllObjects();
  342. BOOL AFXAPI AfxOleCanExitApp();
  343. void AFXAPI AfxOleLockApp();
  344. void AFXAPI AfxOleUnlockApp();
  345. void AFXAPI AfxOleSetUserCtrl(BOOL bUserCtrl);
  346. BOOL AFXAPI AfxOleGetUserCtrl();
  347. /////////////////////////////////////////////////////////////////////////////
  348. // Inline function declarations
  349. typedef CY CURRENCY; // compatibility with 32-bit
  350. #ifdef _AFX_ENABLE_INLINES
  351. #define _AFXDISP_INLINE inline
  352. #include <afxole.inl>
  353. #undef _AFXDISP_INLINE
  354. #endif
  355. #undef AFXAPP_DATA
  356. #define AFXAPP_DATA NEAR
  357. #endif //__AFXDISP_H__
  358. /////////////////////////////////////////////////////////////////////////////