We must include windef.h before wtypes.h (directly or indirectly).
[wine] / dlls / dplayx / dplobby.c
1 /* Direct Play Lobby 2 & 3 Implementation
2  *
3  * Copyright 1998,1999,2000 - Peter Hunnisett
4  *
5  * <presently under construction - contact hunnise@nortelnetworks.com>
6  *
7  */
8 #include "winerror.h"
9 #include "winnt.h"
10 #include "winreg.h"
11 #include "heap.h"
12 #include "debugtools.h"
13
14 #include "dplobby.h"
15 #include "dpinit.h"
16 #include "dplayx_global.h"
17 #include "dplayx_messages.h"
18 #include "dplayx_queue.h"
19
20 DEFAULT_DEBUG_CHANNEL(dplay)
21
22 /*****************************************************************************
23  * Predeclare the interface implementation structures
24  */
25 typedef struct IDirectPlayLobbyImpl  IDirectPlayLobbyAImpl;
26 typedef struct IDirectPlayLobbyImpl  IDirectPlayLobbyWImpl;
27 typedef struct IDirectPlayLobby2Impl IDirectPlayLobby2AImpl;
28 typedef struct IDirectPlayLobby2Impl IDirectPlayLobby2WImpl;
29 typedef struct IDirectPlayLobby3Impl IDirectPlayLobby3AImpl;
30 typedef struct IDirectPlayLobby3Impl IDirectPlayLobby3WImpl;
31
32 /* Forward declarations for this module helper methods */
33 HRESULT DPL_CreateCompoundAddress ( LPCDPCOMPOUNDADDRESSELEMENT lpElements, DWORD dwElementCount,
34                                     LPVOID lpAddress, LPDWORD lpdwAddressSize, BOOL bAnsiInterface );
35
36 HRESULT DPL_CreateAddress( REFGUID guidSP, REFGUID guidDataType, LPCVOID lpData, DWORD dwDataSize, 
37                            LPVOID lpAddress, LPDWORD lpdwAddressSize, BOOL bAnsiInterface );
38
39
40
41 extern HRESULT DPL_EnumAddress( LPDPENUMADDRESSCALLBACK lpEnumAddressCallback, LPCVOID lpAddress,
42                                 DWORD dwAddressSize, LPVOID lpContext );
43
44 static HRESULT WINAPI DPL_ConnectEx( IDirectPlayLobbyAImpl* This, 
45                                      DWORD dwFlags, REFIID riid, 
46                                      LPVOID* lplpDP, IUnknown* pUnk );
47
48 BOOL DPL_CreateAndSetLobbyHandles( DWORD dwDestProcessId, HANDLE hDestProcess,
49                                    LPHANDLE lphStart, LPHANDLE lphDeath,
50                                    LPHANDLE lphRead );
51
52
53 /*****************************************************************************
54  * IDirectPlayLobby {1,2,3} implementation structure
55  * 
56  * The philosophy behind this extra pointer derefernce is that I wanted to 
57  * have the same structure for all types of objects without having to do
58  * alot of casting. I also only wanted to implement an interface in the 
59  * object it was "released" with IUnknown interface being implemented in the 1 version.
60  * Of course, with these new interfaces comes the data required to keep the state required
61  * by these interfaces. So, basically, the pointers contain the data associated with
62  * a release. If you use the data associated with release 3 in a release 2 object, you'll
63  * get a run time trap, as that won't have any data.
64  *
65  */
66 struct DPLMSG
67 {
68   DPQ_ENTRY( DPLMSG ) msgs;  /* Link to next queued message */
69 };
70 typedef struct DPLMSG* LPDPLMSG;
71
72 typedef struct tagDirectPlayLobbyIUnknownData
73 {
74   ULONG             ulObjRef;
75   CRITICAL_SECTION  DPL_lock;
76 } DirectPlayLobbyIUnknownData;
77
78 typedef struct tagDirectPlayLobbyData
79 {
80   HKEY  hkCallbackKeyHack;
81   DWORD dwMsgThread;
82   DPQ_HEAD( DPLMSG ) msgs;  /* List of messages received */
83 } DirectPlayLobbyData;
84
85 typedef struct tagDirectPlayLobby2Data
86 {
87   BOOL dummy;
88 } DirectPlayLobby2Data;
89
90 typedef struct tagDirectPlayLobby3Data
91 {
92   BOOL dummy;
93 } DirectPlayLobby3Data;
94
95 #define DPL_IMPL_FIELDS \
96  ULONG ulInterfaceRef; \
97  DirectPlayLobbyIUnknownData*  unk; \
98  DirectPlayLobbyData*          dpl; \
99  DirectPlayLobby2Data*         dpl2; \
100  DirectPlayLobby3Data*         dpl3;
101
102 struct IDirectPlayLobbyImpl
103 {
104     ICOM_VFIELD(IDirectPlayLobby);
105     DPL_IMPL_FIELDS
106 };
107
108 struct IDirectPlayLobby2Impl
109 {
110     ICOM_VFIELD(IDirectPlayLobby2);
111     DPL_IMPL_FIELDS
112 };
113
114 struct IDirectPlayLobby3Impl
115 {
116     ICOM_VFIELD(IDirectPlayLobby3);
117     DPL_IMPL_FIELDS
118 };
119
120
121 /* Forward declarations of virtual tables */
122 static ICOM_VTABLE(IDirectPlayLobby)  directPlayLobbyWVT;
123 static ICOM_VTABLE(IDirectPlayLobby2) directPlayLobby2WVT;
124 static ICOM_VTABLE(IDirectPlayLobby3) directPlayLobby3WVT;
125
126 static ICOM_VTABLE(IDirectPlayLobby)  directPlayLobbyAVT;
127 static ICOM_VTABLE(IDirectPlayLobby2) directPlayLobby2AVT;
128 static ICOM_VTABLE(IDirectPlayLobby3) directPlayLobby3AVT;
129
130
131
132
133 static BOOL DPL_CreateIUnknown( LPVOID lpDPL ) 
134 {
135   ICOM_THIS(IDirectPlayLobbyAImpl,lpDPL);
136
137   This->unk = (DirectPlayLobbyIUnknownData*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
138                                                        sizeof( *(This->unk) ) ); 
139   if ( This->unk == NULL )
140   {
141     return FALSE; 
142   }
143
144   InitializeCriticalSection( &This->unk->DPL_lock );
145
146   return TRUE;
147 }
148
149 static BOOL DPL_DestroyIUnknown( LPVOID lpDPL )
150 {
151   ICOM_THIS(IDirectPlayLobbyAImpl,lpDPL);
152
153   DeleteCriticalSection( &This->unk->DPL_lock );
154   HeapFree( GetProcessHeap(), 0, This->unk );
155
156   return TRUE;
157 }
158
159 static BOOL DPL_CreateLobby1( LPVOID lpDPL )
160 {
161   ICOM_THIS(IDirectPlayLobbyAImpl,lpDPL);
162
163   This->dpl = (DirectPlayLobbyData*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
164                                                sizeof( *(This->dpl) ) );
165   if ( This->dpl == NULL )
166   {
167     return FALSE;
168   }
169
170   DPQ_INIT( This->dpl->msgs ); 
171
172   return TRUE;  
173 }
174
175 static BOOL DPL_DestroyLobby1( LPVOID lpDPL )
176 {
177   ICOM_THIS(IDirectPlayLobbyAImpl,lpDPL);
178
179   if( This->dpl->dwMsgThread )
180   {
181     FIXME( "Should kill the msg thread\n" );
182   }
183
184   DPQ_DELETEQ( This->dpl->msgs, msgs, LPDPLMSG, cbDeleteElemFromHeap );
185
186   /* Delete the contents */
187   HeapFree( GetProcessHeap(), 0, This->dpl );
188
189   return TRUE;
190 }
191
192 static BOOL DPL_CreateLobby2( LPVOID lpDPL )
193 {
194   ICOM_THIS(IDirectPlayLobby2AImpl,lpDPL);
195
196   This->dpl2 = (DirectPlayLobby2Data*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
197                                                  sizeof( *(This->dpl2) ) );
198   if ( This->dpl2 == NULL )
199   {
200     return FALSE;
201   }
202
203   return TRUE;
204 }
205
206 static BOOL DPL_DestroyLobby2( LPVOID lpDPL )
207 {
208   ICOM_THIS(IDirectPlayLobby2AImpl,lpDPL);
209
210   HeapFree( GetProcessHeap(), 0, This->dpl2 );
211
212   return TRUE;
213 }
214
215 static BOOL DPL_CreateLobby3( LPVOID lpDPL )
216 {
217   ICOM_THIS(IDirectPlayLobby3AImpl,lpDPL);
218
219   This->dpl3 = (DirectPlayLobby3Data*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
220                                                  sizeof( *(This->dpl3) ) );
221   if ( This->dpl3 == NULL )
222   {
223     return FALSE;
224   }
225
226   return TRUE;
227 }
228
229 static BOOL DPL_DestroyLobby3( LPVOID lpDPL )
230 {
231   ICOM_THIS(IDirectPlayLobby3AImpl,lpDPL);
232
233   HeapFree( GetProcessHeap(), 0, This->dpl3 );
234
235   return TRUE;
236 }
237
238
239 /* The COM interface for upversioning an interface
240  * We've been given a GUID (riid) and we need to replace the present
241  * interface with that of the requested interface.
242  *
243  * Snip from some Microsoft document:
244  * There are four requirements for implementations of QueryInterface (In these
245  * cases, "must succeed" means "must succeed barring catastrophic failure."):
246  *
247  *  * The set of interfaces accessible on an object through
248  *    IUnknown::QueryInterface must be static, not dynamic. This means that
249  *    if a call to QueryInterface for a pointer to a specified interface
250  *    succeeds the first time, it must succeed again, and if it fails the
251  *    first time, it must fail on all subsequent queries.
252  *  * It must be symmetric ~W if a client holds a pointer to an interface on
253  *    an object, and queries for that interface, the call must succeed.
254  *  * It must be reflexive ~W if a client holding a pointer to one interface
255  *    queries successfully for another, a query through the obtained pointer
256  *    for the first interface must succeed.
257  *  * It must be transitive ~W if a client holding a pointer to one interface
258  *    queries successfully for a second, and through that pointer queries
259  *    successfully for a third interface, a query for the first interface
260  *    through the pointer for the third interface must succeed.
261  */
262 extern 
263 HRESULT DPL_CreateInterface
264          ( REFIID riid, LPVOID* ppvObj )
265 {
266   TRACE( " for %s\n", debugstr_guid( riid ) );
267
268   *ppvObj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
269                        sizeof( IDirectPlayLobbyWImpl ) );
270
271   if( *ppvObj == NULL )
272   {
273     return DPERR_OUTOFMEMORY;
274   }
275
276   if( IsEqualGUID( &IID_IDirectPlayLobby, riid ) )
277   {
278     ICOM_THIS(IDirectPlayLobbyWImpl,*ppvObj);
279     ICOM_VTBL(This) = &directPlayLobbyWVT;
280   }
281   else if( IsEqualGUID( &IID_IDirectPlayLobbyA, riid ) )
282   {
283     ICOM_THIS(IDirectPlayLobbyAImpl,*ppvObj);
284     ICOM_VTBL(This) = &directPlayLobbyAVT;
285   }
286   else if( IsEqualGUID( &IID_IDirectPlayLobby2, riid ) )
287   {
288     ICOM_THIS(IDirectPlayLobby2WImpl,*ppvObj);
289     ICOM_VTBL(This) = &directPlayLobby2WVT;
290   }
291   else if( IsEqualGUID( &IID_IDirectPlayLobby2A, riid ) )
292   {
293     ICOM_THIS(IDirectPlayLobby2AImpl,*ppvObj);
294     ICOM_VTBL(This) = &directPlayLobby2AVT;
295   }
296   else if( IsEqualGUID( &IID_IDirectPlayLobby3, riid ) )
297   {
298     ICOM_THIS(IDirectPlayLobby3WImpl,*ppvObj);
299     ICOM_VTBL(This) = &directPlayLobby3WVT;
300   }
301   else if( IsEqualGUID( &IID_IDirectPlayLobby3A, riid ) )
302   {
303     ICOM_THIS(IDirectPlayLobby3AImpl,*ppvObj);
304     ICOM_VTBL(This) = &directPlayLobby3AVT;
305   }
306   else
307   {
308     /* Unsupported interface */
309     HeapFree( GetProcessHeap(), 0, *ppvObj );
310     *ppvObj = NULL;
311
312     return E_NOINTERFACE;
313   }
314   
315   /* Initialize it */
316   if ( DPL_CreateIUnknown( *ppvObj ) &&
317        DPL_CreateLobby1( *ppvObj ) &&
318        DPL_CreateLobby2( *ppvObj ) &&
319        DPL_CreateLobby3( *ppvObj )
320      )
321   { 
322     IDirectPlayLobby_AddRef( (LPDIRECTPLAYLOBBY)*ppvObj );
323     return S_OK;
324   }
325
326   /* Initialize failed, destroy it */
327   DPL_DestroyLobby3( *ppvObj );
328   DPL_DestroyLobby2( *ppvObj );
329   DPL_DestroyLobby1( *ppvObj );
330   DPL_DestroyIUnknown( *ppvObj );
331   HeapFree( GetProcessHeap(), 0, *ppvObj );
332
333   *ppvObj = NULL;
334   return DPERR_NOMEMORY;
335 }
336
337 static HRESULT WINAPI DPL_QueryInterface
338 ( LPDIRECTPLAYLOBBYA iface,
339   REFIID riid,
340   LPVOID* ppvObj )
341 {
342   ICOM_THIS(IDirectPlayLobbyAImpl,iface);
343   TRACE("(%p)->(%s,%p)\n", This, debugstr_guid( riid ), ppvObj );
344
345   *ppvObj = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
346                        sizeof( *This ) );
347
348   if( *ppvObj == NULL )
349   {
350     return DPERR_OUTOFMEMORY;
351   }
352
353   CopyMemory( *ppvObj, This, sizeof( *This )  );
354   (*(IDirectPlayLobbyAImpl**)ppvObj)->ulInterfaceRef = 0;
355
356   if( IsEqualGUID( &IID_IDirectPlayLobby, riid ) )
357   {
358     ICOM_THIS(IDirectPlayLobbyWImpl,*ppvObj);
359     ICOM_VTBL(This) = &directPlayLobbyWVT;
360   }
361   else if( IsEqualGUID( &IID_IDirectPlayLobbyA, riid ) )
362   {
363     ICOM_THIS(IDirectPlayLobbyAImpl,*ppvObj);
364     ICOM_VTBL(This) = &directPlayLobbyAVT;
365   }
366   else if( IsEqualGUID( &IID_IDirectPlayLobby2, riid ) )
367   {
368     ICOM_THIS(IDirectPlayLobby2WImpl,*ppvObj);
369     ICOM_VTBL(This) = &directPlayLobby2WVT;
370   }
371   else if( IsEqualGUID( &IID_IDirectPlayLobby2A, riid ) )
372   {
373     ICOM_THIS(IDirectPlayLobby2AImpl,*ppvObj);
374     ICOM_VTBL(This) = &directPlayLobby2AVT;
375   }
376   else if( IsEqualGUID( &IID_IDirectPlayLobby3, riid ) )
377   {
378     ICOM_THIS(IDirectPlayLobby3WImpl,*ppvObj);
379     ICOM_VTBL(This) = &directPlayLobby3WVT;
380   }
381   else if( IsEqualGUID( &IID_IDirectPlayLobby3A, riid ) )
382   {
383     ICOM_THIS(IDirectPlayLobby3AImpl,*ppvObj);
384     ICOM_VTBL(This) = &directPlayLobby3AVT;
385   }
386   else
387   {
388     /* Unsupported interface */
389     HeapFree( GetProcessHeap(), 0, *ppvObj );
390     *ppvObj = NULL;
391
392     return E_NOINTERFACE;
393   }
394
395   IDirectPlayLobby_AddRef( (LPDIRECTPLAYLOBBY)*ppvObj );
396
397   return S_OK;
398 }
399
400 /* 
401  * Simple procedure. Just increment the reference count to this
402  * structure and return the new reference count.
403  */
404 static ULONG WINAPI DPL_AddRef
405 ( LPDIRECTPLAYLOBBY iface )
406 {
407   ULONG ulInterfaceRefCount, ulObjRefCount;
408   ICOM_THIS(IDirectPlayLobbyWImpl,iface);
409
410   ulObjRefCount       = InterlockedIncrement( &This->unk->ulObjRef );
411   ulInterfaceRefCount = InterlockedIncrement( &This->ulInterfaceRef );
412
413   TRACE( "ref count incremented to %lu:%lu for %p\n",
414          ulInterfaceRefCount, ulObjRefCount, This );
415
416   return ulObjRefCount;
417 }
418
419 /*
420  * Simple COM procedure. Decrease the reference count to this object.
421  * If the object no longer has any reference counts, free up the associated
422  * memory.
423  */
424 static ULONG WINAPI DPL_Release
425 ( LPDIRECTPLAYLOBBYA iface )
426 {
427   ULONG ulInterfaceRefCount, ulObjRefCount;
428   ICOM_THIS(IDirectPlayLobbyAImpl,iface);
429
430   ulObjRefCount       = InterlockedDecrement( &This->unk->ulObjRef );
431   ulInterfaceRefCount = InterlockedDecrement( &This->ulInterfaceRef );
432
433   TRACE( "ref count decremented to %lu:%lu for %p\n",
434          ulInterfaceRefCount, ulObjRefCount, This );
435
436   /* Deallocate if this is the last reference to the object */
437   if( ulObjRefCount == 0 )
438   {
439      DPL_DestroyLobby3( This );
440      DPL_DestroyLobby2( This );
441      DPL_DestroyLobby1( This );
442      DPL_DestroyIUnknown( This );
443   }
444
445   if( ulInterfaceRefCount == 0 )
446   {
447     HeapFree( GetProcessHeap(), 0, This );
448   }
449
450   return ulInterfaceRefCount;
451 }
452
453
454 /********************************************************************
455  * 
456  * Connects an application to the session specified by the DPLCONNECTION
457  * structure currently stored with the DirectPlayLobby object.
458  *
459  * Returns a IDirectPlay interface.
460  *
461  */
462 static HRESULT WINAPI DPL_ConnectEx
463 ( IDirectPlayLobbyAImpl* This, 
464   DWORD     dwFlags,
465   REFIID    riid, 
466   LPVOID*   lplpDP,
467   IUnknown* pUnk)
468 {
469   HRESULT         hr;
470   DWORD           dwOpenFlags = 0;
471   DWORD           dwConnSize = 0;
472   LPDPLCONNECTION lpConn;
473
474   FIXME("(%p)->(0x%08lx,%p,%p): semi stub\n", This, dwFlags, lplpDP, pUnk );
475
476   if( pUnk )
477   {
478      return DPERR_INVALIDPARAMS;
479   }
480
481   /* Backwards compatibility */
482   if( dwFlags == 0 )
483   {
484     dwFlags = DPCONNECT_RETURNSTATUS;
485   }
486
487   /* Create the DirectPlay interface */
488   if( ( hr = DP_CreateInterface( riid, lplpDP ) ) != DP_OK )
489   {
490      ERR( "error creating interface for %s:%s.\n", 
491           debugstr_guid( riid ), DPLAYX_HresultToString( hr ) );
492      return hr;
493   }
494
495   /* FIXME: Is it safe/correct to use appID of 0? */
496   hr = IDirectPlayLobby_GetConnectionSettings( (LPDIRECTPLAYLOBBY)This, 
497                                                0, NULL, &dwConnSize ); 
498   if( hr != DPERR_BUFFERTOOSMALL )
499   {
500     return hr;
501   }
502  
503   lpConn = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, dwConnSize );
504
505   if( lpConn == NULL )
506   {
507     return DPERR_NOMEMORY;
508   }
509
510   /* FIXME: Is it safe/correct to use appID of 0? */
511   hr = IDirectPlayLobby_GetConnectionSettings( (LPDIRECTPLAYLOBBY)This, 
512                                                0, lpConn, &dwConnSize );
513   if( FAILED( hr ) )
514   {
515     return hr;
516   }
517
518 #if 0
519   /* - Need to call IDirectPlay::EnumConnections with the service provider to get that good information
520    * - Need to call CreateAddress to create the lpConnection param for IDirectPlay::InitializeConnection
521    * - Call IDirectPlay::InitializeConnection
522    */
523
524   /* Now initialize the Service Provider */
525   hr = IDirectPlayX_InitializeConnection( (*(LPDIRECTPLAY2*)lplpDP),
526 #endif
527                                           
528
529   /* Setup flags to pass into DirectPlay::Open */
530   if( dwFlags & DPCONNECT_RETURNSTATUS )
531   {
532     dwOpenFlags |= DPOPEN_RETURNSTATUS;
533   }
534   dwOpenFlags |= lpConn->dwFlags;
535
536   hr = IDirectPlayX_Open( (*(LPDIRECTPLAY2*)lplpDP), lpConn->lpSessionDesc, 
537                           dwOpenFlags );
538
539   HeapFree( GetProcessHeap(), 0, lpConn );
540
541   return hr;
542 }
543
544 static HRESULT WINAPI IDirectPlayLobbyAImpl_Connect
545 ( LPDIRECTPLAYLOBBYA iface,
546   DWORD dwFlags,
547   LPDIRECTPLAY2A* lplpDP,
548   IUnknown* pUnk)
549 {
550   ICOM_THIS(IDirectPlayLobbyAImpl,iface);
551   return DPL_ConnectEx( This, dwFlags, &IID_IDirectPlay2A, 
552                         (LPVOID)lplpDP, pUnk );
553 }
554
555 static HRESULT WINAPI IDirectPlayLobbyWImpl_Connect
556 ( LPDIRECTPLAYLOBBY iface,
557   DWORD dwFlags,
558   LPDIRECTPLAY2* lplpDP,
559   IUnknown* pUnk)
560 {
561   ICOM_THIS(IDirectPlayLobbyAImpl,iface); /* Yes cast to A */
562   return DPL_ConnectEx( This, dwFlags, &IID_IDirectPlay2, 
563                         (LPVOID)lplpDP, pUnk );
564 }
565
566 /********************************************************************
567  *
568  * Creates a DirectPlay Address, given a service provider-specific network
569  * address. 
570  * Returns an address contains the globally unique identifier
571  * (GUID) of the service provider and data that the service provider can
572  * interpret as a network address.
573  *
574  * NOTE: It appears that this method is supposed to be really really stupid
575  *       with no error checking on the contents.
576  */
577 static HRESULT WINAPI IDirectPlayLobbyAImpl_CreateAddress
578 ( LPDIRECTPLAYLOBBYA iface,
579   REFGUID guidSP,
580   REFGUID guidDataType,
581   LPCVOID lpData, 
582   DWORD dwDataSize,
583   LPVOID lpAddress, 
584   LPDWORD lpdwAddressSize )
585 {
586   return DPL_CreateAddress( guidSP, guidDataType, lpData, dwDataSize, 
587                             lpAddress, lpdwAddressSize, TRUE ); 
588 }
589
590 static HRESULT WINAPI IDirectPlayLobbyWImpl_CreateAddress
591 ( LPDIRECTPLAYLOBBY iface,
592   REFGUID guidSP,
593   REFGUID guidDataType,
594   LPCVOID lpData,
595   DWORD dwDataSize,
596   LPVOID lpAddress,
597   LPDWORD lpdwAddressSize )
598 {
599   return DPL_CreateAddress( guidSP, guidDataType, lpData, dwDataSize,
600                             lpAddress, lpdwAddressSize, FALSE );
601 }
602
603 HRESULT DPL_CreateAddress(
604   REFGUID guidSP,
605   REFGUID guidDataType,
606   LPCVOID lpData,
607   DWORD dwDataSize,
608   LPVOID lpAddress,
609   LPDWORD lpdwAddressSize,
610   BOOL bAnsiInterface )
611 {
612   const DWORD dwNumAddElements = 2; /* Service Provide & address data type */
613   DPCOMPOUNDADDRESSELEMENT addressElements[ 2 /* dwNumAddElements */ ];
614
615   TRACE( "(%p)->(%p,%p,0x%08lx,%p,%p,%d)\n", guidSP, guidDataType, lpData, dwDataSize, 
616                                              lpAddress, lpdwAddressSize, bAnsiInterface );
617
618   addressElements[ 0 ].guidDataType = DPAID_ServiceProvider;
619   addressElements[ 0 ].dwDataSize = sizeof( GUID );
620   addressElements[ 0 ].lpData = (LPVOID)guidSP;
621
622   addressElements[ 1 ].guidDataType = *guidDataType;
623   addressElements[ 1 ].dwDataSize = dwDataSize;
624   addressElements[ 1 ].lpData = (LPVOID)lpData;
625
626   /* Call CreateCompoundAddress to cut down on code.
627      NOTE: We can do this because we don't support DPL 1 interfaces! */
628   return DPL_CreateCompoundAddress( addressElements, dwNumAddElements,
629                                     lpAddress, lpdwAddressSize, bAnsiInterface );
630 }
631
632
633
634 /********************************************************************
635  *
636  * Parses out chunks from the DirectPlay Address buffer by calling the
637  * given callback function, with lpContext, for each of the chunks.
638  *
639  */
640 static HRESULT WINAPI IDirectPlayLobbyAImpl_EnumAddress
641 ( LPDIRECTPLAYLOBBYA iface,
642   LPDPENUMADDRESSCALLBACK lpEnumAddressCallback,
643   LPCVOID lpAddress,
644   DWORD dwAddressSize,
645   LPVOID lpContext )
646 {
647   ICOM_THIS(IDirectPlayLobbyAImpl,iface);
648
649   TRACE("(%p)->(%p,%p,0x%08lx,%p)\n", This, lpEnumAddressCallback, lpAddress, 
650                                       dwAddressSize, lpContext );
651
652   return DPL_EnumAddress( lpEnumAddressCallback, lpAddress, dwAddressSize, lpContext );
653 }
654   
655 static HRESULT WINAPI IDirectPlayLobbyWImpl_EnumAddress
656 ( LPDIRECTPLAYLOBBY iface,
657   LPDPENUMADDRESSCALLBACK lpEnumAddressCallback,
658   LPCVOID lpAddress,
659   DWORD dwAddressSize,
660   LPVOID lpContext )
661 {
662   ICOM_THIS(IDirectPlayLobbyWImpl,iface);
663
664   TRACE("(%p)->(%p,%p,0x%08lx,%p)\n", This, lpEnumAddressCallback, lpAddress,     
665                                       dwAddressSize, lpContext );
666
667   return DPL_EnumAddress( lpEnumAddressCallback, lpAddress, dwAddressSize, lpContext );
668 }
669
670 extern HRESULT DPL_EnumAddress( LPDPENUMADDRESSCALLBACK lpEnumAddressCallback, LPCVOID lpAddress,
671                                 DWORD dwAddressSize, LPVOID lpContext )
672
673   DWORD dwTotalSizeEnumerated = 0;
674
675   /* FIXME: First chunk is always the total size chunk - Should we report it? */ 
676
677   while ( dwTotalSizeEnumerated < dwAddressSize )
678   {
679     LPDPADDRESS lpElements = (LPDPADDRESS)lpAddress;
680     DWORD dwSizeThisEnumeration; 
681
682     /* Invoke the enum method. If false is returned, stop enumeration */
683     if ( !lpEnumAddressCallback( &lpElements->guidDataType, 
684                                  lpElements->dwDataSize, 
685                                  (BYTE*)lpElements + sizeof( DPADDRESS ), 
686                                  lpContext ) )
687     {
688       break;
689     }
690
691     dwSizeThisEnumeration  = sizeof( DPADDRESS ) + lpElements->dwDataSize;
692     lpAddress = (BYTE*) lpAddress + dwSizeThisEnumeration;
693     dwTotalSizeEnumerated += dwSizeThisEnumeration;
694   }
695
696   return DP_OK;
697 }
698
699 /********************************************************************
700  *
701  * Enumerates all the address types that a given service provider needs to
702  * build the DirectPlay Address.
703  *
704  */
705 static HRESULT WINAPI IDirectPlayLobbyAImpl_EnumAddressTypes
706 ( LPDIRECTPLAYLOBBYA iface,
707   LPDPLENUMADDRESSTYPESCALLBACK lpEnumAddressTypeCallback,
708   REFGUID guidSP,
709   LPVOID lpContext,
710   DWORD dwFlags )
711 {
712   ICOM_THIS(IDirectPlayLobbyAImpl,iface);
713
714   HKEY   hkResult;
715   LPCSTR searchSubKey    = "SOFTWARE\\Microsoft\\DirectPlay\\Service Providers";
716   DWORD  dwIndex, sizeOfSubKeyName=50;
717   char   subKeyName[51];
718   FILETIME filetime;
719
720   TRACE(" (%p)->(%p,%p,%p,0x%08lx)\n", This, lpEnumAddressTypeCallback, guidSP, lpContext, dwFlags );
721
722   if( dwFlags != 0 )
723   {
724     return DPERR_INVALIDPARAMS;
725   }
726
727   if( !lpEnumAddressTypeCallback || !*lpEnumAddressTypeCallback )
728   {
729      return DPERR_INVALIDPARAMS;
730   }
731
732   if( guidSP == NULL )
733   {
734     return DPERR_INVALIDOBJECT;
735   }
736
737     /* Need to loop over the service providers in the registry */
738     if( RegOpenKeyExA( HKEY_LOCAL_MACHINE, searchSubKey,
739                          0, KEY_READ, &hkResult ) != ERROR_SUCCESS )
740     {
741       /* Hmmm. Does this mean that there are no service providers? */
742       ERR(": no service providers?\n");
743       return DP_OK;
744     }
745
746     /* Traverse all the service providers we have available */
747     for( dwIndex=0;
748          RegEnumKeyExA( hkResult, dwIndex, subKeyName, &sizeOfSubKeyName,
749                         NULL, NULL, NULL, &filetime ) != ERROR_NO_MORE_ITEMS;
750          ++dwIndex, sizeOfSubKeyName=50 )
751     {
752
753       HKEY     hkServiceProvider, hkServiceProviderAt;
754       GUID     serviceProviderGUID;
755       DWORD    returnTypeGUID, sizeOfReturnBuffer = 50;
756       char     atSubKey[51];
757       char     returnBuffer[51];
758       LPWSTR   lpWGUIDString;
759       DWORD    dwAtIndex;
760       LPSTR    atKey = "Address Types";
761       LPSTR    guidDataSubKey   = "Guid";
762       FILETIME filetime;
763
764
765       TRACE(" this time through: %s\n", subKeyName );
766
767       /* Get a handle for this particular service provider */
768       if( RegOpenKeyExA( hkResult, subKeyName, 0, KEY_READ,
769                          &hkServiceProvider ) != ERROR_SUCCESS )
770       {
771          ERR(": what the heck is going on?\n" );
772          continue;
773       }
774
775       if( RegQueryValueExA( hkServiceProvider, guidDataSubKey,
776                             NULL, &returnTypeGUID, returnBuffer,
777                             &sizeOfReturnBuffer ) != ERROR_SUCCESS )
778       {
779         ERR(": missing GUID registry data members\n" );
780         continue;
781       }
782
783       /* FIXME: Check return types to ensure we're interpreting data right */
784       lpWGUIDString = HEAP_strdupAtoW( GetProcessHeap(), 0, returnBuffer );
785       CLSIDFromString( (LPCOLESTR)lpWGUIDString, &serviceProviderGUID );
786       HeapFree( GetProcessHeap(), 0, lpWGUIDString );
787       /* FIXME: Have I got a memory leak on the serviceProviderGUID? */
788
789       /* Determine if this is the Service Provider that the user asked for */
790       if( !IsEqualGUID( &serviceProviderGUID, guidSP ) )
791       {
792         continue;
793       }
794
795       /* Get a handle for this particular service provider */
796       if( RegOpenKeyExA( hkServiceProvider, atKey, 0, KEY_READ,
797                          &hkServiceProviderAt ) != ERROR_SUCCESS )
798       {
799         TRACE(": No Address Types registry data sub key/members\n" );
800         break;
801       }
802
803       /* Traverse all the address type we have available */
804       for( dwAtIndex=0;
805            RegEnumKeyExA( hkServiceProviderAt, dwAtIndex, atSubKey, &sizeOfSubKeyName,
806                           NULL, NULL, NULL, &filetime ) != ERROR_NO_MORE_ITEMS;
807            ++dwAtIndex, sizeOfSubKeyName=50 )
808       {
809         TRACE( "Found Address Type GUID %s\n", atSubKey );
810
811         /* FIXME: Check return types to ensure we're interpreting data right */
812         lpWGUIDString = HEAP_strdupAtoW( GetProcessHeap(), 0, atSubKey );
813         CLSIDFromString( (LPCOLESTR)lpWGUIDString, &serviceProviderGUID );
814         HeapFree( GetProcessHeap(), 0, lpWGUIDString );
815         /* FIXME: Have I got a memory leak on the serviceProviderGUID? */
816     
817         /* The enumeration will return FALSE if we are not to continue */
818         if( !lpEnumAddressTypeCallback( &serviceProviderGUID, lpContext, 0 ) )
819         {
820            WARN("lpEnumCallback returning FALSE\n" );
821            break; /* FIXME: This most likely has to break from the procedure...*/
822         }
823
824       }
825
826       /* We only enumerate address types for 1 GUID. We've found it, so quit looking */
827       break;
828     } 
829
830   return DP_OK;
831 }
832
833 static HRESULT WINAPI IDirectPlayLobbyWImpl_EnumAddressTypes
834 ( LPDIRECTPLAYLOBBY iface,
835   LPDPLENUMADDRESSTYPESCALLBACK lpEnumAddressTypeCallback,
836   REFGUID guidSP,
837   LPVOID lpContext,
838   DWORD dwFlags )
839 {
840   FIXME(":stub\n");
841   return DPERR_OUTOFMEMORY;
842 }
843
844 /********************************************************************
845  *
846  * Enumerates what applications are registered with DirectPlay by
847  * invoking the callback function with lpContext.
848  *
849  */
850 static HRESULT WINAPI IDirectPlayLobbyWImpl_EnumLocalApplications
851 ( LPDIRECTPLAYLOBBY iface,
852   LPDPLENUMLOCALAPPLICATIONSCALLBACK lpEnumLocalAppCallback,
853   LPVOID lpContext,
854   DWORD dwFlags )
855 {
856   ICOM_THIS(IDirectPlayLobbyWImpl,iface);
857
858   FIXME("(%p)->(%p,%p,0x%08lx):stub\n", This, lpEnumLocalAppCallback, lpContext, dwFlags );
859
860   return DPERR_OUTOFMEMORY;
861 }
862
863 static HRESULT WINAPI IDirectPlayLobbyAImpl_EnumLocalApplications
864 ( LPDIRECTPLAYLOBBYA iface,
865   LPDPLENUMLOCALAPPLICATIONSCALLBACK lpEnumLocalAppCallback,
866   LPVOID lpContext,
867   DWORD dwFlags )
868 {
869   ICOM_THIS(IDirectPlayLobbyAImpl,iface);
870
871   HKEY hkResult;
872   LPCSTR searchSubKey    = "SOFTWARE\\Microsoft\\DirectPlay\\Applications";
873   LPSTR guidDataSubKey   = "Guid";
874   DWORD dwIndex, sizeOfSubKeyName=50;
875   char subKeyName[51];
876   FILETIME filetime;
877
878   TRACE("(%p)->(%p,%p,0x%08lx)\n", This, lpEnumLocalAppCallback, lpContext, dwFlags );
879
880   if( dwFlags != 0 )
881   {
882     return DPERR_INVALIDPARAMS;
883   }
884
885   if( !lpEnumLocalAppCallback || !*lpEnumLocalAppCallback )
886   {
887      return DPERR_INVALIDPARAMS;
888   }
889
890   /* Need to loop over the service providers in the registry */
891   if( RegOpenKeyExA( HKEY_LOCAL_MACHINE, searchSubKey,
892                      0, KEY_READ, &hkResult ) != ERROR_SUCCESS )
893   {
894     /* Hmmm. Does this mean that there are no service providers? */
895     ERR(": no service providers?\n");
896     return DP_OK;
897   }
898
899   /* Traverse all registered applications */
900   for( dwIndex=0;
901        RegEnumKeyExA( hkResult, dwIndex, subKeyName, &sizeOfSubKeyName, NULL, NULL, NULL, &filetime ) != ERROR_NO_MORE_ITEMS;
902        ++dwIndex, sizeOfSubKeyName=50 )
903   {
904
905     HKEY       hkServiceProvider;
906     GUID       serviceProviderGUID;
907     DWORD      returnTypeGUID, sizeOfReturnBuffer = 50;
908     char       returnBuffer[51];
909     LPWSTR     lpWGUIDString;
910     DPLAPPINFO dplAppInfo;
911
912     TRACE(" this time through: %s\n", subKeyName );
913
914     /* Get a handle for this particular service provider */
915     if( RegOpenKeyExA( hkResult, subKeyName, 0, KEY_READ,
916                        &hkServiceProvider ) != ERROR_SUCCESS )
917     {
918        ERR(": what the heck is going on?\n" );
919        continue;
920     }
921
922     if( RegQueryValueExA( hkServiceProvider, guidDataSubKey,
923                           NULL, &returnTypeGUID, returnBuffer,
924                           &sizeOfReturnBuffer ) != ERROR_SUCCESS )
925     {
926       ERR(": missing GUID registry data members\n" );
927       continue;
928     }
929
930     /* FIXME: Check return types to ensure we're interpreting data right */
931     lpWGUIDString = HEAP_strdupAtoW( GetProcessHeap(), 0, returnBuffer );
932     CLSIDFromString( (LPCOLESTR)lpWGUIDString, &serviceProviderGUID );
933     HeapFree( GetProcessHeap(), 0, lpWGUIDString );
934     /* FIXME: Have I got a memory leak on the serviceProviderGUID? */
935
936     dplAppInfo.dwSize               = sizeof( dplAppInfo );
937     dplAppInfo.guidApplication      = serviceProviderGUID;
938     dplAppInfo.appName.lpszAppNameA = subKeyName; 
939
940     EnterCriticalSection( &This->unk->DPL_lock );
941  
942     memcpy( &This->dpl->hkCallbackKeyHack, &hkServiceProvider, sizeof( hkServiceProvider ) );
943
944     if( !lpEnumLocalAppCallback( &dplAppInfo, lpContext, dwFlags ) )
945     {
946        LeaveCriticalSection( &This->unk->DPL_lock );
947        break;
948     }
949
950     LeaveCriticalSection( &This->unk->DPL_lock );
951   }
952
953   return DP_OK;
954 }
955
956 /********************************************************************
957  *
958  * Retrieves the DPLCONNECTION structure that contains all the information
959  * needed to start and connect an application. This was generated using
960  * either the RunApplication or SetConnectionSettings methods.
961  *
962  * NOTES: If lpData is NULL then just return lpdwDataSize. This allows
963  *        the data structure to be allocated by our caller which can then
964  *        call this procedure/method again with a valid data pointer.
965  */
966 static HRESULT WINAPI IDirectPlayLobbyAImpl_GetConnectionSettings
967 ( LPDIRECTPLAYLOBBYA iface,
968   DWORD dwAppID,
969   LPVOID lpData,
970   LPDWORD lpdwDataSize )
971 {
972   ICOM_THIS(IDirectPlayLobbyAImpl,iface);
973   HRESULT hr; 
974
975   TRACE("(%p)->(0x%08lx,%p,%p)\n", This, dwAppID, lpData, lpdwDataSize );
976
977   EnterCriticalSection( &This->unk->DPL_lock );
978
979   hr = DPLAYX_GetConnectionSettingsA( dwAppID, 
980                                       lpData, 
981                                       lpdwDataSize
982                                     );
983
984   LeaveCriticalSection( &This->unk->DPL_lock );
985
986   return hr;
987 }
988
989 static HRESULT WINAPI IDirectPlayLobbyWImpl_GetConnectionSettings
990 ( LPDIRECTPLAYLOBBY iface,
991   DWORD dwAppID,
992   LPVOID lpData,
993   LPDWORD lpdwDataSize )
994 {
995   ICOM_THIS(IDirectPlayLobbyWImpl,iface);
996   HRESULT hr;
997
998   TRACE("(%p)->(0x%08lx,%p,%p)\n", This, dwAppID, lpData, lpdwDataSize );
999  
1000   EnterCriticalSection( &This->unk->DPL_lock );
1001
1002   hr = DPLAYX_GetConnectionSettingsW( dwAppID, 
1003                                       lpData, 
1004                                       lpdwDataSize
1005                                     );
1006
1007   LeaveCriticalSection( &This->unk->DPL_lock );
1008
1009   return hr;
1010 }
1011
1012 /********************************************************************
1013  *
1014  * Retrieves the message sent between a lobby client and a DirectPlay 
1015  * application. All messages are queued until received.
1016  *
1017  */
1018 static HRESULT WINAPI IDirectPlayLobbyAImpl_ReceiveLobbyMessage
1019 ( LPDIRECTPLAYLOBBYA iface,
1020   DWORD dwFlags,
1021   DWORD dwAppID,
1022   LPDWORD lpdwMessageFlags,
1023   LPVOID lpData,
1024   LPDWORD lpdwDataSize )
1025 {
1026   ICOM_THIS(IDirectPlayLobbyAImpl,iface);
1027   FIXME(":stub %p %08lx %08lx %p %p %p\n", This, dwFlags, dwAppID, lpdwMessageFlags, lpData,
1028          lpdwDataSize );
1029   return DPERR_OUTOFMEMORY;
1030 }
1031
1032 static HRESULT WINAPI IDirectPlayLobbyWImpl_ReceiveLobbyMessage
1033 ( LPDIRECTPLAYLOBBY iface,
1034   DWORD dwFlags,
1035   DWORD dwAppID,
1036   LPDWORD lpdwMessageFlags,
1037   LPVOID lpData,
1038   LPDWORD lpdwDataSize )
1039 {
1040   ICOM_THIS(IDirectPlayLobbyWImpl,iface);
1041   FIXME(":stub %p %08lx %08lx %p %p %p\n", This, dwFlags, dwAppID, lpdwMessageFlags, lpData,
1042          lpdwDataSize );
1043   return DPERR_OUTOFMEMORY;
1044 }
1045
1046 typedef struct tagRunApplicationEnumStruct
1047 {
1048   IDirectPlayLobbyAImpl* This;
1049
1050   GUID  appGUID;
1051   LPSTR lpszPath;
1052   LPSTR lpszFileName;
1053   LPSTR lpszCommandLine;
1054   LPSTR lpszCurrentDirectory;
1055 } RunApplicationEnumStruct, *lpRunApplicationEnumStruct;
1056
1057 /* To be called by RunApplication to find how to invoke the function */
1058 static BOOL CALLBACK RunApplicationA_EnumLocalApplications
1059 ( LPCDPLAPPINFO   lpAppInfo,
1060   LPVOID          lpContext,
1061   DWORD           dwFlags )
1062 {
1063   lpRunApplicationEnumStruct lpData = (lpRunApplicationEnumStruct)lpContext;
1064
1065   if( IsEqualGUID( &lpAppInfo->guidApplication, &lpData->appGUID ) )
1066   {
1067     char  returnBuffer[200];
1068     DWORD returnType, sizeOfReturnBuffer;
1069     LPSTR clSubKey   = "CommandLine";
1070     LPSTR cdSubKey   = "CurrentDirectory";  
1071     LPSTR fileSubKey = "File";
1072     LPSTR pathSubKey = "Path";
1073
1074     /* FIXME: Lazy man hack - dplay struct has the present reg key saved */ 
1075
1076     sizeOfReturnBuffer = 200;
1077     
1078     /* Get all the appropriate data from the registry */
1079     if( RegQueryValueExA( lpData->This->dpl->hkCallbackKeyHack, clSubKey,
1080                           NULL, &returnType, returnBuffer,
1081                           &sizeOfReturnBuffer ) != ERROR_SUCCESS )
1082     {
1083        ERR( ": missing CommandLine registry data member\n" );
1084     }
1085     else
1086     {
1087       lpData->lpszCommandLine = HEAP_strdupA( GetProcessHeap(), HEAP_ZERO_MEMORY, returnBuffer ); 
1088     }
1089
1090     sizeOfReturnBuffer = 200;
1091
1092     if( RegQueryValueExA( lpData->This->dpl->hkCallbackKeyHack, cdSubKey,
1093                           NULL, &returnType, returnBuffer,
1094                           &sizeOfReturnBuffer ) != ERROR_SUCCESS )
1095     {
1096        ERR( ": missing CurrentDirectory registry data member\n" );
1097     }
1098     else
1099     {
1100       lpData->lpszCurrentDirectory = HEAP_strdupA( GetProcessHeap(), HEAP_ZERO_MEMORY, returnBuffer );
1101     }
1102
1103     sizeOfReturnBuffer = 200;
1104
1105     if( RegQueryValueExA( lpData->This->dpl->hkCallbackKeyHack, fileSubKey,
1106                           NULL, &returnType, returnBuffer,
1107                           &sizeOfReturnBuffer ) != ERROR_SUCCESS )
1108     {
1109        ERR( ": missing File registry data member\n" );
1110     }
1111     else
1112     {
1113       lpData->lpszFileName = HEAP_strdupA( GetProcessHeap(), HEAP_ZERO_MEMORY, returnBuffer );
1114     }
1115
1116     sizeOfReturnBuffer = 200;
1117
1118     if( RegQueryValueExA( lpData->This->dpl->hkCallbackKeyHack, pathSubKey,
1119                           NULL, &returnType, returnBuffer,
1120                           &sizeOfReturnBuffer ) != ERROR_SUCCESS )
1121     {
1122        ERR( ": missing Path registry data member\n" );
1123     }
1124     else
1125     {
1126       lpData->lpszPath = HEAP_strdupA( GetProcessHeap(), HEAP_ZERO_MEMORY, returnBuffer );
1127     }
1128
1129     return FALSE; /* No need to keep going as we found what we wanted */
1130   }
1131
1132   return TRUE; /* Keep enumerating, haven't found the application yet */ 
1133 }
1134
1135 BOOL DPL_CreateAndSetLobbyHandles( DWORD dwDestProcessId, HANDLE hDestProcess,
1136                                    LPHANDLE lphStart, LPHANDLE lphDeath, 
1137                                    LPHANDLE lphRead )
1138 {
1139   /* These are the handles for the created process */
1140   HANDLE hAppStart, hAppDeath, hAppRead, hTemp;
1141   SECURITY_ATTRIBUTES s_attrib;
1142
1143   s_attrib.nLength              = sizeof( s_attrib );
1144   s_attrib.lpSecurityDescriptor = NULL;
1145   s_attrib.bInheritHandle       = TRUE;
1146
1147   /* FIXME: Is there a handle leak here? */
1148   hTemp = CreateEventA( &s_attrib, TRUE, FALSE, NULL );
1149   *lphStart = ConvertToGlobalHandle( hTemp );
1150
1151   hTemp = CreateEventA( &s_attrib, TRUE, FALSE, NULL );
1152   *lphDeath = ConvertToGlobalHandle( hTemp );
1153
1154   hTemp = CreateEventA( &s_attrib, TRUE, FALSE, NULL );
1155   *lphRead  = ConvertToGlobalHandle( hTemp );
1156
1157   if( ( !DuplicateHandle( GetCurrentProcess(), *lphStart, 
1158                           hDestProcess, &hAppStart, 
1159                           0, FALSE, DUPLICATE_SAME_ACCESS ) ) ||
1160       ( !DuplicateHandle( GetCurrentProcess(), *lphDeath, 
1161                           hDestProcess, &hAppDeath, 
1162                           0, FALSE, DUPLICATE_SAME_ACCESS ) ) ||
1163       ( !DuplicateHandle( GetCurrentProcess(), *lphRead, 
1164                           hDestProcess, &hAppRead, 
1165                           0, FALSE, DUPLICATE_SAME_ACCESS ) )
1166     )
1167   {
1168     /* FIXME: Handle leak... */
1169     ERR( "Unable to dup handles\n" );
1170     return FALSE;
1171   }
1172
1173   if( !DPLAYX_SetLobbyHandles( dwDestProcessId, 
1174                                hAppStart, hAppDeath, hAppRead ) )
1175   {
1176     return FALSE; 
1177   }
1178
1179   return TRUE;
1180 }
1181
1182
1183 /********************************************************************
1184  *
1185  * Starts an application and passes to it all the information to
1186  * connect to a session.
1187  *
1188  */
1189 static HRESULT WINAPI IDirectPlayLobbyAImpl_RunApplication
1190 ( LPDIRECTPLAYLOBBYA iface,
1191   DWORD dwFlags,
1192   LPDWORD lpdwAppID,
1193   LPDPLCONNECTION lpConn,
1194   HANDLE hReceiveEvent )
1195 {
1196   ICOM_THIS(IDirectPlayLobbyAImpl,iface);
1197   HRESULT hr;
1198   RunApplicationEnumStruct enumData;
1199   char temp[200];
1200   STARTUPINFOA startupInfo;
1201   PROCESS_INFORMATION newProcessInfo;
1202   LPSTR appName;
1203   DWORD dwSuspendCount;
1204   HANDLE hStart, hDeath, hSettingRead;
1205
1206   TRACE( "(%p)->(0x%08lx,%p,%p,%x)\n", 
1207          This, dwFlags, lpdwAppID, lpConn, hReceiveEvent );
1208
1209   if( dwFlags != 0 )
1210   {
1211     return DPERR_INVALIDPARAMS;
1212   }
1213
1214   if( DPLAYX_AnyLobbiesWaitingForConnSettings() )
1215   {
1216     FIXME( "Waiting lobby not being handled correctly\n" );
1217   }
1218
1219   EnterCriticalSection( &This->unk->DPL_lock );
1220
1221   ZeroMemory( &enumData, sizeof( enumData ) );
1222   enumData.This    = This;
1223   enumData.appGUID = lpConn->lpSessionDesc->guidApplication;
1224
1225   /* Our callback function will fill up the enumData structure with all the information 
1226      required to start a new process */
1227   IDirectPlayLobby_EnumLocalApplications( iface, RunApplicationA_EnumLocalApplications,
1228                                           (LPVOID)(&enumData), 0 );
1229
1230   /* First the application name */
1231   strcpy( temp, enumData.lpszPath );
1232   strcat( temp, "\\" );
1233   strcat( temp, enumData.lpszFileName ); 
1234   HeapFree( GetProcessHeap(), 0, enumData.lpszPath );   
1235   HeapFree( GetProcessHeap(), 0, enumData.lpszFileName );
1236   appName = HEAP_strdupA( GetProcessHeap(), HEAP_ZERO_MEMORY, temp );
1237
1238   /* Now the command line */
1239   strcat( temp, " " ); 
1240   strcat( temp, enumData.lpszCommandLine );
1241   HeapFree( GetProcessHeap(), 0, enumData.lpszCommandLine );
1242   enumData.lpszCommandLine = HEAP_strdupA( GetProcessHeap(), HEAP_ZERO_MEMORY, temp );
1243
1244   ZeroMemory( &startupInfo, sizeof( startupInfo ) );
1245   startupInfo.cb = sizeof( startupInfo );
1246   /* FIXME: Should any fields be filled in? */
1247
1248   ZeroMemory( &newProcessInfo, sizeof( newProcessInfo ) );
1249
1250   if( !CreateProcessA( appName,
1251                        enumData.lpszCommandLine,
1252                        NULL,
1253                        NULL,
1254                        FALSE,
1255                        CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_CONSOLE | CREATE_SUSPENDED, /* Creation Flags */
1256                        NULL,
1257                        enumData.lpszCurrentDirectory,
1258                        &startupInfo,
1259                        &newProcessInfo
1260                      )
1261     )
1262   {
1263     ERR( "Failed to create process for app %s\n", appName );
1264
1265     HeapFree( GetProcessHeap(), 0, appName );
1266     HeapFree( GetProcessHeap(), 0, enumData.lpszCommandLine );
1267     HeapFree( GetProcessHeap(), 0, enumData.lpszCurrentDirectory );
1268
1269     return DPERR_CANTCREATEPROCESS; 
1270   } 
1271
1272   HeapFree( GetProcessHeap(), 0, appName );
1273   HeapFree( GetProcessHeap(), 0, enumData.lpszCommandLine );
1274   HeapFree( GetProcessHeap(), 0, enumData.lpszCurrentDirectory );
1275
1276   /* Reserve this global application id! */
1277   if( !DPLAYX_CreateLobbyApplication( newProcessInfo.dwProcessId ) )
1278   {
1279     ERR( "Unable to create global application data for 0x%08lx\n",
1280            newProcessInfo.dwProcessId );
1281   }
1282
1283   hr = IDirectPlayLobby_SetConnectionSettings( iface, 0, newProcessInfo.dwProcessId, lpConn );
1284  
1285   if( hr != DP_OK )
1286   {
1287     ERR( "SetConnectionSettings failure %s\n", DPLAYX_HresultToString( hr ) );
1288     return hr;
1289   }
1290
1291   /* Setup the handles for application notification */
1292   DPL_CreateAndSetLobbyHandles( newProcessInfo.dwProcessId,
1293                                 newProcessInfo.hProcess,
1294                                 &hStart, &hDeath, &hSettingRead );
1295
1296   /* Setup the message thread ID */
1297   This->dpl->dwMsgThread = 
1298     CreateLobbyMessageReceptionThread( hReceiveEvent, hStart, hDeath, hSettingRead );
1299
1300   DPLAYX_SetLobbyMsgThreadId( newProcessInfo.dwProcessId, This->dpl->dwMsgThread );
1301
1302   LeaveCriticalSection( &This->unk->DPL_lock );
1303
1304   /* Everything seems to have been set correctly, update the dwAppID */
1305   *lpdwAppID = newProcessInfo.dwProcessId;
1306
1307   /* Unsuspend the process - should return the prev suspension count */ 
1308   if( ( dwSuspendCount = ResumeThread( newProcessInfo.hThread ) ) != 1 )
1309   {
1310     ERR( "ResumeThread failed with 0x%08lx\n", dwSuspendCount );
1311   }
1312
1313   return DP_OK;
1314 }
1315
1316 static HRESULT WINAPI IDirectPlayLobbyWImpl_RunApplication
1317 ( LPDIRECTPLAYLOBBY iface,
1318   DWORD dwFlags,
1319   LPDWORD lpdwAppID,
1320   LPDPLCONNECTION lpConn,
1321   HANDLE hReceiveEvent )
1322 {
1323   ICOM_THIS(IDirectPlayLobbyWImpl,iface);
1324   FIXME( "(%p)->(0x%08lx,%p,%p,%p):stub\n", This, dwFlags, lpdwAppID, lpConn, (void *)hReceiveEvent );
1325   return DPERR_OUTOFMEMORY;
1326 }
1327
1328 /********************************************************************
1329  *
1330  * Sends a message between the application and the lobby client.
1331  * All messages are queued until received.
1332  *
1333  */
1334 static HRESULT WINAPI IDirectPlayLobbyAImpl_SendLobbyMessage
1335 ( LPDIRECTPLAYLOBBYA iface,
1336   DWORD dwFlags,
1337   DWORD dwAppID,
1338   LPVOID lpData,
1339   DWORD dwDataSize )
1340 {
1341   FIXME(":stub\n");
1342   return DPERR_OUTOFMEMORY;
1343 }
1344
1345 static HRESULT WINAPI IDirectPlayLobbyWImpl_SendLobbyMessage
1346 ( LPDIRECTPLAYLOBBY iface,
1347   DWORD dwFlags,
1348   DWORD dwAppID,
1349   LPVOID lpData,
1350   DWORD dwDataSize )
1351 {
1352   FIXME(":stub\n");
1353   return DPERR_OUTOFMEMORY;
1354 }
1355
1356 /********************************************************************
1357  *
1358  * Modifies the DPLCONNECTION structure to contain all information
1359  * needed to start and connect an application.
1360  *
1361  */
1362 static HRESULT WINAPI IDirectPlayLobbyWImpl_SetConnectionSettings
1363 ( LPDIRECTPLAYLOBBY iface,
1364   DWORD dwFlags,
1365   DWORD dwAppID,
1366   LPDPLCONNECTION lpConn )
1367 {
1368   ICOM_THIS(IDirectPlayLobbyWImpl,iface);
1369   HRESULT hr;
1370
1371   TRACE("(%p)->(0x%08lx,0x%08lx,%p)\n", This, dwFlags, dwAppID, lpConn );
1372
1373   EnterCriticalSection( &This->unk->DPL_lock );
1374
1375   hr = DPLAYX_SetConnectionSettingsW( dwFlags, dwAppID, lpConn );
1376
1377   /* FIXME: Don't think that this is supposed to fail, but the docuementation
1378             is somewhat sketchy. I'll try creating a lobby application
1379             for this... */
1380   if( hr == DPERR_NOTLOBBIED )
1381   {
1382     FIXME( "Unlobbied app setting connections. Is this correct behavior?\n" );
1383     if( dwAppID == 0 )
1384     {
1385        dwAppID = GetCurrentProcessId();
1386     }
1387     DPLAYX_CreateLobbyApplication( dwAppID );
1388     hr = DPLAYX_SetConnectionSettingsW( dwFlags, dwAppID, lpConn );
1389   }
1390
1391   LeaveCriticalSection( &This->unk->DPL_lock );
1392
1393   return hr;
1394 }
1395
1396 static HRESULT WINAPI IDirectPlayLobbyAImpl_SetConnectionSettings
1397 ( LPDIRECTPLAYLOBBYA iface,
1398   DWORD dwFlags,
1399   DWORD dwAppID,
1400   LPDPLCONNECTION lpConn )
1401 {
1402   ICOM_THIS(IDirectPlayLobbyAImpl,iface);
1403   HRESULT hr;
1404
1405   TRACE("(%p)->(0x%08lx,0x%08lx,%p)\n", This, dwFlags, dwAppID, lpConn );
1406
1407   EnterCriticalSection( &This->unk->DPL_lock );
1408
1409   hr = DPLAYX_SetConnectionSettingsA( dwFlags, dwAppID, lpConn );
1410
1411   /* FIXME: Don't think that this is supposed to fail, but the docuementation
1412             is somewhat sketchy. I'll try creating a lobby application
1413             for this... */
1414   if( hr == DPERR_NOTLOBBIED )
1415   {
1416     FIXME( "Unlobbied app setting connections. Is this correct behavior?\n" );
1417     dwAppID = GetCurrentProcessId();
1418     DPLAYX_CreateLobbyApplication( dwAppID );
1419     hr = DPLAYX_SetConnectionSettingsA( dwFlags, dwAppID, lpConn );
1420   }
1421
1422   LeaveCriticalSection( &This->unk->DPL_lock );
1423
1424   return hr;
1425 }
1426
1427 /********************************************************************
1428  *
1429  * Registers an event that will be set when a lobby message is received.
1430  *
1431  */
1432 static HRESULT WINAPI IDirectPlayLobbyAImpl_SetLobbyMessageEvent
1433 ( LPDIRECTPLAYLOBBYA iface,
1434   DWORD dwFlags,
1435   DWORD dwAppID,
1436   HANDLE hReceiveEvent )
1437 {
1438   FIXME(":stub\n");
1439   return DPERR_OUTOFMEMORY;
1440 }
1441
1442 static HRESULT WINAPI IDirectPlayLobbyWImpl_SetLobbyMessageEvent
1443 ( LPDIRECTPLAYLOBBY iface,
1444   DWORD dwFlags,
1445   DWORD dwAppID,
1446   HANDLE hReceiveEvent )
1447 {
1448   FIXME(":stub\n");
1449   return DPERR_OUTOFMEMORY;
1450 }
1451
1452
1453 /* DPL 2 methods */
1454 static HRESULT WINAPI IDirectPlayLobby2WImpl_CreateCompoundAddress
1455 ( LPDIRECTPLAYLOBBY2 iface,
1456   LPCDPCOMPOUNDADDRESSELEMENT lpElements,
1457   DWORD dwElementCount,
1458   LPVOID lpAddress,
1459   LPDWORD lpdwAddressSize )
1460 {
1461   return DPL_CreateCompoundAddress( lpElements, dwElementCount, lpAddress, lpdwAddressSize, FALSE );
1462 }
1463
1464 static HRESULT WINAPI IDirectPlayLobby2AImpl_CreateCompoundAddress
1465 ( LPDIRECTPLAYLOBBY2A iface,
1466   LPCDPCOMPOUNDADDRESSELEMENT lpElements,
1467   DWORD dwElementCount,
1468   LPVOID lpAddress,
1469   LPDWORD lpdwAddressSize )
1470 {
1471   return DPL_CreateCompoundAddress( lpElements, dwElementCount, lpAddress, lpdwAddressSize, TRUE );
1472 }
1473
1474 HRESULT DPL_CreateCompoundAddress
1475 ( LPCDPCOMPOUNDADDRESSELEMENT lpElements,
1476   DWORD dwElementCount,
1477   LPVOID lpAddress,
1478   LPDWORD lpdwAddressSize,
1479   BOOL bAnsiInterface )
1480 {
1481   DWORD dwSizeRequired = 0;
1482   DWORD dwElements;
1483   LPCDPCOMPOUNDADDRESSELEMENT lpOrigElements = lpElements;
1484
1485   TRACE("(%p,0x%08lx,%p,%p)\n", lpElements, dwElementCount, lpAddress, lpdwAddressSize );
1486
1487   /* Parameter check */
1488   if( ( lpElements == NULL ) ||
1489       ( dwElementCount == 0 )   /* FIXME: Not sure if this is a failure case */
1490     )
1491   {
1492     return DPERR_INVALIDPARAMS;
1493   }
1494
1495   /* Add the total size chunk */
1496   dwSizeRequired += sizeof( DPADDRESS ) + sizeof( DWORD );
1497
1498   /* Calculate the size of the buffer required */
1499   for ( dwElements = dwElementCount; dwElements > 0; --dwElements, ++lpElements ) 
1500   {
1501     if ( ( IsEqualGUID( &lpElements->guidDataType, &DPAID_ServiceProvider ) ) ||
1502          ( IsEqualGUID( &lpElements->guidDataType, &DPAID_LobbyProvider ) )
1503        )
1504     {
1505       dwSizeRequired += sizeof( DPADDRESS ) + sizeof( GUID ); 
1506     }
1507     else if ( ( IsEqualGUID( &lpElements->guidDataType, &DPAID_Phone ) ) ||
1508               ( IsEqualGUID( &lpElements->guidDataType, &DPAID_Modem ) ) ||
1509               ( IsEqualGUID( &lpElements->guidDataType, &DPAID_INet ) )
1510             )
1511     {
1512       if( !bAnsiInterface )
1513       { 
1514         ERR( "Ansi GUIDs used for unicode interface\n" );
1515         return DPERR_INVALIDFLAGS;
1516       }
1517
1518       dwSizeRequired += sizeof( DPADDRESS ) + lpElements->dwDataSize;
1519     }
1520     else if ( ( IsEqualGUID( &lpElements->guidDataType, &DPAID_PhoneW ) ) ||
1521               ( IsEqualGUID( &lpElements->guidDataType, &DPAID_ModemW ) ) ||
1522               ( IsEqualGUID( &lpElements->guidDataType, &DPAID_INetW ) )
1523             )
1524     {
1525       if( bAnsiInterface )
1526       {
1527         ERR( "Unicode GUIDs used for ansi interface\n" );
1528         return DPERR_INVALIDFLAGS;
1529       }
1530
1531       FIXME( "Right size for unicode interface?\n" );
1532       dwSizeRequired += sizeof( DPADDRESS ) + lpElements->dwDataSize * sizeof( WCHAR );
1533     }
1534     else if ( IsEqualGUID( &lpElements->guidDataType, &DPAID_INetPort ) )
1535     {
1536       dwSizeRequired += sizeof( DPADDRESS ) + sizeof( WORD );
1537     }
1538     else if ( IsEqualGUID( &lpElements->guidDataType, &DPAID_ComPort ) )
1539     {
1540       FIXME( "Right size for unicode interface?\n" );
1541       dwSizeRequired += sizeof( DPADDRESS ) + sizeof( DPCOMPORTADDRESS ); /* FIXME: Right size? */
1542     }
1543     else
1544     {
1545       ERR( "Unknown GUID %s\n", debugstr_guid(&lpElements->guidDataType) );
1546       return DPERR_INVALIDFLAGS; 
1547     }
1548   }
1549
1550   /* The user wants to know how big a buffer to allocate for us */
1551   if( ( lpAddress == NULL ) ||
1552       ( *lpdwAddressSize < dwSizeRequired ) 
1553     )
1554   {
1555     *lpdwAddressSize = dwSizeRequired; 
1556     return DPERR_BUFFERTOOSMALL;
1557   }  
1558
1559   /* Add the total size chunk */
1560   {
1561     LPDPADDRESS lpdpAddress = (LPDPADDRESS)lpAddress;
1562
1563     CopyMemory( &lpdpAddress->guidDataType, &DPAID_TotalSize, sizeof( GUID ) );
1564     lpdpAddress->dwDataSize = sizeof( DWORD );
1565     lpAddress = (char *) lpAddress + sizeof( DPADDRESS );
1566
1567     *(LPDWORD)lpAddress = dwSizeRequired;
1568     lpAddress = (char *) lpAddress + sizeof( DWORD );
1569   }
1570
1571   /* Calculate the size of the buffer required */
1572   for( dwElements = dwElementCount, lpElements = lpOrigElements; 
1573        dwElements > 0; 
1574        --dwElements, ++lpElements ) 
1575   {
1576     if ( ( IsEqualGUID( &lpElements->guidDataType, &DPAID_ServiceProvider ) ) ||
1577          ( IsEqualGUID( &lpElements->guidDataType, &DPAID_LobbyProvider ) )
1578        )
1579     {
1580       LPDPADDRESS lpdpAddress = (LPDPADDRESS)lpAddress;
1581
1582       CopyMemory( &lpdpAddress->guidDataType, &lpElements->guidDataType, 
1583                   sizeof( GUID ) );
1584       lpdpAddress->dwDataSize = sizeof( GUID );
1585       lpAddress = (char *) lpAddress + sizeof( DPADDRESS );
1586
1587       CopyMemory( lpAddress, lpElements->lpData, sizeof( GUID ) );
1588       lpAddress = (char *) lpAddress + sizeof( GUID );
1589     }
1590     else if ( ( IsEqualGUID( &lpElements->guidDataType, &DPAID_Phone ) ) ||
1591               ( IsEqualGUID( &lpElements->guidDataType, &DPAID_Modem ) ) || 
1592               ( IsEqualGUID( &lpElements->guidDataType, &DPAID_INet ) )
1593             )
1594     {
1595       LPDPADDRESS lpdpAddress = (LPDPADDRESS)lpAddress;
1596
1597       CopyMemory( &lpdpAddress->guidDataType, &lpElements->guidDataType, 
1598                   sizeof( GUID ) );
1599       lpdpAddress->dwDataSize = lpElements->dwDataSize;
1600       lpAddress = (char *) lpAddress + sizeof( DPADDRESS );
1601
1602       lstrcpynA( (LPSTR)lpAddress, 
1603                  (LPCSTR)lpElements->lpData, 
1604                  lpElements->dwDataSize );
1605       lpAddress = (char *) lpAddress + lpElements->dwDataSize;
1606     }
1607     else if ( ( IsEqualGUID( &lpElements->guidDataType, &DPAID_PhoneW ) ) ||
1608               ( IsEqualGUID( &lpElements->guidDataType, &DPAID_ModemW ) ) ||
1609               ( IsEqualGUID( &lpElements->guidDataType, &DPAID_INetW ) )
1610             )
1611     {
1612       LPDPADDRESS lpdpAddress = (LPDPADDRESS)lpAddress;
1613
1614       CopyMemory( &lpdpAddress->guidDataType, &lpElements->guidDataType, 
1615                   sizeof( GUID ) );
1616       lpdpAddress->dwDataSize = lpElements->dwDataSize;
1617       lpAddress = (char *) lpAddress + sizeof( DPADDRESS );
1618
1619       lstrcpynW( (LPWSTR)lpAddress,
1620                  (LPCWSTR)lpElements->lpData,
1621                  lpElements->dwDataSize );
1622       lpAddress = (char *) lpAddress + lpElements->dwDataSize * sizeof( WCHAR );
1623     }
1624     else if ( IsEqualGUID( &lpElements->guidDataType, &DPAID_INetPort ) )
1625     {
1626       LPDPADDRESS lpdpAddress = (LPDPADDRESS)lpAddress;
1627
1628       CopyMemory( &lpdpAddress->guidDataType, &lpElements->guidDataType, 
1629                   sizeof( GUID ) );
1630       lpdpAddress->dwDataSize = lpElements->dwDataSize;
1631       lpAddress = (char *) lpAddress + sizeof( DPADDRESS );
1632
1633       *((LPWORD)lpAddress) = *((LPWORD)lpElements->lpData);
1634       lpAddress = (char *) lpAddress + sizeof( WORD );
1635     }
1636     else if ( IsEqualGUID( &lpElements->guidDataType, &DPAID_ComPort ) )
1637     {
1638       LPDPADDRESS lpdpAddress = (LPDPADDRESS)lpAddress;
1639
1640       CopyMemory( &lpdpAddress->guidDataType, &lpElements->guidDataType, 
1641                   sizeof( GUID ) );
1642       lpdpAddress->dwDataSize = lpElements->dwDataSize;
1643       lpAddress = (char *) lpAddress + sizeof( DPADDRESS );
1644
1645       CopyMemory( lpAddress, lpElements->lpData, sizeof( DPADDRESS ) ); 
1646       lpAddress = (char *) lpAddress + sizeof( DPADDRESS );
1647     }
1648   }
1649
1650   return DP_OK;
1651 }
1652
1653 /* DPL 3 methods */
1654
1655 static HRESULT WINAPI IDirectPlayLobby3WImpl_ConnectEx
1656 ( LPDIRECTPLAYLOBBY3 iface, DWORD dwFlags, REFIID riid, 
1657   LPVOID* lplpDP, IUnknown* pUnk )
1658 {
1659   ICOM_THIS( IDirectPlayLobbyAImpl, iface );
1660   return DPL_ConnectEx( This, dwFlags, riid, lplpDP, pUnk );
1661 }
1662
1663 static HRESULT WINAPI IDirectPlayLobby3AImpl_ConnectEx
1664 ( LPDIRECTPLAYLOBBY3A iface, DWORD dwFlags, REFIID riid, 
1665   LPVOID* lplpDP, IUnknown* pUnk )
1666 {
1667   ICOM_THIS( IDirectPlayLobbyAImpl, iface );
1668   return DPL_ConnectEx( This, dwFlags, riid, lplpDP, pUnk );
1669 }
1670
1671 static HRESULT WINAPI IDirectPlayLobby3WImpl_RegisterApplication
1672 ( LPDIRECTPLAYLOBBY3 iface, DWORD dwFlags, LPDPAPPLICATIONDESC lpAppDesc )
1673 {
1674   FIXME(":stub\n");
1675   return DP_OK;
1676 }
1677
1678 static HRESULT WINAPI IDirectPlayLobby3AImpl_RegisterApplication
1679 ( LPDIRECTPLAYLOBBY3A iface, DWORD dwFlags, LPDPAPPLICATIONDESC lpAppDesc )
1680 {
1681   FIXME(":stub\n");
1682   return DP_OK;
1683 }
1684
1685 static HRESULT WINAPI IDirectPlayLobby3WImpl_UnregisterApplication
1686 ( LPDIRECTPLAYLOBBY3 iface, DWORD dwFlags, REFGUID lpAppDesc )
1687 {
1688   FIXME(":stub\n");
1689   return DP_OK;
1690 }
1691
1692 static HRESULT WINAPI IDirectPlayLobby3AImpl_UnregisterApplication
1693 ( LPDIRECTPLAYLOBBY3A iface, DWORD dwFlags, REFGUID lpAppDesc )
1694 {
1695   FIXME(":stub\n");
1696   return DP_OK;
1697 }
1698
1699 static HRESULT WINAPI IDirectPlayLobby3WImpl_WaitForConnectionSettings
1700 ( LPDIRECTPLAYLOBBY3 iface, DWORD dwFlags )
1701 {
1702   HRESULT hr         = DP_OK;
1703   BOOL    bStartWait = (dwFlags & DPLWAIT_CANCEL) ? FALSE : TRUE;
1704
1705   TRACE( "(%p)->(0x%08lx)\n", iface, dwFlags );
1706
1707   if( DPLAYX_WaitForConnectionSettings( bStartWait ) )
1708   {
1709     /* FIXME: What is the correct error return code? */
1710     hr = DPERR_NOTLOBBIED; 
1711   }
1712
1713   return hr;
1714 }
1715
1716 static HRESULT WINAPI IDirectPlayLobby3AImpl_WaitForConnectionSettings
1717 ( LPDIRECTPLAYLOBBY3A iface, DWORD dwFlags )
1718 {
1719   HRESULT hr         = DP_OK;
1720   BOOL    bStartWait = (dwFlags & DPLWAIT_CANCEL) ? FALSE : TRUE;
1721
1722   TRACE( "(%p)->(0x%08lx)\n", iface, dwFlags );
1723
1724   if( DPLAYX_WaitForConnectionSettings( bStartWait ) )
1725   {
1726     /* FIXME: What is the correct error return code? */
1727     hr = DPERR_NOTLOBBIED;
1728   }
1729
1730   return hr;
1731 }
1732
1733
1734 /* Virtual Table definitions for DPL{1,2,3}{A,W} */
1735
1736 /* Note: Hack so we can reuse the old functions without compiler warnings */
1737 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1738 # define XCAST(fun)     (typeof(directPlayLobbyAVT.fn##fun))
1739 #else
1740 # define XCAST(fun)     (void*)
1741 #endif
1742
1743 /* Direct Play Lobby 1 (ascii) Virtual Table for methods */
1744 /* All lobby 1 methods are exactly the same except QueryInterface */
1745 static struct ICOM_VTABLE(IDirectPlayLobby) directPlayLobbyAVT = 
1746 {
1747   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1748
1749   XCAST(QueryInterface)DPL_QueryInterface,
1750   XCAST(AddRef)DPL_AddRef,
1751   XCAST(Release)DPL_Release,
1752
1753   IDirectPlayLobbyAImpl_Connect,
1754   IDirectPlayLobbyAImpl_CreateAddress,
1755   IDirectPlayLobbyAImpl_EnumAddress,
1756   IDirectPlayLobbyAImpl_EnumAddressTypes,
1757   IDirectPlayLobbyAImpl_EnumLocalApplications,
1758   IDirectPlayLobbyAImpl_GetConnectionSettings,
1759   IDirectPlayLobbyAImpl_ReceiveLobbyMessage,
1760   IDirectPlayLobbyAImpl_RunApplication,
1761   IDirectPlayLobbyAImpl_SendLobbyMessage,
1762   IDirectPlayLobbyAImpl_SetConnectionSettings,
1763   IDirectPlayLobbyAImpl_SetLobbyMessageEvent
1764 };
1765 #undef XCAST
1766
1767
1768 /* Note: Hack so we can reuse the old functions without compiler warnings */
1769 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1770 # define XCAST(fun)     (typeof(directPlayLobbyWVT.fn##fun))
1771 #else
1772 # define XCAST(fun)     (void*)
1773 #endif
1774
1775 /* Direct Play Lobby 1 (unicode) Virtual Table for methods */
1776 static ICOM_VTABLE(IDirectPlayLobby) directPlayLobbyWVT = 
1777 {
1778   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1779
1780   XCAST(QueryInterface)DPL_QueryInterface,
1781   XCAST(AddRef)DPL_AddRef,
1782   XCAST(Release)DPL_Release,
1783
1784   IDirectPlayLobbyWImpl_Connect,
1785   IDirectPlayLobbyWImpl_CreateAddress, 
1786   IDirectPlayLobbyWImpl_EnumAddress,
1787   IDirectPlayLobbyWImpl_EnumAddressTypes,
1788   IDirectPlayLobbyWImpl_EnumLocalApplications,
1789   IDirectPlayLobbyWImpl_GetConnectionSettings,
1790   IDirectPlayLobbyWImpl_ReceiveLobbyMessage,
1791   IDirectPlayLobbyWImpl_RunApplication,
1792   IDirectPlayLobbyWImpl_SendLobbyMessage,
1793   IDirectPlayLobbyWImpl_SetConnectionSettings,
1794   IDirectPlayLobbyWImpl_SetLobbyMessageEvent
1795 };
1796 #undef XCAST
1797
1798 /* Note: Hack so we can reuse the old functions without compiler warnings */
1799 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1800 # define XCAST(fun)     (typeof(directPlayLobby2AVT.fn##fun))
1801 #else
1802 # define XCAST(fun)     (void*)
1803 #endif
1804
1805 /* Direct Play Lobby 2 (ascii) Virtual Table for methods */
1806 static ICOM_VTABLE(IDirectPlayLobby2) directPlayLobby2AVT = 
1807 {
1808   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1809
1810   XCAST(QueryInterface)DPL_QueryInterface,
1811   XCAST(AddRef)DPL_AddRef,
1812   XCAST(Release)DPL_Release,
1813
1814   XCAST(Connect)IDirectPlayLobbyAImpl_Connect,
1815   XCAST(CreateAddress)IDirectPlayLobbyAImpl_CreateAddress,
1816   XCAST(EnumAddress)IDirectPlayLobbyAImpl_EnumAddress,
1817   XCAST(EnumAddressTypes)IDirectPlayLobbyAImpl_EnumAddressTypes,
1818   XCAST(EnumLocalApplications)IDirectPlayLobbyAImpl_EnumLocalApplications,
1819   XCAST(GetConnectionSettings)IDirectPlayLobbyAImpl_GetConnectionSettings,
1820   XCAST(ReceiveLobbyMessage)IDirectPlayLobbyAImpl_ReceiveLobbyMessage,
1821   XCAST(RunApplication)IDirectPlayLobbyAImpl_RunApplication,
1822   XCAST(SendLobbyMessage)IDirectPlayLobbyAImpl_SendLobbyMessage,
1823   XCAST(SetConnectionSettings)IDirectPlayLobbyAImpl_SetConnectionSettings,
1824   XCAST(SetLobbyMessageEvent)IDirectPlayLobbyAImpl_SetLobbyMessageEvent,
1825
1826   IDirectPlayLobby2AImpl_CreateCompoundAddress 
1827 };
1828 #undef XCAST
1829
1830 /* Note: Hack so we can reuse the old functions without compiler warnings */
1831 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1832 # define XCAST(fun)     (typeof(directPlayLobby2AVT.fn##fun))
1833 #else
1834 # define XCAST(fun)     (void*)
1835 #endif
1836
1837 /* Direct Play Lobby 2 (unicode) Virtual Table for methods */
1838 static ICOM_VTABLE(IDirectPlayLobby2) directPlayLobby2WVT = 
1839 {
1840   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1841
1842   XCAST(QueryInterface)DPL_QueryInterface,
1843   XCAST(AddRef)DPL_AddRef, 
1844   XCAST(Release)DPL_Release,
1845
1846   XCAST(Connect)IDirectPlayLobbyWImpl_Connect,
1847   XCAST(CreateAddress)IDirectPlayLobbyWImpl_CreateAddress,
1848   XCAST(EnumAddress)IDirectPlayLobbyWImpl_EnumAddress,
1849   XCAST(EnumAddressTypes)IDirectPlayLobbyWImpl_EnumAddressTypes,
1850   XCAST(EnumLocalApplications)IDirectPlayLobbyWImpl_EnumLocalApplications,
1851   XCAST(GetConnectionSettings)IDirectPlayLobbyWImpl_GetConnectionSettings,
1852   XCAST(ReceiveLobbyMessage)IDirectPlayLobbyWImpl_ReceiveLobbyMessage,
1853   XCAST(RunApplication)IDirectPlayLobbyWImpl_RunApplication,
1854   XCAST(SendLobbyMessage)IDirectPlayLobbyWImpl_SendLobbyMessage,
1855   XCAST(SetConnectionSettings)IDirectPlayLobbyWImpl_SetConnectionSettings,
1856   XCAST(SetLobbyMessageEvent)IDirectPlayLobbyWImpl_SetLobbyMessageEvent,
1857
1858   IDirectPlayLobby2WImpl_CreateCompoundAddress
1859 };
1860 #undef XCAST
1861
1862 /* Direct Play Lobby 3 (ascii) Virtual Table for methods */
1863
1864 /* Note: Hack so we can reuse the old functions without compiler warnings */
1865 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1866 # define XCAST(fun)     (typeof(directPlayLobby3AVT.fn##fun))
1867 #else
1868 # define XCAST(fun)     (void*)
1869 #endif
1870
1871 static ICOM_VTABLE(IDirectPlayLobby3) directPlayLobby3AVT =
1872 {
1873   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1874   XCAST(QueryInterface)DPL_QueryInterface,
1875   XCAST(AddRef)DPL_AddRef,
1876   XCAST(Release)DPL_Release,
1877
1878   XCAST(Connect)IDirectPlayLobbyAImpl_Connect,
1879   XCAST(CreateAddress)IDirectPlayLobbyAImpl_CreateAddress,
1880   XCAST(EnumAddress)IDirectPlayLobbyAImpl_EnumAddress,
1881   XCAST(EnumAddressTypes)IDirectPlayLobbyAImpl_EnumAddressTypes,
1882   XCAST(EnumLocalApplications)IDirectPlayLobbyAImpl_EnumLocalApplications,
1883   XCAST(GetConnectionSettings)IDirectPlayLobbyAImpl_GetConnectionSettings,
1884   XCAST(ReceiveLobbyMessage)IDirectPlayLobbyAImpl_ReceiveLobbyMessage,
1885   XCAST(RunApplication)IDirectPlayLobbyAImpl_RunApplication,
1886   XCAST(SendLobbyMessage)IDirectPlayLobbyAImpl_SendLobbyMessage,
1887   XCAST(SetConnectionSettings)IDirectPlayLobbyAImpl_SetConnectionSettings,
1888   XCAST(SetLobbyMessageEvent)IDirectPlayLobbyAImpl_SetLobbyMessageEvent,
1889
1890   XCAST(CreateCompoundAddress)IDirectPlayLobby2AImpl_CreateCompoundAddress,
1891
1892   IDirectPlayLobby3AImpl_ConnectEx,
1893   IDirectPlayLobby3AImpl_RegisterApplication,
1894   IDirectPlayLobby3AImpl_UnregisterApplication,
1895   IDirectPlayLobby3AImpl_WaitForConnectionSettings
1896 };
1897 #undef XCAST
1898
1899 /* Direct Play Lobby 3 (unicode) Virtual Table for methods */
1900
1901 /* Note: Hack so we can reuse the old functions without compiler warnings */
1902 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1903 # define XCAST(fun)     (typeof(directPlayLobby3WVT.fn##fun))
1904 #else
1905 # define XCAST(fun)     (void*)
1906 #endif
1907
1908 static ICOM_VTABLE(IDirectPlayLobby3) directPlayLobby3WVT =
1909 {
1910   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1911   XCAST(QueryInterface)DPL_QueryInterface,
1912   XCAST(AddRef)DPL_AddRef,
1913   XCAST(Release)DPL_Release,
1914
1915   XCAST(Connect)IDirectPlayLobbyWImpl_Connect,
1916   XCAST(CreateAddress)IDirectPlayLobbyWImpl_CreateAddress,
1917   XCAST(EnumAddress)IDirectPlayLobbyWImpl_EnumAddress,
1918   XCAST(EnumAddressTypes)IDirectPlayLobbyWImpl_EnumAddressTypes,
1919   XCAST(EnumLocalApplications)IDirectPlayLobbyWImpl_EnumLocalApplications,
1920   XCAST(GetConnectionSettings)IDirectPlayLobbyWImpl_GetConnectionSettings,
1921   XCAST(ReceiveLobbyMessage)IDirectPlayLobbyWImpl_ReceiveLobbyMessage,
1922   XCAST(RunApplication)IDirectPlayLobbyWImpl_RunApplication,
1923   XCAST(SendLobbyMessage)IDirectPlayLobbyWImpl_SendLobbyMessage,
1924   XCAST(SetConnectionSettings)IDirectPlayLobbyWImpl_SetConnectionSettings,
1925   XCAST(SetLobbyMessageEvent)IDirectPlayLobbyWImpl_SetLobbyMessageEvent,
1926
1927   XCAST(CreateCompoundAddress)IDirectPlayLobby2WImpl_CreateCompoundAddress,
1928
1929   IDirectPlayLobby3WImpl_ConnectEx,
1930   IDirectPlayLobby3WImpl_RegisterApplication,
1931   IDirectPlayLobby3WImpl_UnregisterApplication,
1932   IDirectPlayLobby3WImpl_WaitForConnectionSettings
1933 };
1934 #undef XCAST
1935
1936
1937 /*********************************************************
1938  *
1939  * Direct Play Lobby Interface Implementation 
1940  * 
1941  *********************************************************/ 
1942
1943 /***************************************************************************
1944  *  DirectPlayLobbyCreateA   (DPLAYX.4)
1945  *
1946  */
1947 HRESULT WINAPI DirectPlayLobbyCreateA( LPGUID lpGUIDDSP,
1948                                        LPDIRECTPLAYLOBBYA *lplpDPL,
1949                                        IUnknown *lpUnk, 
1950                                        LPVOID lpData,
1951                                        DWORD dwDataSize )
1952 {
1953   TRACE("lpGUIDDSP=%p lplpDPL=%p lpUnk=%p lpData=%p dwDataSize=%08lx\n",
1954         lpGUIDDSP,lplpDPL,lpUnk,lpData,dwDataSize);
1955
1956   /* Parameter Check: lpGUIDSP, lpUnk & lpData must be NULL. dwDataSize must
1957    * equal 0. These fields are mostly for future expansion.
1958    */
1959   if ( lpGUIDDSP || lpData || dwDataSize )
1960   {
1961      *lplpDPL = NULL;
1962      return DPERR_INVALIDPARAMS;
1963   }
1964
1965   if( lpUnk )
1966   {
1967      *lplpDPL = NULL;
1968      ERR("Bad parameters!\n" );
1969      return CLASS_E_NOAGGREGATION;
1970   }
1971
1972   return DPL_CreateInterface( &IID_IDirectPlayLobbyA, (void**)lplpDPL ); 
1973 }
1974
1975 /***************************************************************************
1976  *  DirectPlayLobbyCreateW   (DPLAYX.5)
1977  *
1978  */
1979 HRESULT WINAPI DirectPlayLobbyCreateW( LPGUID lpGUIDDSP, 
1980                                        LPDIRECTPLAYLOBBY *lplpDPL,
1981                                        IUnknown *lpUnk,
1982                                        LPVOID lpData, 
1983                                        DWORD dwDataSize )
1984 {
1985   TRACE("lpGUIDDSP=%p lplpDPL=%p lpUnk=%p lpData=%p dwDataSize=%08lx\n",
1986         lpGUIDDSP,lplpDPL,lpUnk,lpData,dwDataSize);
1987
1988   /* Parameter Check: lpGUIDSP, lpUnk & lpData must be NULL. dwDataSize must 
1989    * equal 0. These fields are mostly for future expansion.
1990    */
1991   if ( lpGUIDDSP || lpData || dwDataSize )
1992   {
1993      *lplpDPL = NULL;
1994      ERR("Bad parameters!\n" );
1995      return DPERR_INVALIDPARAMS;
1996   }
1997
1998   if( lpUnk )
1999   {
2000      *lplpDPL = NULL;
2001      ERR("Bad parameters!\n" );
2002      return CLASS_E_NOAGGREGATION;
2003   }
2004
2005   return DPL_CreateInterface( &IID_IDirectPlayLobby, (void**)lplpDPL );  
2006 }