LOBBY.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:3k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1996-1997 Microsoft Corporation.  All Rights Reserved.
  4.  *
  5.  *  File:       lobby.cpp
  6.  *  Content: Uses information from the lobby to establish a connection.
  7.  *
  8.  ***************************************************************************/
  9. #include <windows.h>
  10. #include <windowsx.h>
  11. #include <dplobby.h>
  12. #include "dpslots.h"
  13. HRESULT ConnectUsingLobby(LPDPLAYINFO lpDPInfo)
  14. {
  15. LPDIRECTPLAY2A lpDirectPlay2A = NULL;
  16. LPDIRECTPLAY3A lpDirectPlay3A = NULL;
  17. LPDIRECTPLAYLOBBYA lpDirectPlayLobbyA = NULL;
  18. LPDPLCONNECTION lpConnectionSettings = NULL;
  19. DPID dpidPlayer;
  20. DWORD dwSize;
  21. BOOL bIsHost;
  22. DWORD dwPlayerFlags;
  23. HRESULT hr;
  24. // get an ANSI DirectPlay lobby interface
  25. hr = CoCreateInstance(CLSID_DirectPlayLobby, NULL, CLSCTX_INPROC_SERVER, 
  26.   IID_IDirectPlayLobbyA, (LPVOID*)&lpDirectPlayLobbyA);
  27. if FAILED(hr)
  28. goto FAILURE;
  29.     // get connection settings from the lobby
  30. // if this routine returns DPERR_NOTLOBBIED, then a lobby did not
  31. // launch this application and the user needs to configure the connection.
  32. // pass in a NULL pointer to just get the size of the connection setttings
  33. hr = lpDirectPlayLobbyA->GetConnectionSettings(0, NULL, &dwSize);
  34. if (DPERR_BUFFERTOOSMALL != hr)
  35. goto FAILURE;
  36. // allocate memory for the connection setttings
  37. lpConnectionSettings = (LPDPLCONNECTION) GlobalAllocPtr(GHND, dwSize);
  38. if (NULL == lpConnectionSettings)
  39. {
  40. hr = DPERR_OUTOFMEMORY;
  41. goto FAILURE;
  42. }
  43. // get the connection settings
  44. hr = lpDirectPlayLobbyA->GetConnectionSettings(0, lpConnectionSettings, &dwSize);
  45. if FAILED(hr)
  46. goto FAILURE;
  47. // see if we are the host or not
  48. if (lpConnectionSettings->dwFlags & DPLCONNECTION_CREATESESSION)
  49. {
  50. lpConnectionSettings->lpSessionDesc->dwFlags = NONSECURESESSIONFLAGS;
  51. bIsHost = TRUE;
  52. }
  53. else
  54. {
  55. bIsHost = FALSE;
  56. }
  57.     // store the updated connection settings
  58.     hr = lpDirectPlayLobbyA->SetConnectionSettings(0, 0, lpConnectionSettings);
  59. if FAILED(hr)
  60. goto FAILURE;
  61. // connect to the session - returns an ANSI IDirectPlay2A interface
  62. hr = lpDirectPlayLobbyA->Connect(0, &lpDirectPlay2A, NULL);
  63. if FAILED(hr)
  64. goto FAILURE;
  65. // Obtain an IDirectPlay3A interface, the IDirectPlay2A interface will
  66. // be released at the end of the function
  67. hr = lpDirectPlay2A->QueryInterface(IID_IDirectPlay3A, (LPVOID *) &lpDirectPlay3A);
  68. if FAILED(hr)
  69. goto FAILURE;
  70. // if we are hosting then we need to create a server player
  71. if (bIsHost)
  72. dwPlayerFlags = SERVERPLAYERFLAGS;
  73. else
  74. dwPlayerFlags = CLIENTPLAYERFLAGS;
  75. // create a player with the name returned in the connection settings
  76. hr = lpDirectPlay3A->CreatePlayer(&dpidPlayer,
  77. lpConnectionSettings->lpPlayerName, 
  78. lpDPInfo->hPlayerEvent, NULL, 0, dwPlayerFlags);
  79. if FAILED(hr)
  80. goto FAILURE;
  81. // return connection info
  82. lpDPInfo->lpDirectPlay3A = lpDirectPlay3A;
  83. lpDPInfo->dpidPlayer = dpidPlayer;
  84. lpDPInfo->bIsHost = bIsHost;
  85. lpDPInfo->bIsSecure = FALSE;
  86. lpDirectPlay3A = NULL; // set to NULL here so it won't release below
  87. FAILURE:
  88. if (lpDirectPlay2A)
  89. lpDirectPlay2A->Release();
  90. if (lpDirectPlay3A)
  91. lpDirectPlay3A->Release();
  92. if (lpDirectPlayLobbyA)
  93. lpDirectPlayLobbyA->Release();
  94. if (lpConnectionSettings)
  95. GlobalFreePtr(lpConnectionSettings);
  96. return (hr);
  97. }