xref: /reactos/dll/win32/ole32/compobj_private.h (revision c2c66aff)
1 /*
2  * Copyright 1995 Martin von Loewis
3  * Copyright 1998 Justin Bradford
4  * Copyright 1999 Francis Beaudet
5  * Copyright 1999 Sylvain St-Germain
6  * Copyright 2002 Marcus Meissner
7  * Copyright 2003 Ove Kåven, TransGaming Technologies
8  * Copyright 2004 Mike Hearn, Rob Shearman, CodeWeavers Inc
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef __WINE_OLE_COMPOBJ_H
26 #define __WINE_OLE_COMPOBJ_H
27 
28 /* All private prototype functions used by OLE will be added to this header file */
29 
30 struct apartment;
31 typedef struct apartment APARTMENT;
32 typedef struct LocalServer LocalServer;
33 
34 DEFINE_OLEGUID( CLSID_DfMarshal, 0x0000030b, 0, 0 );
35 
36 /* signal to stub manager that this is a rem unknown object */
37 #define MSHLFLAGSP_REMUNKNOWN   0x80000000
38 
39 /* Thread-safety Annotation Legend:
40  *
41  * RO    - The value is read only. It never changes after creation, so no
42  *         locking is required.
43  * LOCK  - The value is written to only using Interlocked* functions.
44  * CS    - The value is read or written to inside a critical section.
45  *         The identifier following "CS" is the specific critical section that
46  *         must be used.
47  * MUTEX - The value is read or written to with a mutex held.
48  *         The identifier following "MUTEX" is the specific mutex that
49  *         must be used.
50  */
51 
52 typedef enum ifstub_state
53 {
54     STUBSTATE_NORMAL_MARSHALED,
55     STUBSTATE_NORMAL_UNMARSHALED,
56     STUBSTATE_TABLE_WEAK_MARSHALED,
57     STUBSTATE_TABLE_WEAK_UNMARSHALED,
58     STUBSTATE_TABLE_STRONG,
59 } STUB_STATE;
60 
61 /* an interface stub */
62 struct ifstub
63 {
64     struct list       entry;      /* entry in stub_manager->ifstubs list (CS stub_manager->lock) */
65     IRpcStubBuffer   *stubbuffer; /* RO */
66     IID               iid;        /* RO */
67     IPID              ipid;       /* RO */
68     IUnknown         *iface;      /* RO */
69     MSHLFLAGS         flags;      /* so we can enforce process-local marshalling rules (RO) */
70     IRpcChannelBuffer*chan;       /* channel passed to IRpcStubBuffer::Invoke (RO) */
71 };
72 
73 
74 /* stub managers hold refs on the object and each interface stub */
75 struct stub_manager
76 {
77     struct list       entry;      /* entry in apartment stubmgr list (CS apt->cs) */
78     struct list       ifstubs;    /* list of active ifstubs for the object (CS lock) */
79     CRITICAL_SECTION  lock;
80     APARTMENT        *apt;        /* owning apt (RO) */
81 
82     ULONG             extrefs;    /* number of 'external' references (CS lock) */
83     ULONG             refs;       /* internal reference count (CS apt->cs) */
84     ULONG             weakrefs;   /* number of weak references (CS lock) */
85     OID               oid;        /* apartment-scoped unique identifier (RO) */
86     IUnknown         *object;     /* the object we are managing the stub for (RO) */
87     ULONG             next_ipid;  /* currently unused (LOCK) */
88     OXID_INFO         oxid_info;  /* string binding, ipid of rem unknown and other information (RO) */
89 
90     IExternalConnection *extern_conn;
91 
92     /* We need to keep a count of the outstanding marshals, so we can enforce the
93      * marshalling rules (ie, you can only unmarshal normal marshals once). Note
94      * that these counts do NOT include unmarshalled interfaces, once a stream is
95      * unmarshalled and a proxy set up, this count is decremented.
96      */
97 
98     ULONG             norm_refs;  /* refcount of normal marshals (CS lock) */
99     BOOL              disconnected; /* CoDisconnectObject has been called (CS lock) */
100 };
101 
102 /* imported interface proxy */
103 struct ifproxy
104 {
105   struct list entry;       /* entry in proxy_manager list (CS parent->cs) */
106   struct proxy_manager *parent; /* owning proxy_manager (RO) */
107   LPVOID iface;            /* interface pointer (RO) */
108   STDOBJREF stdobjref;     /* marshal data that represents this object (RO) */
109   IID iid;                 /* interface ID (RO) */
110   LPRPCPROXYBUFFER proxy;  /* interface proxy (RO) */
111   ULONG refs;              /* imported (public) references (LOCK) */
112   IRpcChannelBuffer *chan; /* channel to object (CS parent->cs) */
113 };
114 
115 struct apartment
116 {
117   struct list entry;
118 
119   LONG  refs;              /* refcount of the apartment (LOCK) */
120   BOOL multi_threaded;     /* multi-threaded or single-threaded apartment? (RO) */
121   DWORD tid;               /* thread id (RO) */
122   OXID oxid;               /* object exporter ID (RO) */
123   LONG ipidc;              /* interface pointer ID counter, starts at 1 (LOCK) */
124   CRITICAL_SECTION cs;     /* thread safety */
125   struct list proxies;     /* imported objects (CS cs) */
126   struct list stubmgrs;    /* stub managers for exported objects (CS cs) */
127   BOOL remunk_exported;    /* has the IRemUnknown interface for this apartment been created yet? (CS cs) */
128   LONG remoting_started;   /* has the RPC system been started for this apartment? (LOCK) */
129   struct list loaded_dlls; /* list of dlls loaded by this apartment (CS cs) */
130   DWORD host_apt_tid;      /* thread ID of apartment hosting objects of differing threading model (CS cs) */
131   HWND host_apt_hwnd;      /* handle to apartment window of host apartment (CS cs) */
132   LocalServer *local_server; /* A marshallable object exposing local servers (CS cs) */
133 
134   /* FIXME: OIDs should be given out by RPCSS */
135   OID oidc;                /* object ID counter, starts at 1, zero is invalid OID (CS cs) */
136 
137   /* STA-only fields */
138   HWND win;                /* message window (LOCK) */
139   LPMESSAGEFILTER filter;  /* message filter (CS cs) */
140   BOOL main;               /* is this a main-threaded-apartment? (RO) */
141 };
142 
143 /* this is what is stored in TEB->ReservedForOle */
144 struct oletls
145 {
146     struct apartment *apt;
147     IErrorInfo       *errorinfo;   /* see errorinfo.c */
148     IUnknown         *state;       /* see CoSetState */
149     DWORD             apt_mask;    /* apartment mask (+0Ch on x86) */
150     IInitializeSpy   *spy;         /* The "SPY" from CoInitializeSpy */
151     DWORD            inits;        /* number of times CoInitializeEx called */
152     DWORD            ole_inits;    /* number of times OleInitialize called */
153     GUID             causality_id; /* unique identifier for each COM call */
154     LONG             pending_call_count_client; /* number of client calls pending */
155     LONG             pending_call_count_server; /* number of server calls pending */
156     DWORD            unknown;
157     IObjContext     *context_token; /* (+38h on x86) */
158     IUnknown        *call_state;    /* current call context (+3Ch on x86) */
159     DWORD            unknown2[46];
160     IUnknown        *cancel_object; /* cancel object set by CoSetCancelObject (+F8h on x86) */
161 };
162 
163 
164 /* Global Interface Table Functions */
165 extern IGlobalInterfaceTable *get_std_git(void) DECLSPEC_HIDDEN;
166 extern void release_std_git(void) DECLSPEC_HIDDEN;
167 extern HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv) DECLSPEC_HIDDEN;
168 
169 HRESULT COM_OpenKeyForCLSID(REFCLSID clsid, LPCWSTR keyname, REGSAM access, HKEY *key) DECLSPEC_HIDDEN;
170 HRESULT COM_OpenKeyForAppIdFromCLSID(REFCLSID clsid, REGSAM access, HKEY *subkey) DECLSPEC_HIDDEN;
171 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv) DECLSPEC_HIDDEN;
172 HRESULT FTMarshalCF_Create(REFIID riid, LPVOID *ppv) DECLSPEC_HIDDEN;
173 
174 /* Stub Manager */
175 
176 ULONG stub_manager_int_release(struct stub_manager *This) DECLSPEC_HIDDEN;
177 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs, BOOL tableweak) DECLSPEC_HIDDEN;
178 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL tableweak, BOOL last_unlock_releases) DECLSPEC_HIDDEN;
179 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, REFIID iid,
180      DWORD dest_context, void *dest_context_data, MSHLFLAGS flags) DECLSPEC_HIDDEN;
181 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags) DECLSPEC_HIDDEN;
182 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid) DECLSPEC_HIDDEN;
183 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, IUnknown *object, BOOL alloc) DECLSPEC_HIDDEN;
184 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid) DECLSPEC_HIDDEN;
185 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid) DECLSPEC_HIDDEN;
186 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid, BOOL tableweak) DECLSPEC_HIDDEN;
187 void stub_manager_disconnect(struct stub_manager *m) DECLSPEC_HIDDEN;
188 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **manager, IRpcStubBuffer **stub,
189                                  IRpcChannelBuffer **chan, IID *iid, IUnknown **iface) DECLSPEC_HIDDEN;
190 HRESULT start_apartment_remote_unknown(void) DECLSPEC_HIDDEN;
191 
192 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *obj, DWORD dest_context, void *dest_context_data, MSHLFLAGS mshlflags) DECLSPEC_HIDDEN;
193 
194 /* RPC Backend */
195 
196 struct dispatch_params;
197 
198 void    RPC_StartRemoting(struct apartment *apt) DECLSPEC_HIDDEN;
199 HRESULT RPC_CreateClientChannel(const OXID *oxid, const IPID *ipid,
200                                 const OXID_INFO *oxid_info,
201                                 DWORD dest_context, void *dest_context_data,
202                                 IRpcChannelBuffer **chan) DECLSPEC_HIDDEN;
203 HRESULT RPC_CreateServerChannel(DWORD dest_context, void *dest_context_data, IRpcChannelBuffer **chan) DECLSPEC_HIDDEN;
204 void    RPC_ExecuteCall(struct dispatch_params *params) DECLSPEC_HIDDEN;
205 HRESULT RPC_RegisterInterface(REFIID riid) DECLSPEC_HIDDEN;
206 void    RPC_UnregisterInterface(REFIID riid, BOOL wait) DECLSPEC_HIDDEN;
207 HRESULT RPC_StartLocalServer(REFCLSID clsid, IStream *stream, BOOL multi_use, void **registration) DECLSPEC_HIDDEN;
208 void    RPC_StopLocalServer(void *registration) DECLSPEC_HIDDEN;
209 HRESULT RPC_GetLocalClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv) DECLSPEC_HIDDEN;
210 HRESULT RPC_RegisterChannelHook(REFGUID rguid, IChannelHook *hook) DECLSPEC_HIDDEN;
211 void    RPC_UnregisterAllChannelHooks(void) DECLSPEC_HIDDEN;
212 HRESULT RPC_ResolveOxid(OXID oxid, OXID_INFO *oxid_info) DECLSPEC_HIDDEN;
213 
214 /* This function initialize the Running Object Table */
215 HRESULT WINAPI RunningObjectTableImpl_Initialize(void) DECLSPEC_HIDDEN;
216 
217 /* This function uninitialize the Running Object Table */
218 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void) DECLSPEC_HIDDEN;
219 
220 /* Drag and drop */
221 void OLEDD_UnInitialize(void) DECLSPEC_HIDDEN;
222 
223 /* Apartment Functions */
224 
225 APARTMENT *apartment_findfromoxid(OXID oxid, BOOL ref) DECLSPEC_HIDDEN;
226 APARTMENT *apartment_findfromtid(DWORD tid) DECLSPEC_HIDDEN;
227 DWORD apartment_release(struct apartment *apt) DECLSPEC_HIDDEN;
228 HRESULT apartment_disconnectproxies(struct apartment *apt) DECLSPEC_HIDDEN;
229 static inline HRESULT apartment_getoxid(const struct apartment *apt, OXID *oxid)
230 {
231     *oxid = apt->oxid;
232     return S_OK;
233 }
234 HRESULT apartment_createwindowifneeded(struct apartment *apt) DECLSPEC_HIDDEN;
235 HWND apartment_getwindow(const struct apartment *apt) DECLSPEC_HIDDEN;
236 void apartment_joinmta(void) DECLSPEC_HIDDEN;
237 
238 
239 /* DCOM messages used by the apartment window (not compatible with native) */
240 #define DM_EXECUTERPC   (WM_USER + 0) /* WPARAM = 0, LPARAM = (struct dispatch_params *) */
241 #define DM_HOSTOBJECT   (WM_USER + 1) /* WPARAM = 0, LPARAM = (struct host_object_params *) */
242 
243 /*
244  * Per-thread values are stored in the TEB on offset 0xF80
245  */
246 
247 /* will create if necessary */
248 static inline struct oletls *COM_CurrentInfo(void)
249 {
250     if (!NtCurrentTeb()->ReservedForOle)
251         NtCurrentTeb()->ReservedForOle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct oletls));
252 
253     return NtCurrentTeb()->ReservedForOle;
254 }
255 
256 static inline APARTMENT* COM_CurrentApt(void)
257 {
258     return COM_CurrentInfo()->apt;
259 }
260 
261 static inline GUID COM_CurrentCausalityId(void)
262 {
263     struct oletls *info = COM_CurrentInfo();
264     if (!info)
265         return GUID_NULL;
266     if (IsEqualGUID(&info->causality_id, &GUID_NULL))
267         CoCreateGuid(&info->causality_id);
268     return info->causality_id;
269 }
270 
271 /* helpers for debugging */
272 # define DEBUG_SET_CRITSEC_NAME(cs, name) (cs)->DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": " name)
273 # define DEBUG_CLEAR_CRITSEC_NAME(cs) (cs)->DebugInfo->Spare[0] = 0
274 
275 #define CHARS_IN_GUID 39 /* including NULL */
276 
277 #define WINE_CLSCTX_DONT_HOST   0x80000000
278 
279 /* from dlldata.c */
280 extern HINSTANCE hProxyDll DECLSPEC_HIDDEN;
281 extern HRESULT WINAPI OLE32_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv) DECLSPEC_HIDDEN;
282 extern HRESULT WINAPI OLE32_DllRegisterServer(void) DECLSPEC_HIDDEN;
283 extern HRESULT WINAPI OLE32_DllUnregisterServer(void) DECLSPEC_HIDDEN;
284 
285 extern HRESULT Handler_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv) DECLSPEC_HIDDEN;
286 extern HRESULT HandlerCF_Create(REFCLSID rclsid, REFIID riid, LPVOID *ppv) DECLSPEC_HIDDEN;
287 
288 extern HRESULT WINAPI GlobalOptions_CreateInstance(IClassFactory *iface, IUnknown *pUnk,
289                                                    REFIID riid, void **ppv) DECLSPEC_HIDDEN;
290 extern IClassFactory GlobalOptionsCF DECLSPEC_HIDDEN;
291 
292 /* Exported non-interface Data Advise Holder functions */
293 HRESULT DataAdviseHolder_OnConnect(IDataAdviseHolder *iface, IDataObject *pDelegate) DECLSPEC_HIDDEN;
294 void DataAdviseHolder_OnDisconnect(IDataAdviseHolder *iface) DECLSPEC_HIDDEN;
295 
296 extern UINT ownerlink_clipboard_format DECLSPEC_HIDDEN;
297 extern UINT filename_clipboard_format DECLSPEC_HIDDEN;
298 extern UINT filenameW_clipboard_format DECLSPEC_HIDDEN;
299 extern UINT dataobject_clipboard_format DECLSPEC_HIDDEN;
300 extern UINT embedded_object_clipboard_format DECLSPEC_HIDDEN;
301 extern UINT embed_source_clipboard_format DECLSPEC_HIDDEN;
302 extern UINT custom_link_source_clipboard_format DECLSPEC_HIDDEN;
303 extern UINT link_source_clipboard_format DECLSPEC_HIDDEN;
304 extern UINT object_descriptor_clipboard_format DECLSPEC_HIDDEN;
305 extern UINT link_source_descriptor_clipboard_format DECLSPEC_HIDDEN;
306 extern UINT ole_private_data_clipboard_format DECLSPEC_HIDDEN;
307 
308 extern LSTATUS create_classes_key(HKEY, const WCHAR *, REGSAM, HKEY *) DECLSPEC_HIDDEN;
309 extern LSTATUS open_classes_key(HKEY, const WCHAR *, REGSAM, HKEY *) DECLSPEC_HIDDEN;
310 
311 extern BOOL actctx_get_miscstatus(const CLSID*, DWORD, DWORD*) DECLSPEC_HIDDEN;
312 
313 extern const char *debugstr_formatetc(const FORMATETC *formatetc) DECLSPEC_HIDDEN;
314 
315 static inline void* __WINE_ALLOC_SIZE(1) heap_alloc(size_t len)
316 {
317     return HeapAlloc(GetProcessHeap(), 0, len);
318 }
319 
320 static inline BOOL heap_free(void *mem)
321 {
322     return HeapFree(GetProcessHeap(), 0, mem);
323 }
324 
325 static inline HRESULT copy_formatetc(FORMATETC *dst, const FORMATETC *src)
326 {
327     *dst = *src;
328     if (src->ptd)
329     {
330         dst->ptd = CoTaskMemAlloc( src->ptd->tdSize );
331         if (!dst->ptd) return E_OUTOFMEMORY;
332         memcpy( dst->ptd, src->ptd, src->ptd->tdSize );
333     }
334     return S_OK;
335 }
336 
337 extern HRESULT EnumSTATDATA_Construct(IUnknown *holder, ULONG index, DWORD array_len, STATDATA *data,
338                                       BOOL copy, IEnumSTATDATA **ppenum) DECLSPEC_HIDDEN;
339 
340 #endif /* __WINE_OLE_COMPOBJ_H */
341