[OLE32]
[reactos.git] / reactos / dll / win32 / ole32 / compobj_private.h
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 };
100
101 /* imported interface proxy */
102 struct ifproxy
103 {
104 struct list entry; /* entry in proxy_manager list (CS parent->cs) */
105 struct proxy_manager *parent; /* owning proxy_manager (RO) */
106 LPVOID iface; /* interface pointer (RO) */
107 STDOBJREF stdobjref; /* marshal data that represents this object (RO) */
108 IID iid; /* interface ID (RO) */
109 LPRPCPROXYBUFFER proxy; /* interface proxy (RO) */
110 ULONG refs; /* imported (public) references (LOCK) */
111 IRpcChannelBuffer *chan; /* channel to object (CS parent->cs) */
112 };
113
114 struct apartment
115 {
116 struct list entry;
117
118 LONG refs; /* refcount of the apartment (LOCK) */
119 BOOL multi_threaded; /* multi-threaded or single-threaded apartment? (RO) */
120 DWORD tid; /* thread id (RO) */
121 OXID oxid; /* object exporter ID (RO) */
122 LONG ipidc; /* interface pointer ID counter, starts at 1 (LOCK) */
123 CRITICAL_SECTION cs; /* thread safety */
124 struct list proxies; /* imported objects (CS cs) */
125 struct list stubmgrs; /* stub managers for exported objects (CS cs) */
126 BOOL remunk_exported; /* has the IRemUnknown interface for this apartment been created yet? (CS cs) */
127 LONG remoting_started; /* has the RPC system been started for this apartment? (LOCK) */
128 struct list psclsids; /* list of registered PS CLSIDs (CS cs) */
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 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object) DECLSPEC_HIDDEN;
178 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs, BOOL tableweak) DECLSPEC_HIDDEN;
179 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs, BOOL tableweak, BOOL last_unlock_releases) DECLSPEC_HIDDEN;
180 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid,
181 DWORD dest_context, void *dest_context_data, MSHLFLAGS flags) DECLSPEC_HIDDEN;
182 struct ifstub *stub_manager_find_ifstub(struct stub_manager *m, REFIID iid, MSHLFLAGS flags) DECLSPEC_HIDDEN;
183 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid) DECLSPEC_HIDDEN;
184 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object) DECLSPEC_HIDDEN;
185 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid) DECLSPEC_HIDDEN;
186 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid) DECLSPEC_HIDDEN;
187 void stub_manager_release_marshal_data(struct stub_manager *m, ULONG refs, const IPID *ipid, BOOL tableweak) DECLSPEC_HIDDEN;
188 HRESULT ipid_get_dispatch_params(const IPID *ipid, APARTMENT **stub_apt, IRpcStubBuffer **stub, IRpcChannelBuffer **chan, IID *iid, IUnknown **iface) DECLSPEC_HIDDEN;
189 HRESULT start_apartment_remote_unknown(void) DECLSPEC_HIDDEN;
190
191 HRESULT marshal_object(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *obj, DWORD dest_context, void *dest_context_data, MSHLFLAGS mshlflags) DECLSPEC_HIDDEN;
192
193 /* RPC Backend */
194
195 struct dispatch_params;
196
197 void RPC_StartRemoting(struct apartment *apt) DECLSPEC_HIDDEN;
198 HRESULT RPC_CreateClientChannel(const OXID *oxid, const IPID *ipid,
199 const OXID_INFO *oxid_info,
200 DWORD dest_context, void *dest_context_data,
201 IRpcChannelBuffer **chan) DECLSPEC_HIDDEN;
202 HRESULT RPC_CreateServerChannel(DWORD dest_context, void *dest_context_data, IRpcChannelBuffer **chan) DECLSPEC_HIDDEN;
203 void RPC_ExecuteCall(struct dispatch_params *params) DECLSPEC_HIDDEN;
204 HRESULT RPC_RegisterInterface(REFIID riid) DECLSPEC_HIDDEN;
205 void RPC_UnregisterInterface(REFIID riid) DECLSPEC_HIDDEN;
206 HRESULT RPC_StartLocalServer(REFCLSID clsid, IStream *stream, BOOL multi_use, void **registration) DECLSPEC_HIDDEN;
207 void RPC_StopLocalServer(void *registration) DECLSPEC_HIDDEN;
208 HRESULT RPC_GetLocalClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv) DECLSPEC_HIDDEN;
209 HRESULT RPC_RegisterChannelHook(REFGUID rguid, IChannelHook *hook) DECLSPEC_HIDDEN;
210 void RPC_UnregisterAllChannelHooks(void) DECLSPEC_HIDDEN;
211 HRESULT RPC_ResolveOxid(OXID oxid, OXID_INFO *oxid_info) DECLSPEC_HIDDEN;
212
213 /* This function initialize the Running Object Table */
214 HRESULT WINAPI RunningObjectTableImpl_Initialize(void) DECLSPEC_HIDDEN;
215
216 /* This function uninitialize the Running Object Table */
217 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void) DECLSPEC_HIDDEN;
218
219 /* Drag and drop */
220 void OLEDD_UnInitialize(void) DECLSPEC_HIDDEN;
221
222 /* Apartment Functions */
223
224 APARTMENT *apartment_findfromoxid(OXID oxid, BOOL ref) DECLSPEC_HIDDEN;
225 APARTMENT *apartment_findfromtid(DWORD tid) DECLSPEC_HIDDEN;
226 DWORD apartment_release(struct apartment *apt) DECLSPEC_HIDDEN;
227 HRESULT apartment_disconnectproxies(struct apartment *apt) DECLSPEC_HIDDEN;
228 void apartment_disconnectobject(struct apartment *apt, void *object) 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 /* Exported non-interface Data Advise Holder functions */
289 HRESULT DataAdviseHolder_OnConnect(IDataAdviseHolder *iface, IDataObject *pDelegate) DECLSPEC_HIDDEN;
290 void DataAdviseHolder_OnDisconnect(IDataAdviseHolder *iface) DECLSPEC_HIDDEN;
291
292 extern UINT ownerlink_clipboard_format DECLSPEC_HIDDEN;
293 extern UINT filename_clipboard_format DECLSPEC_HIDDEN;
294 extern UINT filenameW_clipboard_format DECLSPEC_HIDDEN;
295 extern UINT dataobject_clipboard_format DECLSPEC_HIDDEN;
296 extern UINT embedded_object_clipboard_format DECLSPEC_HIDDEN;
297 extern UINT embed_source_clipboard_format DECLSPEC_HIDDEN;
298 extern UINT custom_link_source_clipboard_format DECLSPEC_HIDDEN;
299 extern UINT link_source_clipboard_format DECLSPEC_HIDDEN;
300 extern UINT object_descriptor_clipboard_format DECLSPEC_HIDDEN;
301 extern UINT link_source_descriptor_clipboard_format DECLSPEC_HIDDEN;
302 extern UINT ole_private_data_clipboard_format DECLSPEC_HIDDEN;
303
304 extern LSTATUS create_classes_key(HKEY, const WCHAR *, REGSAM, HKEY *) DECLSPEC_HIDDEN;
305 extern LSTATUS open_classes_key(HKEY, const WCHAR *, REGSAM, HKEY *) DECLSPEC_HIDDEN;
306
307 static inline void *heap_alloc(size_t len)
308 {
309 return HeapAlloc(GetProcessHeap(), 0, len);
310 }
311
312 static inline BOOL heap_free(void *mem)
313 {
314 return HeapFree(GetProcessHeap(), 0, mem);
315 }
316
317 #endif /* __WINE_OLE_COMPOBJ_H */