dplaychk.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include <windows.h>
  2. #include <windowsx.h>
  3. #include <dplobby.h>
  4. #include "dplaychk.h"
  5. // {81662310-FCB4-11d0-A88A-006097B58FBF}
  6. DEFINE_GUID(FEDSRV_GUID,
  7. 0x81662310, 0xfcb4, 0x11d0, 0xa8, 0x8a, 0x0, 0x60, 0x97, 0xb5, 0x8f, 0xbf);
  8. CDplayServerChecker::CDplayServerChecker()
  9. : m_pszServer(NULL), m_fDPInit(FALSE), m_pDirectPlay3A(NULL), m_szMsg(NULL),
  10. m_pDirectPlayLobbyA(NULL)
  11. {
  12. }
  13. CDplayServerChecker::~CDplayServerChecker()
  14. {
  15. if (!m_fDPInit)
  16. {
  17. if (m_pDirectPlay3A)
  18. {
  19. m_pDirectPlay3A->Release();
  20. m_pDirectPlay3A = NULL;
  21. }
  22. }
  23. }
  24. void CDplayServerChecker::ClearServer()
  25. {
  26. // delete the server id string
  27. if (m_pszServer)
  28. {
  29. delete m_pszServer;
  30. m_pszServer = NULL;
  31. }
  32. // free the server connection
  33. if (!m_fDPInit)
  34. {
  35. if (m_pDirectPlay3A)
  36. {
  37. m_pDirectPlay3A->Release();
  38. m_pDirectPlay3A = NULL;
  39. }
  40. }
  41. }
  42. void CDplayServerChecker::SetServer(LPSTR szServer)
  43. {
  44. // if we had a server before, clear it
  45. ClearServer();
  46. m_pszServer = new char[strlen(szServer)+1];
  47. lstrcpy(m_pszServer, szServer);
  48. }
  49. //---------------------------------------------------------------------------
  50. //
  51. // FUNCTION: EnumSessionsCallback2
  52. //
  53. // PURPOSE: Called by IDirectPlay3A::EnumSessions.
  54. //
  55. //---------------------------------------------------------------------------
  56. BOOL FAR PASCAL EnumSessionsCallback2(LPCDPSESSIONDESC2 lpThisSD,
  57. LPDWORD lpdwTimeOut,
  58. DWORD dwFlags,
  59. LPVOID lpContext)
  60. {
  61. // We're not actually going to open this session, so let's just remember that we have one
  62. if (dwFlags & DPESC_TIMEDOUT)
  63. return FALSE;
  64. if (lpThisSD) // will be null if we timed out
  65. *(BOOL *)lpContext = TRUE;
  66. return FALSE;
  67. }
  68. BOOL CDplayServerChecker::ServerConnectionAlive()
  69. {
  70. HRESULT hr = E_FAIL;
  71. BOOL rc = FALSE;
  72. if (!m_fDPInit)
  73. {
  74. // setup dplay stuff
  75. hr = CoCreateInstance(CLSID_DirectPlay, NULL, CLSCTX_INPROC_SERVER,
  76. IID_IDirectPlay3A, (void**)&m_pDirectPlay3A);
  77. if (FAILED(hr))
  78. {
  79. m_szMsg = "DPlay5 is not properly installed. (IID_IDirectPlay3A)";
  80. goto CLEANUP;
  81. }
  82. // Create an IDirectPlayLobby interface, just so I can create an address.
  83. hr = DirectPlayLobbyCreate(NULL, &m_pDirectPlayLobbyA, NULL, NULL, 0);
  84. if (FAILED(hr))
  85. {
  86. m_szMsg = "DPlay5 is not properly installed. (DirectPlayLobbyCreate)";
  87. goto CLEANUP;
  88. }
  89. void * pvAddress = NULL; // dplay address of server
  90. // Create an IDirectPlay3 interface
  91. DWORD dwAddressSize;
  92. // Find out how big the address buffer needs to be--intentional fail 1st time
  93. hr = m_pDirectPlayLobbyA->CreateAddress(DPSPGUID_TCPIP, DPAID_INet, m_pszServer,
  94. lstrlen(m_pszServer) + 1, pvAddress, &dwAddressSize);
  95. if (DPERR_BUFFERTOOSMALL != hr)
  96. {
  97. m_szMsg = "DPlay5 is not properly installed. (CreateAddress)";
  98. goto CLEANUP;
  99. }
  100. pvAddress = GlobalAllocPtr(GMEM_MOVEABLE, dwAddressSize);
  101. hr = m_pDirectPlayLobbyA->CreateAddress(DPSPGUID_TCPIP, DPAID_INet, m_pszServer,
  102. lstrlen(m_pszServer) + 1, pvAddress, &dwAddressSize);
  103. m_pDirectPlayLobbyA->Release();
  104. m_pDirectPlayLobbyA = NULL;
  105. if (FAILED(hr))
  106. {
  107. m_szMsg = "DPlay5 is not properly installed. (CreateAddress)";
  108. goto CLEANUP;
  109. }
  110. hr = m_pDirectPlay3A->InitializeConnection(pvAddress, 0);
  111. GlobalFreePtr(pvAddress);
  112. if (FAILED(hr))
  113. {
  114. m_szMsg = "Can't initialize connection to specified machine";
  115. goto CLEANUP;
  116. }
  117. m_fDPInit = true;
  118. }
  119. {
  120. // try and find the session
  121. bool fFoundOne = false;
  122. DPSESSIONDESC2 sessionDesc;
  123. ZeroMemory(&sessionDesc, sizeof(DPSESSIONDESC2));
  124. sessionDesc.dwSize = sizeof(DPSESSIONDESC2);
  125. sessionDesc.guidApplication = FEDSRV_GUID;
  126. hr = m_pDirectPlay3A->EnumSessions(&sessionDesc, 2000, EnumSessionsCallback2, &fFoundOne, 0); //DPENUMSESSIONS_ASYNC);
  127. if (FAILED(hr) || !fFoundOne)
  128. {
  129. m_szMsg = "Down for the count";
  130. goto CLEANUP;
  131. }
  132. }
  133. rc = TRUE;
  134. CLEANUP:
  135. // save connection, so we can use it over and over
  136. if (!m_fDPInit)
  137. {
  138. if (m_pDirectPlay3A)
  139. {
  140. m_pDirectPlay3A->Release();
  141. m_pDirectPlay3A = NULL;
  142. }
  143. }
  144. return rc;
  145. }