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