- Use InterlockedIncrement for the ipid counter instead of a critical
[wine] / dlls / 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, 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 #include <stdarg.h>
31
32 #include "wine/list.h"
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wtypes.h"
37 #include "dcom.h"
38 #include "winreg.h"
39 #include "winternl.h"
40
41 struct apartment;
42 typedef struct apartment APARTMENT;
43
44 /* Thread-safety Annotation Legend:
45  *
46  * RO   - The value is read only. It never changes after creation, so no
47  *        locking is required.
48  * LOCK - The value is written to only using Interlocked* functions.
49  * CS   - The value is read or written to with a critical section held.
50  *        The identifier following "CS" is the specific critical section that
51  *        must be used.
52  */
53
54 typedef enum ifstub_state
55 {
56     IFSTUB_STATE_NORMAL_MARSHALED,
57     IFSTUB_STATE_NORMAL_UNMARSHALED,
58     IFSTUB_STATE_TABLE_MARSHALED
59 } IFSTUB_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     IFSTUB_STATE      state;      /* CS stub_manager->lock */
70 };
71
72
73 /* stub managers hold refs on the object and each interface stub */
74 struct stub_manager
75 {
76     struct list       entry;      /* entry in apartment stubmgr list (CS apt->cs) */
77     struct list       ifstubs;    /* list of active ifstubs for the object (CS lock) */
78     CRITICAL_SECTION  lock;
79     APARTMENT        *apt;        /* owning apt (RO) */
80
81     ULONG             extrefs;    /* number of 'external' references (LOCK) */
82     ULONG             refs;       /* internal reference count (CS apt->cs) */
83     OID               oid;        /* apartment-scoped unique identifier (RO) */
84     IUnknown         *object;     /* the object we are managing the stub for (RO) */
85     ULONG             next_ipid;  /* currently unused (LOCK) */
86 };
87
88 /* imported interface proxy */
89 struct ifproxy
90 {
91   struct list entry;       /* entry in proxy_manager list (CS parent->cs) */
92   struct proxy_manager *parent; /* owning proxy_manager (RO) */
93   LPVOID iface;            /* interface pointer (RO) */
94   IID iid;                 /* interface ID (RO) */
95   IPID ipid;               /* imported interface ID (RO) */
96   LPRPCPROXYBUFFER proxy;  /* interface proxy (RO) */
97   DWORD refs;              /* imported (public) references (CS parent->cs) */
98 };
99
100 /* imported object / proxy manager */
101 struct proxy_manager
102 {
103   const IInternalUnknownVtbl *lpVtbl;
104   struct apartment *parent; /* owning apartment (RO) */
105   struct list entry;        /* entry in apartment (CS parent->cs) */
106   LPRPCCHANNELBUFFER chan;  /* channel to object (CS cs) */
107   OXID oxid;                /* object exported ID (RO) */
108   OID oid;                  /* object ID (RO) */
109   struct list interfaces;   /* imported interfaces (CS cs) */
110   DWORD refs;               /* proxy reference count (LOCK) */
111   CRITICAL_SECTION cs;      /* thread safety for this object and children */
112   ULONG sorflags;           /* STDOBJREF flags (RO) */
113   IRemUnknown *remunk;      /* proxy to IRemUnknown used for lifecycle management (CS cs) */
114 };
115
116 /* this needs to become a COM object that implements IRemUnknown */
117 struct apartment
118 {
119   struct list entry;       
120
121   DWORD refs;              /* refcount of the apartment (LOCK) */
122   DWORD model;             /* threading model (RO) */
123   DWORD tid;               /* thread id (RO) */
124   HANDLE thread;           /* thread handle (RO) */
125   OXID oxid;               /* object exporter ID (RO) */
126   DWORD ipidc;             /* interface pointer ID counter, starts at 1 (LOCK) */
127   HWND win;                /* message window (RO) */
128   CRITICAL_SECTION cs;     /* thread safety */
129   LPMESSAGEFILTER filter;  /* message filter (CS cs) */
130   struct list proxies;     /* imported objects (CS cs) */
131   struct list stubmgrs;    /* stub managers for exported objects (CS cs) */
132   BOOL remunk_exported;    /* has the IRemUnknown interface for this apartment been created yet? (CS cs) */
133
134   OID oidc;                /* object ID counter, starts at 1, zero is invalid OID (CS cs). FIXME: remove me */
135   DWORD listenertid;       /* id of apartment_listener_thread. FIXME: remove me */
136 };
137
138 /* this is what is stored in TEB->ReservedForOle */
139 struct oletls
140 {
141     struct apartment *apt;
142     IErrorInfo       *errorinfo;   /* see errorinfo.c */
143     IUnknown         *state;       /* see CoSetState */
144     DWORD            inits;        /* number of times CoInitializeEx called */
145 };
146
147 extern void* StdGlobalInterfaceTable_Construct(void);
148 extern void  StdGlobalInterfaceTable_Destroy(void* self);
149 extern HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv);
150
151 /* FIXME: these shouldn't be needed, except for 16-bit functions */
152 extern HRESULT WINE_StringFromCLSID(const CLSID *id,LPSTR idstr);
153 HRESULT WINAPI __CLSIDFromStringA(LPCSTR idstr, CLSID *id);
154
155 extern HRESULT create_marshalled_proxy(REFCLSID rclsid, REFIID iid, LPVOID *ppv);
156
157 extern void* StdGlobalInterfaceTableInstance;
158
159 /* Standard Marshalling definitions */
160 typedef struct _wine_marshal_id {
161     OXID    oxid;       /* id of apartment */
162     OID     oid;        /* id of stub manager */
163     IPID    ipid;       /* id of interface pointer */
164 } wine_marshal_id;
165
166 inline static BOOL
167 MARSHAL_Compare_Mids(wine_marshal_id *mid1,wine_marshal_id *mid2) {
168     return
169         (mid1->oxid == mid2->oxid)      &&
170         (mid1->oid == mid2->oid)        &&
171         IsEqualGUID(&(mid1->ipid),&(mid2->ipid))
172     ;
173 }
174
175 HRESULT MARSHAL_Disconnect_Proxies(APARTMENT *apt);
176 HRESULT MARSHAL_GetStandardMarshalCF(LPVOID *ppv);
177
178 ULONG stub_manager_int_addref(struct stub_manager *This);
179 ULONG stub_manager_int_release(struct stub_manager *This);
180 struct stub_manager *new_stub_manager(APARTMENT *apt, IUnknown *object);
181 ULONG stub_manager_ext_addref(struct stub_manager *m, ULONG refs);
182 ULONG stub_manager_ext_release(struct stub_manager *m, ULONG refs);
183 struct ifstub *stub_manager_new_ifstub(struct stub_manager *m, IRpcStubBuffer *sb, IUnknown *iptr, REFIID iid, BOOL tablemarshal);
184 struct stub_manager *get_stub_manager(APARTMENT *apt, OID oid);
185 struct stub_manager *get_stub_manager_from_object(APARTMENT *apt, void *object);
186 BOOL stub_manager_notify_unmarshal(struct stub_manager *m, const IPID *ipid);
187 BOOL stub_manager_is_table_marshaled(struct stub_manager *m, const IPID *ipid);
188 HRESULT register_ifstub(APARTMENT *apt, STDOBJREF *stdobjref, REFIID riid, IUnknown *obj, MSHLFLAGS mshlflags);
189 HRESULT ipid_to_stub_manager(const IPID *ipid, APARTMENT **stub_apt, struct stub_manager **stubmgr_ret);
190 IRpcStubBuffer *ipid_to_stubbuffer(const IPID *ipid);
191 HRESULT start_apartment_remote_unknown(void);
192
193 IRpcStubBuffer *mid_to_stubbuffer(wine_marshal_id *mid);
194
195 void start_apartment_listener_thread(void);
196
197 extern HRESULT PIPE_GetNewPipeBuf(wine_marshal_id *mid, IRpcChannelBuffer **pipebuf);
198 void RPC_StartLocalServer(REFCLSID clsid, IStream *stream);
199
200 /* This function initialize the Running Object Table */
201 HRESULT WINAPI RunningObjectTableImpl_Initialize(void);
202
203 /* This function uninitialize the Running Object Table */
204 HRESULT WINAPI RunningObjectTableImpl_UnInitialize(void);
205
206 /* This function decomposes a String path to a String Table containing all the elements ("\" or "subDirectory" or "Directory" or "FileName") of the path */
207 int WINAPI FileMonikerImpl_DecomposePath(LPCOLESTR str, LPOLESTR** stringTable);
208
209 /* compobj.c */
210 APARTMENT *COM_CreateApartment(DWORD model);
211 APARTMENT *COM_ApartmentFromOXID(OXID oxid, BOOL ref);
212 APARTMENT *COM_ApartmentFromTID(DWORD tid);
213 DWORD COM_ApartmentAddRef(struct apartment *apt);
214 DWORD COM_ApartmentRelease(struct apartment *apt);
215
216 /*
217  * Per-thread values are stored in the TEB on offset 0xF80,
218  * see http://www.microsoft.com/msj/1099/bugslayer/bugslayer1099.htm
219  */
220
221 /* will create if necessary */
222 static inline struct oletls *COM_CurrentInfo(void)
223 {
224     if (!NtCurrentTeb()->ReservedForOle)
225         NtCurrentTeb()->ReservedForOle = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct oletls));
226
227     return NtCurrentTeb()->ReservedForOle;
228 }
229
230 static inline APARTMENT* COM_CurrentApt(void)
231 {  
232     return COM_CurrentInfo()->apt;
233 }
234
235 #define ICOM_THIS_MULTI(impl,field,iface) impl* const This=(impl*)((char*)(iface) - offsetof(impl,field))
236
237 #endif /* __WINE_OLE_COMPOBJ_H */