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