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 "dpchat.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. HRESULT hr;
  22. // get an ANSI DirectPlay lobby interface
  23. hr = DirectPlayLobbyCreate(NULL, &lpDirectPlayLobbyA, NULL, NULL, 0);
  24. if FAILED(hr)
  25. goto FAILURE;
  26.     // get connection settings from the lobby
  27. // if this routine returns DPERR_NOTLOBBIED, then a lobby did not
  28. // launch this application and the user needs to configure the connection.
  29. // pass in a NULL pointer to just get the size of the connection setttings
  30. hr = lpDirectPlayLobbyA->GetConnectionSettings(0, NULL, &dwSize);
  31. if (DPERR_BUFFERTOOSMALL != hr)
  32. goto FAILURE;
  33. // allocate memory for the connection setttings
  34. lpConnectionSettings = (LPDPLCONNECTION) GlobalAllocPtr(GHND, dwSize);
  35. if (NULL == lpConnectionSettings)
  36. {
  37. hr = DPERR_OUTOFMEMORY;
  38. goto FAILURE;
  39. }
  40. // get the connection settings
  41. hr = lpDirectPlayLobbyA->GetConnectionSettings(0, lpConnectionSettings, &dwSize);
  42. if FAILED(hr)
  43. goto FAILURE;
  44. // before connecting, the game should configure the session description
  45. // with any settings it needs
  46. // set flags and max players used by the game
  47.     lpConnectionSettings->lpSessionDesc->dwFlags = DPSESSION_MIGRATEHOST | 
  48.    DPSESSION_KEEPALIVE;
  49.     lpConnectionSettings->lpSessionDesc->dwMaxPlayers = MAXPLAYERS;
  50.     // store the updated connection settings
  51.     hr = lpDirectPlayLobbyA->SetConnectionSettings(0, 0, lpConnectionSettings);
  52. if FAILED(hr)
  53. goto FAILURE;
  54. // connect to the session - returns an ANSI IDirectPlay2A interface
  55. hr = lpDirectPlayLobbyA->Connect(0, &lpDirectPlay2A, NULL);
  56. if FAILED(hr)
  57. goto FAILURE;
  58. // Obtain an IDirectPlay3A interface, the IDirectPlay2A interface will
  59. // be released at the end of the function
  60. hr = lpDirectPlay2A->QueryInterface(IID_IDirectPlay3A, (LPVOID *) &lpDirectPlay3A);
  61. if FAILED(hr)
  62. goto FAILURE;
  63. // create a player with the name returned in the connection settings
  64. hr = lpDirectPlay3A->CreatePlayer(&dpidPlayer,
  65. lpConnectionSettings->lpPlayerName, 
  66. lpDPInfo->hPlayerEvent, NULL, 0, 0);
  67. if FAILED(hr)
  68. goto FAILURE;
  69. // return connection info
  70. lpDPInfo->lpDirectPlay3A = lpDirectPlay3A;
  71. lpDPInfo->dpidPlayer = dpidPlayer;
  72. if (lpConnectionSettings->dwFlags & DPLCONNECTION_CREATESESSION)
  73. lpDPInfo->bIsHost = TRUE;
  74. else
  75. lpDPInfo->bIsHost = FALSE;
  76. lpDirectPlay3A = NULL; // set to NULL here so it won't release below
  77. FAILURE:
  78. if (lpDirectPlay2A)
  79. lpDirectPlay2A->Release();
  80. if (lpDirectPlay3A)
  81. lpDirectPlay3A->Release();
  82. if (lpDirectPlayLobbyA)
  83. lpDirectPlayLobbyA->Release();
  84. if (lpConnectionSettings)
  85. GlobalFreePtr(lpConnectionSettings);
  86. return (hr);
  87. }