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