Get rid of the registry saving level hack.
[wine] / documentation / ole.sgml
1   <chapter id="ole">
2     <title>COM in Wine</title>
3
4     <sect1 id="com-writing">
5       <title>Writing COM Components for Wine</title>
6
7       <para>
8         This section describes how to create your own natively
9         compiled COM components.
10       </para>
11
12       <sect2>
13         <title>Macros to define a COM interface</title>
14
15         <para>
16           The goal of the following set of definitions is to provide a
17           way to use the same header file definitions to provide both
18           a C interface and a C++ object oriented interface to COM
19           interfaces. The type of interface is selected automatically
20           depending on the language but it is always possible to get
21           the C interface in C++ by defining CINTERFACE.
22         </para>
23         <para>
24           It is based on the following assumptions:
25         </para>
26         <itemizedlist>
27           <listitem>
28             <para>
29               all COM interfaces derive from IUnknown, this should not
30               be a problem.
31             </para>
32           </listitem>
33           <listitem>
34             <para>
35               the header file only defines the interface, the actual
36               fields are defined separately in the C file implementing
37               the interface.
38             </para>
39           </listitem>
40         </itemizedlist>
41         <para>
42           The natural approach to this problem would be to make sure
43           we get a C++ class and virtual methods in C++ and a
44           structure with a table of pointer to functions in C.
45           Unfortunately the layout of the virtual table is compiler
46           specific, the layout of g++ virtual tables is not the same
47           as that of an egcs virtual table which is not the same as
48           that generated by Visual C++. There are work arounds to make
49           the virtual tables compatible via padding but unfortunately
50           the one which is imposed to the Wine emulator by the Windows
51           binaries, i.e. the Visual C++ one, is the most compact of
52           all.
53         </para>
54         <para>
55           So the solution I finally adopted does not use virtual
56           tables. Instead I use in-line non virtual methods that
57           dereference the method pointer themselves and perform the
58           call.
59         </para>
60         <para>
61           Let's take Direct3D as an example:
62         </para>
63         <programlisting>#define ICOM_INTERFACE IDirect3D
64 #define IDirect3D_METHODS \
65     ICOM_METHOD1(HRESULT,Initialize,    REFIID,) \
66     ICOM_METHOD2(HRESULT,EnumDevices,   LPD3DENUMDEVICESCALLBACK,, LPVOID,) \
67     ICOM_METHOD2(HRESULT,CreateLight,   LPDIRECT3DLIGHT*,, IUnknown*,) \
68     ICOM_METHOD2(HRESULT,CreateMaterial,LPDIRECT3DMATERIAL*,, IUnknown*,) \
69     ICOM_METHOD2(HRESULT,CreateViewport,LPDIRECT3DVIEWPORT*,, IUnknown*,) \
70     ICOM_METHOD2(HRESULT,FindDevice,    LPD3DFINDDEVICESEARCH,, LPD3DFINDDEVICERESULT,)
71 #define IDirect3D_IMETHODS \
72     IUnknown_IMETHODS \
73     IDirect3D_METHODS
74 ICOM_DEFINE(IDirect3D,IUnknown)
75 #undef ICOM_INTERFACE
76
77 #ifdef ICOM_CINTERFACE
78 // *** IUnknown methods *** //
79 #define IDirect3D_QueryInterface(p,a,b) ICOM_CALL2(QueryInterface,p,a,b)
80 #define IDirect3D_AddRef(p)             ICOM_CALL (AddRef,p)
81 #define IDirect3D_Release(p)            ICOM_CALL (Release,p)
82 // *** IDirect3D methods *** //
83 #define IDirect3D_Initialize(p,a)       ICOM_CALL1(Initialize,p,a)
84 #define IDirect3D_EnumDevices(p,a,b)    ICOM_CALL2(EnumDevice,p,a,b)
85 #define IDirect3D_CreateLight(p,a,b)    ICOM_CALL2(CreateLight,p,a,b)
86 #define IDirect3D_CreateMaterial(p,a,b) ICOM_CALL2(CreateMaterial,p,a,b)
87 #define IDirect3D_CreateViewport(p,a,b) ICOM_CALL2(CreateViewport,p,a,b)
88 #define IDirect3D_FindDevice(p,a,b)     ICOM_CALL2(FindDevice,p,a,b)
89 #endif</programlisting>
90         <para>
91           Comments:
92         </para>
93         <para>
94           The ICOM_INTERFACE macro is used in the ICOM_METHOD macros
95           to define the type of the 'this' pointer. Defining this
96           macro here saves us the trouble of having to repeat the
97           interface name everywhere. Note however that because of the
98           way macros work, a macro like ICOM_METHOD1 cannot use
99           'ICOM_INTERFACE##_VTABLE' because this would give
100           'ICOM_INTERFACE_VTABLE' and not 'IDirect3D_VTABLE'.
101         </para>
102         <para>
103           ICOM_METHODS defines the methods specific to this
104           interface. It is then aggregated with the inherited methods
105           to form ICOM_IMETHODS.
106         </para>
107         <para>
108           ICOM_IMETHODS defines the list of methods that are
109           inheritable from this interface. It must be written manually
110           (rather than using a macro to generate the equivalent code)
111           to avoid macro recursion (which compilers don't like).
112         </para>
113         <para>
114           The ICOM_DEFINE finally declares all the structures
115           necessary for the interface. We have to explicitly use the
116           interface name for macro expansion reasons again.  Inherited
117           methods are inherited in C by using the IDirect3D_METHODS
118           macro and the parent's Xxx_IMETHODS macro. In C++ we need
119           only use the IDirect3D_METHODS since method inheritance is
120           taken care of by the language.
121         </para>
122         <para>
123           In C++ the ICOM_METHOD macros generate a function prototype
124           and a call to a function pointer method. This means using
125           once 't1 p1, t2 p2, ...' and once 'p1, p2' without the
126           types. The only way I found to handle this is to have one
127           ICOM_METHOD macro per number of parameters and to have it
128           take only the type information (with const if necessary) as
129           parameters.  The 'undef ICOM_INTERFACE' is here to remind
130           you that using ICOM_INTERFACE in the following macros will
131           not work. This time it's because the ICOM_CALL macro
132           expansion is done only once the 'IDirect3D_Xxx' macro is
133           expanded. And by that time ICOM_INTERFACE will be long gone
134           anyway.
135         </para>
136         <para>
137           You may have noticed the double commas after each parameter
138           type. This allows you to put the name of that parameter
139           which I think is good for documentation. It is not required
140           and since I did not know what to put there for this example
141           (I could only find doc about IDirect3D2), I left them blank.
142         </para>
143         <para>
144           Finally the set of 'IDirect3D_Xxx' macros is a standard set
145           of macros defined to ease access to the interface methods in
146           C. Unfortunately I don't see any way to avoid having to
147           duplicate the inherited method definitions there. This time
148           I could have used a trick to use only one macro whatever the
149           number of parameters but I preferred to have it work the same
150           way as above.
151         </para>
152         <para>
153           You probably have noticed that we don't define the fields we
154           need to actually implement this interface: reference count,
155           pointer to other resources and miscellaneous fields. That's
156           because these interfaces are just that: interfaces. They may
157           be implemented more than once, in different contexts and
158           sometimes not even in Wine. Thus it would not make sense to
159           impose that the interface contains some specific fields.
160         </para>
161       </sect2>
162
163       <sect2>
164         <title>Bindings in C</title>
165
166         <para>
167           In C this gives:
168         </para>
169         <programlisting>typedef struct IDirect3DVtbl IDirect3DVtbl;
170 struct IDirect3D {
171     IDirect3DVtbl* lpVtbl;
172 };
173 struct IDirect3DVtbl {
174     HRESULT (*fnQueryInterface)(IDirect3D* me, REFIID riid, LPVOID* ppvObj);
175     ULONG (*fnAddRef)(IDirect3D* me);
176     ULONG (*fnRelease)(IDirect3D* me);
177     HRESULT (*fnInitialize)(IDirect3D* me, REFIID a);
178     HRESULT (*fnEnumDevices)(IDirect3D* me, LPD3DENUMDEVICESCALLBACK a, LPVOID b);
179     HRESULT (*fnCreateLight)(IDirect3D* me, LPDIRECT3DLIGHT* a, IUnknown* b);
180     HRESULT (*fnCreateMaterial)(IDirect3D* me, LPDIRECT3DMATERIAL* a, IUnknown* b);
181     HRESULT (*fnCreateViewport)(IDirect3D* me, LPDIRECT3DVIEWPORT* a, IUnknown* b);
182     HRESULT (*fnFindDevice)(IDirect3D* me, LPD3DFINDDEVICESEARCH a, LPD3DFINDDEVICERESULT b);
183 }; 
184
185 #ifdef ICOM_CINTERFACE
186 // *** IUnknown methods *** //
187 #define IDirect3D_QueryInterface(p,a,b) (p)->lpVtbl->fnQueryInterface(p,a,b)
188 #define IDirect3D_AddRef(p)             (p)->lpVtbl->fnAddRef(p)
189 #define IDirect3D_Release(p)            (p)->lpVtbl->fnRelease(p)
190 // *** IDirect3D methods *** //
191 #define IDirect3D_Initialize(p,a)       (p)->lpVtbl->fnInitialize(p,a)
192 #define IDirect3D_EnumDevices(p,a,b)    (p)->lpVtbl->fnEnumDevice(p,a,b)
193 #define IDirect3D_CreateLight(p,a,b)    (p)->lpVtbl->fnCreateLight(p,a,b)
194 #define IDirect3D_CreateMaterial(p,a,b) (p)->lpVtbl->fnCreateMaterial(p,a,b)
195 #define IDirect3D_CreateViewport(p,a,b) (p)->lpVtbl->fnCreateViewport(p,a,b)
196 #define IDirect3D_FindDevice(p,a,b)     (p)->lpVtbl->fnFindDevice(p,a,b)
197 #endif</programlisting>
198         <para>
199           Comments:
200         </para>
201         <para>
202           IDirect3D only contains a pointer to the IDirect3D
203           virtual/jump table. This is the only thing the user needs to
204           know to use the interface. Of course the structure we will
205           define to implement this interface will have more fields but
206           the first one will match this pointer.
207         </para>
208         <para>
209           The code generated by ICOM_DEFINE defines both the structure
210           representing the interface and the structure for the jump
211           table. ICOM_DEFINE uses the parent's Xxx_IMETHODS macro to
212           automatically repeat the prototypes of all the inherited
213           methods and then uses IDirect3D_METHODS to define the
214           IDirect3D methods.
215         </para>
216         <para>
217           Each method is declared as a pointer to function field in
218           the jump table. The implementation will fill this jump table
219           with appropriate values, probably using a static variable,
220           and initialize the lpVtbl field to point to this variable.
221         </para>
222         <para>
223           The IDirect3D_Xxx macros then just dereference the lpVtbl
224           pointer and use the function pointer corresponding to the
225           macro name. This emulates the behavior of a virtual table
226           and should be just as fast.
227         </para>
228         <para>
229           This C code should be quite compatible with the Windows
230           headers both for code that uses COM interfaces and for code
231           implementing a COM interface.
232         </para>
233       </sect2>
234
235       <sect2>
236         <title>Bindings in C++</title>
237         <para>
238           And in C++ (with gcc's g++):
239         </para>
240         <programlisting>typedef struct IDirect3D: public IUnknown {
241     private: HRESULT (*fnInitialize)(IDirect3D* me, REFIID a);
242     public: inline HRESULT Initialize(REFIID a) { return ((IDirect3D*)t.lpVtbl)->fnInitialize(this,a); };
243     private: HRESULT (*fnEnumDevices)(IDirect3D* me, LPD3DENUMDEVICESCALLBACK a, LPVOID b);
244     public: inline HRESULT EnumDevices(LPD3DENUMDEVICESCALLBACK a, LPVOID b)
245         { return ((IDirect3D*)t.lpVtbl)->fnEnumDevices(this,a,b); };
246     private: HRESULT (*fnCreateLight)(IDirect3D* me, LPDIRECT3DLIGHT* a, IUnknown* b);
247     public: inline HRESULT CreateLight(LPDIRECT3DLIGHT* a, IUnknown* b)
248         { return ((IDirect3D*)t.lpVtbl)->fnCreateLight(this,a,b); };
249     private: HRESULT (*fnCreateMaterial)(IDirect3D* me, LPDIRECT3DMATERIAL* a, IUnknown* b);
250     public: inline HRESULT CreateMaterial(LPDIRECT3DMATERIAL* a, IUnknown* b)
251         { return ((IDirect3D*)t.lpVtbl)->fnCreateMaterial(this,a,b); };
252     private: HRESULT (*fnCreateViewport)(IDirect3D* me, LPDIRECT3DVIEWPORT* a, IUnknown* b);
253     public: inline HRESULT CreateViewport(LPDIRECT3DVIEWPORT* a, IUnknown* b)
254         { return ((IDirect3D*)t.lpVtbl)->fnCreateViewport(this,a,b); };
255     private:  HRESULT (*fnFindDevice)(IDirect3D* me, LPD3DFINDDEVICESEARCH a, LPD3DFINDDEVICERESULT b);
256     public: inline HRESULT FindDevice(LPD3DFINDDEVICESEARCH a, LPD3DFINDDEVICERESULT b)
257         { return ((IDirect3D*)t.lpVtbl)->fnFindDevice(this,a,b); };
258 };</programlisting>
259         <para>
260           Comments:
261         </para>
262         <para>
263           In C++ IDirect3D does double duty as both the virtual/jump
264           table and as the interface definition. The reason for this
265           is to avoid having to duplicate the method definitions: once
266           to have the function pointers in the jump table and once to
267           have the methods in the interface class. Here one macro can
268           generate both. This means though that the first pointer,
269           t.lpVtbl defined in IUnknown, must be interpreted as the
270           jump table pointer if we interpret the structure as the
271           interface class, and as the function pointer to the
272           QueryInterface method, t.fnQueryInterface, if we interpret
273           the structure as the jump table. Fortunately this gymnastic
274           is entirely taken care of in the header of IUnknown.
275         </para>
276         <para>
277           Of course in C++ we use inheritance so that we don't have to
278           duplicate the method definitions.
279         </para>
280         <para>
281           Since IDirect3D does double duty, each ICOM_METHOD macro
282           defines both a function pointer and a non-virtual inline
283           method which dereferences it and calls it. This way this
284           method behaves just like a virtual method but does not
285           create a true C++ virtual table which would break the
286           structure layout. If you look at the implementation of these
287           methods you'll notice that they would not work for void
288           functions. We have to return something and fortunately this
289           seems to be what all the COM methods do (otherwise we would
290           need another set of macros).
291         </para>
292         <para>
293           Note how the ICOM_METHOD generates both function prototypes
294           mixing types and formal parameter names and the method
295           invocation using only the formal parameter name. This is the
296           reason why we need different macros to handle different
297           numbers of parameters.
298         </para>
299         <para>
300           Finally there is no IDirect3D_Xxx macro. These are not
301           needed in C++ unless the CINTERFACE macro is defined in
302           which case we would not be here.
303         </para>
304         <para>
305           This C++ code works well for code that just uses COM
306           interfaces. But it will not work with C++ code implement a
307           COM interface. That's because such code assumes the
308           interface methods are declared as virtual C++ methods which
309           is not the case here.
310         </para>
311       </sect2>
312
313       <sect2>
314         <title>Implementing a COM interface.</title>
315
316         <para>
317           This continues the above example. This example assumes that
318           the implementation is in C.
319         </para>
320         <programlisting>typedef struct _IDirect3D {
321     void* lpVtbl;
322     // ...
323  } _IDirect3D;
324
325 static ICOM_VTABLE(IDirect3D) d3dvt;
326
327 // implement the IDirect3D methods here
328
329 int IDirect3D_fnQueryInterface(IDirect3D* me)
330 {
331     ICOM_THIS(IDirect3D,me);
332     // ...
333 }
334
335 // ...
336
337 static ICOM_VTABLE(IDirect3D) d3dvt = {
338     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
339     IDirect3D_fnQueryInterface,
340     IDirect3D_fnAdd,
341     IDirect3D_fnAdd2,
342     IDirect3D_fnInitialize,
343     IDirect3D_fnSetWidth
344 };</programlisting>
345         <para>
346           Comments:
347         </para>
348         <para>
349           We first define what the interface really contains. This is
350           the _IDirect3D structure. The first field must of course be
351           the virtual table pointer. Everything else is free.
352         </para>
353         <para>
354           Then we predeclare our static virtual table variable, we
355           will need its address in some methods to initialize the
356           virtual table pointer of the returned interface objects.
357         </para>
358         <para>
359           Then we implement the interface methods. To match what has
360           been declared in the header file they must take a pointer to
361           a IDirect3D structure and we must cast it to an _IDirect3D
362           so that we can manipulate the fields. This is performed by
363           the ICOM_THIS macro.
364         </para>
365         <para>
366           Finally we initialize the virtual table.
367         </para>
368       </sect2>
369     </sect1>
370                                          
371     <sect1 id="dcom-1">
372       <title>A brief introduction to DCOM in Wine</title>
373
374       <para>
375         This section explains the basic principles behind DCOM remoting as used by InstallShield and others.
376       </para>
377
378       <sect2>
379         <title>BASICS</title>
380
381         <para>
382           The basic idea behind DCOM is to take a COM object and make it location
383           transparent. That means you can use it from other threads, processes and
384           machines without having to worry about the fact that you can't just
385           dereference the interface vtable pointer to call methods on it.
386         </para>
387
388         <para>
389           You might be wondering about putting threads next to processes and
390           machines in that last paragraph. You can access thread safe objects from
391           multiple threads without DCOM normally, right? Why would you need RPC
392           magic to do that?
393         </para>
394
395         <para>
396           The answer is of course that COM doesn't assume that objects actually
397           are thread-safe. Most real-world objects aren't, in fact, for various
398           reasons. What these reasons are isn't too important here, though; it's
399           just important to realize that the problem of thread-unsafe objects is
400           what COM tries hard to solve with its apartment model. There are also
401           ways to tell COM that your object is truly thread-safe (namely the
402           free-threaded marshaller). In general, no object is truly thread-safe if
403           it could potentially use another not so thread-safe object, though, so
404           the free-threaded marshaller is less used than you'd think.
405         </para>
406         
407         <para>
408           For now, suffice it to say that COM lets you "marshal" interfaces into
409           other "apartments". An apartment (you may see it referred to as a
410           context in modern versions of COM) can be thought of as a location, and
411           contains objects. 
412         </para>
413
414         <para>
415           Every thread in a program that uses COM exists in an apartment. If a
416           thread wishes to use an object from another apartment, marshalling and
417           the whole DCOM infrastructure gets involved to make that happen behind
418           the scenes.
419         </para>
420
421         <para>
422           So. Each COM object resides in an apartment, and each apartment
423           resides in a process, and each process resides in a machine, and each
424           machine resides in a network. Allowing those objects to be used
425           from <emphasis>any</emphasis> of these different places is what DCOM
426           is all about.
427         </para>
428
429         <para>
430           The process of marshalling refers to taking a function call in an
431           apartment and actually performing it in another apartment. Let's say you
432           have two machines, A and B, and on machine B there is an object sitting
433           in a DLL on the hard disk. You want to create an instance of that object
434           (activate it) and use it as if you had compiled it into your own
435           program. This is hard, because the remote object is expecting to be
436           called by code in its own address space - it may do things like accept
437           pointers to linked lists and even return other objects.
438         </para>
439
440         <para>
441           Very basic marshalling is easy enough to understand. You take a method
442           on a remote interface (that is a COM interface that is
443           implemented on the remote computer), copy each of its
444           parameters into a buffer, and
445           send it to the remote computer. On the other end, the remote server
446           reads each parameter from the buffer, calls the method, writes the
447           result into another buffer and sends it back.
448         </para>
449
450         <para>
451           The tricky part is exactly how to encode those parameters in the buffer,
452           and how to convert standard stdcall/cdecl method calls to network
453           packets and back again. This is the job of the RPCRT4.DLL file - or the
454           Remote Procedure Call Runtime. 
455         </para>
456
457         <para>
458           The backbone of DCOM is this RPC runtime, which is an implementation
459           of <ulink
460           url="http://www.opengroup.org/onlinepubs/009629399/toc.htm">DCE
461           RPC</ulink>. DCE RPC is not naturally object oriented, so this
462           protocol is extended with some new constructs and by assigning new
463           meanings to some of the packet fields, to produce ORPC or Object
464           RPC. You might see it called MS-RPC as well.
465         </para>
466
467         <para>
468           RPC packets contain a buffer containing marshalled data in NDR format.
469           NDR is short for "Network Data Representation" and is similar
470           to the XDR
471           format used in SunRPC (the closest native equivalent on Linux to DCE
472           RPC). NDR/XDR are all based on the idea of graph serialization and were
473           worked out during the 80s, meaning they are very powerful and can do
474           things like marshal doubly linked lists and other rather tricky
475           structures.
476         </para>
477
478         <para>
479           In Wine, our DCOM implementation is <emphasis>not</emphasis>
480           currently based on the
481           RPC runtime, as while few programs use DCOM even fewer use
482           RPC directly so it was developed some time after
483           OLE32/OLEAUT32 were. Eventually this will have to be fixed,
484           otherwise our DCOM will never be compatible with
485           Microsoft's. Bear this in mind as you read through the code
486           however.
487         </para>
488       </sect2>
489       
490       <sect2>
491         <title>PROXIES AND STUBS</title>
492
493         <para>
494           Manually marshalling and unmarshalling each method call using the NDR
495           APIs (NdrConformantArrayMarshall etc) is very tedious work, so the
496           Platform SDK ships with a tool called "midl" which is an IDL compiler.
497           IDL or the "Interface Definition Language" is a tool designed
498           specifically for describing interfaces in a reasonably language neutral
499           fashion, though in reality it bears a close resemblence to C++.
500         </para>
501
502         <para>
503           By describing the functions you want to expose via RPC in IDL therefore,
504           it becomes possible to pass this file to MIDL which spits out a huge
505           amount of C source code. That code defines functions which have the same
506           prototype as the functions described in your IDL but which internally
507           take each argument, marshal it using Ndr, send the packet, and unmarshal
508           the return.
509         </para>
510         
511         <para>
512           Because this code proxies the code from the client to the server, the
513           functions are called proxies. Easy, right?
514         </para>
515
516         <para>
517           Of course, in the RPC server process at the other end, you need some way
518           to unmarshal the RPCs, so you have functions also generated by MIDL
519           which are the inverse of the proxies; they accept an NDR buffer, extract
520           the parameters, call the real function and then marshal the result back.
521           They are called stubs, and stand in for the real calling code in the
522           client process.
523         </para>
524         
525         <para>
526           The sort of marshalling/unmarshalling code that MIDL spits out can be
527           seen in dlls/oleaut32/oaidl_p.c - it's not exactly what it would look
528           like as that file contains DCOM proxies/stubs which are different, but
529           you get the idea. Proxy functions take the arguments and feed them to
530           the NDR marshallers (or picklers), invoke an NdrProxySendReceive and
531           then convert the out parameters and return code. There's a ton of goop
532           in there for dealing with buffer allocation, exceptions and so on - it's
533           really ugly code. But, this is the basic concept behind DCE RPC.
534         </para>
535       </sect2>
536       
537       <sect2>
538         <title>INTERFACE MARSHALLING</title>
539
540         <para>
541           Standard NDR only knows about C style function calls - they
542           can accept and even return structures, but it has no concept
543           of COM interfaces.  Confusingly DCE RPC <emphasis>does</emphasis> have a
544           concept of RPC interfaces which are just convenient ways to
545           bundle function calls together into namespaces, but let's
546           ignore that for now as it just muddies the water. The
547           primary extension made by Microsoft to NDR then was the
548           ability to take a COM interface pointer and marshal that
549           into the NDR stream.
550         </para>
551
552         <para>
553           The basic theory of proxies and stubs and IDL is still here, but it's
554           been modified slightly. Whereas before you could define a bunch of
555           functions in IDL, now a new "object" keyword has appeared. This tells
556           MIDL that you're describing a COM interface, and as a result the
557           proxies/stubs it generates are also COM objects.
558         </para>
559
560         <para>
561           That's a very important distinction. When you make a call to a remote
562           COM object you do it via a proxy object that COM has constructed on the
563           fly. Likewise, a stub object on the remote end unpacks the RPC packet
564           and makes the call. 
565         </para>
566
567         <para>
568           Because this is object-oriented RPC, there are a few complications: for
569           instance, a call that goes via the same proxies/stubs may end up at a
570           different object instance, so the RPC runtime keeps track of "this" and
571           "that" in the RPC packets.
572         </para>
573
574         <para>
575           This leads naturally onto the question of how we got those proxy/stub
576           objects in the first place, and where they came from. You can use the
577           CoCreateInstanceEx API to activate COM objects on a remote machine, this
578           works like CoCreateInstance API. Behind the scenes, a lot of stuff is
579           involved to do this (like IRemoteActivation, IOXIDResolver and so on)
580           but let's gloss over that for now.
581         </para>
582
583         <para>
584           When DCOM creates an object on a remote machine, the DCOM runtime on
585           that machine activates the object in the usual way (by looking it up in
586           the registry etc) and then marshals the requested interface back to the
587           client. Marshalling an interface takes a pointer, and produces a buffer
588           containing all the information DCOM needs to construct a proxy object in
589           the client, a stub object in the server and link the two together.
590         </para>
591
592         <para>
593           The structure of a marshalled interface pointer is somewhat complex.
594           Let's ignore that too. The important thing is how COM proxies/stubs are
595           loaded.
596         </para>
597       </sect2>
598
599       <sect2>
600         <title>COM PROXY/STUB SYSTEM</title>
601
602         <para>
603           COM proxies are objects that implement both the interfaces needing to be
604           proxied and also IRpcProxyBuffer. Likewise, COM stubs implement
605           IRpcStubBuffer and understand how to invoke the methods of the requested
606           interface.
607         </para>
608
609         <para>
610           You may be wondering what the word "buffer" is doing in those interface
611           names. I'm not sure either, except that a running theme in DCOM is that
612           interfaces which have nothing to do with buffers have the word Buffer
613           appended to them, seemingly at random. Ignore it and <emphasis>don't let it
614             confuse you</emphasis>
615           :) This stuff is convoluted enough ...
616         </para>
617
618         <para>
619           The IRpc[Proxy/Stub]Buffer interfaces are used to control the proxy/stub
620           objects and are one of the many semi-public interfaces used in DCOM.
621         </para>
622
623         <para>
624           DCOM is theoretically an internet RFC <ulink
625                                                    url="http://www.grimes.demon.co.uk/DCOM/DCOMSpec.htm">[2]</ulink> and is
626           specced out, but in reality the only implementation of it apart from
627           ours is Microsoft's, and as a result there are lots of interfaces
628           which <emphasis>can</emphasis> be used if you want to customize or
629           control DCOM but in practice are badly documented or not documented at
630           all, or exist mostly as interfaces between MIDL generated code and COM
631           itself. Don't pay too much attention to the MSDN definitions of these
632           interfaces and APIs.
633         </para>
634
635         <para>
636           COM proxies and stubs are like any other normal COM object - they are
637           registered in the registry, they can be loaded with CoCreateInstance and
638           so on. They have to be in process (in DLLs) however. They aren't
639           activated directly by COM however, instead the process goes something
640           like this:
641           
642           <itemizedlist>
643             <listitem> <para> COM receives a marshalled interface packet, and retrieves the IID of
644                 the marshalled interface from it </para> </listitem>
645
646
647             <listitem> <para> COM looks in
648               HKEY_CLASSES_ROOT/Interface/{whatever-iid}/ProxyStubClsId32
649               to retrieve the CLSID of another COM object, which
650               implements IPSFactoryBuffer. </para> </listitem>
651             
652             <listitem> <para> IPSFactoryBuffer has only two methods, CreateProxy and CreateStub. COM
653                 calls whichever is appropriate: CreateStub for the server, CreateProxy
654                 for the client. MIDL will normally provide an implementation of this
655                 object for you in the code it generates. </para></listitem>
656           </itemizedlist>
657           
658         </para>
659
660         <para>
661           Once CreateProxy has been called, the resultant object is QueryInterfaced to
662           IRpcProxyBuffer, which only has 1 method, IRpcProxyBuffer::Connect.
663           This method only takes one parameter, the IRpcChannelBuffer object which
664           encapsulates the "RPC Channel" between the client and server.
665         </para>
666
667         <para>
668           On the server side, a similar process is performed - the PSFactoryBuffer
669           is created, CreateStub is called, result is QId to IRpcStubBuffer, and
670           IRpcStubBuffer::Connect is used to link it to the RPC channel.
671         </para>
672
673       </sect2>
674
675       <sect2>
676         <title>RPC CHANNELS</title>
677
678         <para>
679           Remember the RPC runtime? Well, that's not just responsible for
680           marshalling stuff, it also controls the connection and protocols between
681           the client and server. We can ignore the details of this for now,
682           suffice it to say that an RPC Channel is a COM object that implements
683           IRpcChannelBuffer, and it's basically an abstraction of different RPC
684           methods. For instance, in the case of inter-thread marshalling (not
685           covered here) the RPC connection code isn't used, only the NDR
686           marshallers are, so IRpcChannelBuffer in that case isn't actually
687           implemented by RPCRT4 but rather just by the COM/OLE DLLS.
688         </para>
689
690         <para>
691           On this topic, Ove Kaaven says: It depends on the Windows version, I
692           think. Windows 95 and Windows NT 4 certainly had very different models
693           when I looked. I'm pretty sure the Windows 98 version of RPCRT4 was
694           able to dispatch messages directly to individual apartments. I'd be
695           surprised if some similar functionality was not added to Windows
696           2000. After all, if an object on machine A wanted to use an object on
697           machine B in an apartment C, wouldn't it be most efficient if the RPC
698           system knew about apartments and could dispatch the message directly
699           to it? And if RPC does know how to efficiently dispatch to apartments,
700           why should COM duplicate this functionality? There were, however, no
701           unified way to tell RPC about them across Windows versions, so in that
702           old patch of mine, I let the COM/OLE dlls do the apartment dispatch,
703           but even then, the RPC runtime was always involved. After all, it
704           could be quite tricky to tell whether the call is merely interthread,
705           without involving the RPC runtime...
706         </para>
707
708         <para>
709           RPC channels are constructed on the fly by DCOM as part of the
710           marshalling process. So, when you make a call on a COM proxy, it goes
711           like this:
712         </para>
713
714         <para>
715           Your code -&gt; COM proxy object -&gt; RPC Channel -&gt; COM stub object -&gt; Their code
716         </para>
717
718       </sect2>
719
720       <sect2>
721         <title>HOW THIS ACTUALLY WORKS IN WINE</title>
722
723         <para>
724           Right now, Wine does not use the NDR marshallers or RPC to implement its
725           DCOM. When you marshal an interface in Wine, in the server process a
726           _StubMgrThread thread is started. I haven't gone into the stub manager
727           here. The important thing is that eventually a _StubReaderThread is
728           started which accepts marshalled DCOM RPCs, and then passes them to
729           IRpcStubBuffer::Invoke on the correct stub object which in turn
730           demarshals the packet and performs the call. The threads started by our
731           implementation of DCOM are never terminated, they just hang around until
732           the process dies.
733         </para>
734
735         <para>
736           Remember that I said our DCOM doesn't use RPC? Well, you might be
737           thinking "but we use IRpcStubBuffer like we're supposed to ... isn't
738           that provided by MIDL which generates code that uses the NDR APIs?". If
739           so pat yourself on the back, you're still with me. Go get a cup of
740           coffee.
741         </para>
742
743       </sect2>
744
745       <sect2>
746         <title>TYPELIB MARSHALLER</title>
747
748         <para>
749           In fact, the reason for the PSFactoryBuffer layer of indirection is
750           because not all interfaces are marshalled using MIDL generated code.
751           Why not? Well, to understand <emphasis>that</emphasis>
752           you have to see that one of the
753           driving forces behind OLE and by extension DCOM was the development of
754           Visual Basic. Microsoft wanted VB developers to be first class citizens
755           in the COM world, but things like writing IDL and compiling them with a
756           C compiler into DLLs wasn't easy enough.
757         </para>
758
759         <para>
760           So, type libraries were invented. Actually they were invented as part of
761           a parallel line of COM development known as "OLE Automation", but let's
762           not get into that here. Type libraries are basically binary IDL files,
763           except that despite there being two type library formats neither of them
764           can fully express everything expressable in IDL. Anyway, with a type
765           library (which can be embedded as a resource into a DLL) you have
766           another option beyond compiling MIDL output - you can set the
767           ProxyStubClsId32 registry entry for your interfaces to the CLSID of the
768           "type library marshaller" or "universal marshaller". Both terms are
769           used, but in the Wine source it's called the typelib marshaller.
770         </para>
771
772         <para>
773           The type library marshaller constructs proxy and stub objects on the
774           fly. It does so by having generic marshalling glue which reads the
775           information from the type libraries, and takes the parameters directly
776           off the stack. The CreateProxy method actually builds a vtable out of
777           blocks of assembly stitched together which pass control to _xCall, which
778           then does the marshalling. You can see all this magic in
779           dlls/oleaut32/tmarshal.c
780         </para>
781
782         <para>
783           In the case of InstallShield, it actually comes with typelibs for all
784           the interfaces it needs to marshal (fixme: is this right?), but they
785           actually use a mix of MIDL and typelib marshalling. In order to cover up
786           for the fact that we don't really use RPC they're all forced to go via
787           the typelib marshaller - that's what the 1 || hack is for and what the
788           "Registering non-automation type library!" warning is about (I think).
789         </para>
790       </sect2>
791
792       <sect2>
793         <title>WRAPUP</title>
794
795         <para>
796           OK, so there are some (very) basic notes on DCOM. There's a ton of stuff
797           I have not covered:
798         </para>
799         
800         <itemizedlist>
801           <listitem><para> Format strings/MOPs</para></listitem>
802           
803           <listitem><para> Apartments, threading models, inter-thread marshalling</para></listitem>
804           
805           <listitem><para> OXIDs/OIDs, etc, IOXIDResolver</para></listitem>
806           
807           <listitem><para> IRemoteActivation</para></listitem>
808           
809           <listitem><para> Complex/simple pings, distributed garbage collection</para></listitem>
810
811           <listitem><para> Marshalling IDispatch</para></listitem>
812           
813           <listitem><para> Structure of marshalled interface pointers (STDOBJREFs etc)</para></listitem>
814           
815           <listitem><para> Runtime class object registration (CoRegisterClassObject), ROT</para></listitem>
816           
817           <listitem><para> IRemUnknown</para></listitem>
818           
819           <listitem><para> Exactly how InstallShield uses DCOM</para></listitem>
820         </itemizedlist>
821
822         <para>
823           Then there's a bunch of stuff I still don't understand, like ICallFrame,
824           interface pointer swizzling, exactly where and how all this stuff is
825           actually implemented and so on.
826         </para>
827
828         <para>
829           But for now that's enough.
830         </para>
831       </sect2>
832
833       <sect2>
834         <title>FURTHER READING</title>
835
836         <para>
837           Most of these documents assume you have knowledge only contained in
838           other documents. You may have to reread them a few times for it all to
839           make sense. Don't feel you need to read these to understand DCOM, you
840           don't, you only need to look at them if you're planning to help
841           implement it.
842         </para>
843
844         <itemizedlist>
845             <listitem><para>
846               <ulink url="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmi_n2p_459u.asp">
847                 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmi_n2p_459u.asp</ulink>
848
849             </para></listitem>
850
851
852             <listitem><para>
853               <ulink url="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmi_q2z_5ygi.asp">
854                 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/cmi_q2z_5ygi.asp</ulink>
855             </para></listitem>
856
857
858             <listitem><para>
859                 <ulink url="http://www.microsoft.com/msj/0398/dcom.aspx">
860                   http://www.microsoft.com/msj/0398/dcom.aspx</ulink>
861             </para></listitem>
862
863             <listitem><para>
864                 <ulink url="http://www.microsoft.com/ntserver/techresources/appserv/COM/DCOM/4_ConnectionMgmt.asp">
865                   http://www.microsoft.com/ntserver/techresources/appserv/COM/DCOM/4_ConnectionMgmt.asp</ulink>
866             </para></listitem>
867
868
869             <listitem><para><ulink url="http://www.idevresource.com/com/library/articles/comonlinux.asp">
870                   http://www.idevresource.com/com/library/articles/comonlinux.asp</ulink>
871
872                 (unfortunately part 2 of this article does not seem to exist anymore, if it was ever written)</para></listitem>
873         </itemizedlist>
874       </sect2>
875     </sect1>
876   </chapter>
877
878 <!-- Keep this comment at the end of the file
879 Local variables:
880 mode: sgml
881 sgml-parent-document:("wine-devel.sgml" "set" "book" "part" "chapter" "")
882 End:
883 -->