Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

759 lines
22 KiB

  1. #include "stdafx.h"
  2. #include "Direct.h"
  3. #include "dms.h"
  4. #include "DPlayLobbiedAppObj.h"
  5. #include "DplayAddressObj.h"
  6. extern BSTR GUIDtoBSTR(LPGUID);
  7. extern HRESULT BSTRtoGUID(LPGUID,BSTR);
  8. extern HRESULT DPLAYBSTRtoGUID(LPGUID,BSTR);
  9. extern BOOL IsEmptyString(BSTR szString);
  10. extern void *g_dxj_DirectPlayLobbiedApplication;
  11. extern void *g_dxj_DirectPlayAddress;
  12. #define SAFE_DELETE(p) { if(p) { delete (p); p=NULL; } }
  13. #define SAFE_RELEASE(p) { __try { if(p) { (p)->Release(); (p)=NULL;} } __except(EXCEPTION_EXECUTE_HANDLER) { (p) = NULL;} }
  14. DWORD WINAPI CloseLobbiedAppThreadProc(void* lpParam);
  15. ///////////////////////////////////////////////////////////////////
  16. // InternalAddRef
  17. ///////////////////////////////////////////////////////////////////
  18. DWORD C_dxj_DirectPlayLobbiedApplicationObject::InternalAddRef(){
  19. DWORD i;
  20. i=CComObjectRoot::InternalAddRef();
  21. DPF1(1,"------ DXVB: DirectPlayLobbiedApplication8 AddRef %d \n",i);
  22. return i;
  23. }
  24. ///////////////////////////////////////////////////////////////////
  25. // InternalRelease
  26. ///////////////////////////////////////////////////////////////////
  27. DWORD C_dxj_DirectPlayLobbiedApplicationObject::InternalRelease(){
  28. DWORD i;
  29. i=CComObjectRoot::InternalRelease();
  30. DPF1(1,"------ DXVB: DirectPlayLobbiedApplication8 Release %d \n",i);
  31. return i;
  32. }
  33. ///////////////////////////////////////////////////////////////////
  34. // C_dxj_DirectPlayLobbiedApplicationObject
  35. ///////////////////////////////////////////////////////////////////
  36. C_dxj_DirectPlayLobbiedApplicationObject::C_dxj_DirectPlayLobbiedApplicationObject(){
  37. DPF(1,"------ DXVB: Constructor Creation DirectPlayLobbiedApplication8 \n ");
  38. m__dxj_DirectPlayLobbiedApplication = NULL;
  39. m_fInit = FALSE;
  40. m_fHandleEvents = FALSE;
  41. m_pEventStream=NULL;
  42. }
  43. ///////////////////////////////////////////////////////////////////
  44. // ~C_dxj_DirectPlayLobbiedApplicationObject
  45. ///////////////////////////////////////////////////////////////////
  46. C_dxj_DirectPlayLobbiedApplicationObject::~C_dxj_DirectPlayLobbiedApplicationObject()
  47. {
  48. DPF(1,"------ DXVB: Entering ~C_dxj_DirectPlayLobbiedApplicationObject destructor \n");
  49. SAFE_RELEASE(m__dxj_DirectPlayLobbiedApplication);
  50. m_fHandleEvents = FALSE;
  51. if (m_pEventStream)
  52. m_pEventStream->Release();
  53. }
  54. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::InternalGetObject(IUnknown **pUnk){
  55. *pUnk=(IUnknown*)m__dxj_DirectPlayLobbiedApplication;
  56. return S_OK;
  57. }
  58. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::InternalSetObject(IUnknown *pUnk){
  59. m__dxj_DirectPlayLobbiedApplication=(IDirectPlay8LobbiedApplication*)pUnk;
  60. return S_OK;
  61. }
  62. HRESULT WINAPI DirectPlayLobbiedAppMessageHandler( PVOID pvUserContext,
  63. DWORD dwMessageId,
  64. PVOID pMsgBuffer )
  65. {
  66. HRESULT hr=S_OK;
  67. BOOL fCallCoUninit = FALSE;
  68. VARIANT_BOOL fRejectMsg = VARIANT_FALSE;
  69. // User context for the message handler is a pointer to our class module.
  70. C_dxj_DirectPlayLobbiedApplicationObject *lpPeer = (C_dxj_DirectPlayLobbiedApplicationObject*)pvUserContext;
  71. DPF(1,"-----Entering (DPlayLobbiedApp) MessageHandler call...\n");
  72. if (!lpPeer) return E_FAIL;
  73. if (!lpPeer->m_pEventStream) return E_FAIL;
  74. if (!lpPeer->m_fHandleEvents)
  75. return S_OK;
  76. // First we need to set our stream seek back to the beginning
  77. // We will do this every time we enter this function since we don't know if
  78. // we are on the same thread or not...
  79. I_dxj_DirectPlayLobbyEvent *lpEvent = NULL;
  80. __try {
  81. LARGE_INTEGER l;
  82. l.QuadPart = 0;
  83. lpPeer->m_pEventStream->Seek(l, STREAM_SEEK_SET, NULL);
  84. hr = CoUnmarshalInterface(lpPeer->m_pEventStream, IID_I_dxj_DirectPlayEvent, (void**)&lpEvent);
  85. if (hr == CO_E_NOTINITIALIZED) // Call CoInit so we can unmarshal
  86. {
  87. CoInitialize(NULL);
  88. hr = CoUnmarshalInterface(lpPeer->m_pEventStream, IID_I_dxj_DirectPlayEvent, (void**)&lpEvent);
  89. fCallCoUninit = TRUE;
  90. }
  91. if (!lpEvent)
  92. {
  93. DPF(1,"-----Leaving (DplayLobbyClient) MessageHandler call (No event interface)...\n");
  94. return hr;
  95. }
  96. }
  97. __except(EXCEPTION_EXECUTE_HANDLER)
  98. {
  99. lpPeer->m_fHandleEvents = FALSE;
  100. DPF(1,"-----Leaving (DplayLobbyClient) MessageHandler call (Stream Gone)...\n");
  101. return S_OK;
  102. }
  103. switch( dwMessageId )
  104. {
  105. //Receive
  106. case DPL_MSGID_RECEIVE:
  107. {
  108. DPL_MESSAGE_RECEIVE *msg = (DPL_MESSAGE_RECEIVE*)pMsgBuffer;
  109. SAFEARRAY *lpData = NULL;
  110. SAFEARRAYBOUND rgsabound[1];
  111. DPL_MESSAGE_RECEIVE_CDESC m_dpReceive;
  112. BYTE *lpTemp = NULL;
  113. ZeroMemory(&m_dpReceive, sizeof(DPL_MESSAGE_RECEIVE));
  114. m_dpReceive.Sender = (long)msg->hSender;
  115. // Let's load our SafeArray
  116. if (msg->dwBufferSize)
  117. {
  118. rgsabound[0].lLbound = 0;
  119. rgsabound[0].cElements = msg->dwBufferSize;
  120. lpData = SafeArrayCreate(VT_UI1, 1, rgsabound);
  121. lpTemp = (BYTE*)lpData->pvData;
  122. lpData->pvData = msg->pBuffer;
  123. m_dpReceive.lBufferSize = msg->dwBufferSize;
  124. m_dpReceive.Buffer = lpData;
  125. }
  126. lpEvent->Receive(&m_dpReceive, &fRejectMsg);
  127. if (lpData) //Get rid of the safearray
  128. {
  129. lpData->pvData = lpTemp;
  130. SafeArrayDestroy(lpData);
  131. }
  132. break;
  133. }
  134. //Connect
  135. case DPL_MSGID_CONNECT:
  136. {
  137. DPL_MESSAGE_CONNECT *msg = (DPL_MESSAGE_CONNECT*)pMsgBuffer;
  138. DPNHANDLE m_hConnectID;
  139. DPL_MESSAGE_CONNECT_CDESC m_dpConnection;
  140. SAFEARRAY *lpData = NULL;
  141. SAFEARRAYBOUND rgsabound[1];
  142. WCHAR wszAddress[MAX_PATH];
  143. WCHAR wszDevice[MAX_PATH];
  144. DWORD dwNumChars = 0;
  145. BYTE *lpTemp = NULL;
  146. ZeroMemory(&m_dpConnection, sizeof(DPL_MESSAGE_CONNECT_CDESC));
  147. m_dpConnection.ConnectId = (long)msg->hConnectId;
  148. // Let's load our SafeArray
  149. if (msg->dwLobbyConnectDataSize)
  150. {
  151. rgsabound[0].lLbound = 0;
  152. rgsabound[0].cElements = msg->dwLobbyConnectDataSize;
  153. lpData = SafeArrayCreate(VT_UI1, 1, rgsabound);
  154. lpTemp = (BYTE*)lpData->pvData;
  155. lpData->pvData = msg->pvLobbyConnectData;
  156. m_dpConnection.LobbyConnectData = lpData;
  157. }
  158. lpPeer->GetVBConnSettings(msg->pdplConnectionSettings, &m_dpConnection.dplMsgCon);
  159. __try {
  160. if (msg->pdplConnectionSettings->pdp8HostAddress)
  161. {
  162. hr = msg->pdplConnectionSettings->pdp8HostAddress->GetURLW(NULL, &dwNumChars);
  163. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  164. return hr;
  165. if (FAILED (hr = msg->pdplConnectionSettings->pdp8HostAddress->GetURLW(&wszAddress[0],&dwNumChars) ) )
  166. return hr;
  167. m_dpConnection.dplMsgCon.AddressSenderUrl = SysAllocString(wszAddress);
  168. }
  169. }
  170. __except(EXCEPTION_EXECUTE_HANDLER)
  171. {
  172. // Just skip this part
  173. }
  174. __try {
  175. dwNumChars = 0;
  176. if ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)
  177. {
  178. hr = ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)->GetURLW(NULL, &dwNumChars);
  179. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  180. return hr;
  181. if (FAILED (hr = ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)->GetURLW(&wszDevice[0],&dwNumChars) ) )
  182. return hr;
  183. m_dpConnection.dplMsgCon.AddressDeviceUrl = SysAllocString(wszDevice);
  184. }
  185. }
  186. __except(EXCEPTION_EXECUTE_HANDLER)
  187. {
  188. // Just skip this part
  189. }
  190. lpEvent->Connect(&m_dpConnection, &fRejectMsg);
  191. if (lpData) //Get rid of the safearray
  192. {
  193. lpData->pvData = lpTemp;
  194. SafeArrayDestroy(lpData);
  195. }
  196. // Get rid of these addresses
  197. if (m_dpConnection.dplMsgCon.AddressSenderUrl)
  198. SysFreeString(m_dpConnection.dplMsgCon.AddressSenderUrl);
  199. if (m_dpConnection.dplMsgCon.AddressDeviceUrl)
  200. SysFreeString(m_dpConnection.dplMsgCon.AddressDeviceUrl);
  201. break;
  202. }
  203. //Disconnect
  204. case DPL_MSGID_DISCONNECT:
  205. {
  206. DPL_MESSAGE_DISCONNECT *msg = (DPL_MESSAGE_DISCONNECT*)pMsgBuffer;
  207. DPNHANDLE m_hDisconnectID;
  208. HRESULT m_hDisconnectReason;
  209. m_hDisconnectID = (long)msg->hDisconnectId;
  210. m_hDisconnectReason = (long) msg->hrReason;
  211. lpEvent->Disconnect(m_hDisconnectID, m_hDisconnectReason);
  212. break;
  213. }
  214. //Status
  215. case DPL_MSGID_SESSION_STATUS:
  216. {
  217. DPL_MESSAGE_SESSION_STATUS *msg = (DPL_MESSAGE_SESSION_STATUS*)pMsgBuffer;
  218. DPNHANDLE m_hSender;
  219. DWORD m_dwSessionStatus;
  220. m_dwSessionStatus = (long)msg->dwStatus;
  221. m_hSender = (long)msg->hSender;
  222. lpEvent->SessionStatus(m_dwSessionStatus,m_hSender);
  223. break;
  224. }
  225. //ConnectionSettings
  226. case DPL_MSGID_CONNECTION_SETTINGS:
  227. {
  228. DPL_MESSAGE_CONNECTION_SETTINGS *msg = (DPL_MESSAGE_CONNECTION_SETTINGS*)pMsgBuffer;
  229. DPL_MESSAGE_CONNECTION_SETTINGS_CDESC dpCon;
  230. WCHAR wszAddress[MAX_PATH];
  231. WCHAR wszDevice[MAX_PATH];
  232. DWORD dwNumChars = 0;
  233. ZeroMemory(&dpCon, sizeof(DPL_MESSAGE_CONNECTION_SETTINGS_CDESC));
  234. lpPeer->GetVBConnSettings(msg->pdplConnectionSettings, &dpCon.dplConnectionSettings);
  235. dpCon.hSender = (long)msg->hSender;
  236. __try {
  237. if (msg->pdplConnectionSettings->pdp8HostAddress)
  238. {
  239. hr = msg->pdplConnectionSettings->pdp8HostAddress->GetURLW(NULL, &dwNumChars);
  240. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  241. return hr;
  242. if (FAILED (hr = msg->pdplConnectionSettings->pdp8HostAddress->GetURLW(&wszAddress[0],&dwNumChars) ) )
  243. return hr;
  244. dpCon.dplConnectionSettings.AddressSenderUrl = SysAllocString(wszAddress);
  245. }
  246. }
  247. __except(EXCEPTION_EXECUTE_HANDLER)
  248. {
  249. // Just skip this part
  250. }
  251. __try {
  252. dwNumChars = 0;
  253. if ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)
  254. {
  255. hr = ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)->GetURLW(NULL, &dwNumChars);
  256. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  257. return hr;
  258. if (FAILED (hr = ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)->GetURLW(&wszDevice[0],&dwNumChars) ) )
  259. return hr;
  260. dpCon.dplConnectionSettings.AddressDeviceUrl = SysAllocString(wszDevice);
  261. }
  262. }
  263. __except(EXCEPTION_EXECUTE_HANDLER)
  264. {
  265. // Just skip this part
  266. }
  267. lpEvent->ConnectionSettings(&dpCon);
  268. // Get rid of these addresses
  269. if (dpCon.dplConnectionSettings.AddressSenderUrl)
  270. SysFreeString(dpCon.dplConnectionSettings.AddressSenderUrl);
  271. if (dpCon.dplConnectionSettings.AddressDeviceUrl)
  272. SysFreeString(dpCon.dplConnectionSettings.AddressDeviceUrl);
  273. break;
  274. }
  275. }
  276. __try {
  277. if (lpPeer->m_pEventStream)
  278. // clean up marshaled packet
  279. CoReleaseMarshalData(lpPeer->m_pEventStream);
  280. }
  281. __except(EXCEPTION_EXECUTE_HANDLER)
  282. {
  283. lpPeer->m_fHandleEvents = FALSE;
  284. return S_OK;
  285. }
  286. if (fCallCoUninit)
  287. CoUninitialize();
  288. DPF(1,"-----Leaving (DPlayLobbiedApp) MessageHandler call...\n");
  289. if (fRejectMsg != VARIANT_FALSE)
  290. return E_FAIL;
  291. return S_OK;
  292. }
  293. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::RegisterMessageHandler(I_dxj_DirectPlayLobbyEvent *lobbyEvent, long *lDPNHandle)
  294. {
  295. HRESULT hr=S_OK;
  296. LPSTREAM pStm=NULL;
  297. IUnknown *pUnk=NULL;
  298. DPF(1,"-----Entering (DPlayLobbiedApp) RegisterMessageHandler call...\n");
  299. if (!lobbyEvent) return E_INVALIDARG;
  300. if (!m_fHandleEvents)
  301. {
  302. if (m_pEventStream)
  303. m_pEventStream->Release();
  304. // Create a global stream. The stream needs to be global so we can
  305. // marshal once, and unmarshal as many times as necessary
  306. hr = CreateStreamOnHGlobal(NULL, TRUE, &pStm);
  307. if FAILED(hr) return hr;
  308. // Now we can marshal our IUnknown interface. We use MSHLFLAGS_TABLEWEAK
  309. // so we can unmarshal any number of times
  310. hr = CoMarshalInterface(pStm, IID_I_dxj_DirectPlayLobbyEvent, lobbyEvent, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
  311. if FAILED(hr) return hr;
  312. // Now we need to set the seek location of the stream to the beginning
  313. LARGE_INTEGER l;
  314. l.QuadPart = 0;
  315. pStm->Seek(l, STREAM_SEEK_SET, NULL);
  316. m_pEventStream=pStm;
  317. if (!m_fInit)
  318. {
  319. if (FAILED ( hr = m__dxj_DirectPlayLobbiedApplication->Initialize( this, DirectPlayLobbiedAppMessageHandler, (DPNHANDLE*) lDPNHandle, 0 ) ) )
  320. return hr;
  321. m_fInit = TRUE;
  322. }
  323. m_fHandleEvents = TRUE;
  324. }
  325. else
  326. return DPNERR_ALREADYINITIALIZED;
  327. return hr;
  328. }
  329. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::RegisterProgram(DPL_PROGRAM_DESC_CDESC *ProgramDesc,long lFlags)
  330. {
  331. HRESULT hr;
  332. DPL_PROGRAM_DESC dpProg;
  333. GUID guidApp;
  334. __try {
  335. DPF(1,"-----Entering (DPlayLobbiedApp) RegisterProgram call...\n");
  336. ZeroMemory(&guidApp, sizeof(GUID));
  337. if (FAILED (hr = BSTRtoGUID(&guidApp, ProgramDesc->guidApplication) ) )
  338. return hr;
  339. ZeroMemory(&dpProg, sizeof(DPL_PROGRAM_DESC) );
  340. // Fill out our struct
  341. dpProg.dwSize = sizeof(DPL_PROGRAM_DESC);
  342. dpProg.dwFlags = ProgramDesc->lFlags;
  343. dpProg.guidApplication = guidApp;
  344. dpProg.pwszApplicationName = ProgramDesc->ApplicationName;
  345. dpProg.pwszCommandLine = ProgramDesc->CommandLine;
  346. dpProg.pwszCurrentDirectory = ProgramDesc->CurrentDirectory;
  347. dpProg.pwszDescription = ProgramDesc->Description;
  348. dpProg.pwszExecutableFilename = ProgramDesc->ExecutableFilename;
  349. dpProg.pwszExecutablePath = ProgramDesc->ExecutablePath;
  350. dpProg.pwszLauncherFilename = ProgramDesc->LauncherFilename;
  351. dpProg.pwszLauncherPath = ProgramDesc->LauncherPath;
  352. if (FAILED( hr = m__dxj_DirectPlayLobbiedApplication->RegisterProgram(&dpProg, (DWORD) lFlags) ) )
  353. return hr;
  354. }
  355. __except(EXCEPTION_EXECUTE_HANDLER)
  356. {
  357. return E_FAIL;
  358. }
  359. return S_OK;
  360. }
  361. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::UnRegisterProgram(BSTR guidApplication,long lFlags)
  362. {
  363. HRESULT hr;
  364. GUID guidApp;
  365. __try {
  366. DPF(1,"-----Entering (DPlayLobbiedApp) UnregisterProgram call...\n");
  367. ZeroMemory(&guidApp, sizeof(GUID));
  368. if (FAILED (hr = BSTRtoGUID(&guidApp, guidApplication) ) )
  369. return hr;
  370. if (FAILED (hr = m__dxj_DirectPlayLobbiedApplication->UnRegisterProgram(&guidApp,(DWORD) lFlags) ) )
  371. return hr;
  372. }
  373. __except(EXCEPTION_EXECUTE_HANDLER)
  374. {
  375. return E_FAIL;
  376. }
  377. return S_OK;
  378. }
  379. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::Send(long Target,SAFEARRAY **Buffer,long lBufferSize,long lFlags)
  380. {
  381. HRESULT hr;
  382. DWORD dwBufferSize = 0;
  383. __try {
  384. DPF(1,"-----Entering (DPlayLobbiedApp) Send call...\n");
  385. dwBufferSize = (DWORD)lBufferSize;
  386. hr = m__dxj_DirectPlayLobbiedApplication->Send((DPNHANDLE) Target, (BYTE*)((SAFEARRAY*)*Buffer)->pvData, dwBufferSize, (DWORD) lFlags);
  387. if ((hr != DPNERR_PENDING) && FAILED(hr))
  388. return hr;
  389. }
  390. __except(EXCEPTION_EXECUTE_HANDLER)
  391. {
  392. return E_FAIL;
  393. }
  394. return S_OK;
  395. }
  396. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::SetAppAvailable(VARIANT_BOOL fAvailable, long lFlags)
  397. {
  398. HRESULT hr;
  399. __try {
  400. DPF(1,"-----Entering (DPlayLobbiedApp) SetAppAvailable call...\n");
  401. if (fAvailable == VARIANT_TRUE)
  402. {
  403. if (FAILED (hr = m__dxj_DirectPlayLobbiedApplication->SetAppAvailable(TRUE, (DWORD) lFlags) ) )
  404. return hr;
  405. }
  406. else
  407. {
  408. if (FAILED (hr = m__dxj_DirectPlayLobbiedApplication->SetAppAvailable(FALSE, (DWORD) lFlags) ) )
  409. return hr;
  410. }
  411. }
  412. __except(EXCEPTION_EXECUTE_HANDLER)
  413. {
  414. return E_FAIL;
  415. }
  416. return S_OK;
  417. }
  418. #if 0
  419. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::WaitForConnection(long lMilliseconds)
  420. {
  421. HRESULT hr;
  422. __try {
  423. DPF(1,"-----Entering (DPlayLobbiedApp) WaitForConnection call...\n");
  424. if (FAILED (hr = m__dxj_DirectPlayLobbiedApplication->WaitForConnection((DWORD) lMilliseconds, 0 ) ) )
  425. return hr;
  426. }
  427. __except(EXCEPTION_EXECUTE_HANDLER)
  428. {
  429. return E_FAIL;
  430. }
  431. return S_OK;
  432. }
  433. #endif
  434. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::UpdateStatus(long LobbyClient, long lStatus)
  435. {
  436. HRESULT hr;
  437. __try {
  438. DPF(1,"-----Entering (DPlayLobbiedApp) UpdateStatus call...\n");
  439. if (FAILED (hr = m__dxj_DirectPlayLobbiedApplication->UpdateStatus((DPNHANDLE) LobbyClient, (DWORD) lStatus, 0 ) ) )
  440. return hr;
  441. }
  442. __except(EXCEPTION_EXECUTE_HANDLER)
  443. {
  444. return E_FAIL;
  445. }
  446. return S_OK;
  447. }
  448. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::Close()
  449. {
  450. HRESULT hr;
  451. BOOL bGotMsg = FALSE;
  452. BOOL bWait = FALSE;
  453. DWORD dwObj = 0;
  454. int i=0;
  455. MSG msg;
  456. __try {
  457. DPF(1,"-----Entering (DPlayLobbiedApp) Close call...\n");
  458. HANDLE hThread = NULL;
  459. DWORD dwThread = 0;
  460. hThread = CreateThread(NULL, 0, &CloseLobbiedAppThreadProc, this->m__dxj_DirectPlayLobbiedApplication, 0, &dwThread);
  461. msg.message = WM_NULL;
  462. while ((WM_QUIT != msg.message) && (!bWait))
  463. {
  464. bGotMsg = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
  465. i++;
  466. if ((!bGotMsg) || (i>10))
  467. {
  468. dwObj = WaitForSingleObject(hThread, 10);
  469. bWait = (dwObj == WAIT_OBJECT_0);
  470. i = 0;
  471. }
  472. if (bGotMsg)
  473. {
  474. TranslateMessage( &msg );
  475. DispatchMessage( &msg );
  476. }
  477. bGotMsg = FALSE;
  478. }
  479. }
  480. __except(EXCEPTION_EXECUTE_HANDLER)
  481. {
  482. return E_FAIL;
  483. }
  484. return S_OK;
  485. }
  486. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::UnRegisterMessageHandler()
  487. {
  488. DPF(1,"-----Entering (DPlayLobbiedApp) UnregisterMessageHandler call...\n");
  489. m_fHandleEvents = FALSE;
  490. return S_OK;
  491. }
  492. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::GetConnectionSettings(long hLobbyClient, long lFlags, DPL_CONNECTION_SETTINGS_CDESC *ConnectionSettings)
  493. {
  494. DPL_CONNECTION_SETTINGS *desc = NULL;
  495. DWORD dwSize = 0;
  496. HRESULT hr = S_OK;
  497. WCHAR wszAddress[MAX_PATH];
  498. WCHAR wszDevice[MAX_PATH];
  499. DWORD dwNumChars = 0;
  500. __try {
  501. hr= m__dxj_DirectPlayLobbiedApplication->GetConnectionSettings((DPNHANDLE) hLobbyClient, NULL, &dwSize, (DWORD) lFlags);
  502. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  503. return hr;
  504. desc = (DPL_CONNECTION_SETTINGS*)new BYTE[dwSize];
  505. desc->dwSize = sizeof(DPL_CONNECTION_SETTINGS);
  506. hr= m__dxj_DirectPlayLobbiedApplication->GetConnectionSettings((DPNHANDLE) hLobbyClient, desc, &dwSize, (DWORD) lFlags);
  507. if( FAILED(hr))
  508. return hr;
  509. __try {
  510. hr = desc->pdp8HostAddress->GetURLW(NULL, &dwNumChars);
  511. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  512. return hr;
  513. if (FAILED (hr = desc->pdp8HostAddress->GetURLW(&wszAddress[0],&dwNumChars) ) )
  514. return hr;
  515. ConnectionSettings->AddressSenderUrl = SysAllocString(wszAddress);
  516. }
  517. __except(EXCEPTION_EXECUTE_HANDLER)
  518. {
  519. // Just skip this part
  520. }
  521. __try {
  522. dwNumChars = 0;
  523. hr = ((IDirectPlay8Address*)*desc->ppdp8DeviceAddresses)->GetURLW(NULL, &dwNumChars);
  524. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  525. return hr;
  526. if (FAILED (hr = ((IDirectPlay8Address*)*desc->ppdp8DeviceAddresses)->GetURLW(&wszDevice[0],&dwNumChars) ) )
  527. return hr;
  528. ConnectionSettings->AddressDeviceUrl = SysAllocString(wszDevice);
  529. }
  530. __except(EXCEPTION_EXECUTE_HANDLER)
  531. {
  532. // Just skip this part
  533. }
  534. GetVBConnSettings(desc, ConnectionSettings);
  535. }
  536. __except(EXCEPTION_EXECUTE_HANDLER)
  537. {
  538. return E_FAIL;
  539. }
  540. return S_OK;
  541. }
  542. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::SetConnectionSettings(long hTarget, long lFlags, DPL_CONNECTION_SETTINGS_CDESC *ConnectionSettings, I_dxj_DirectPlayAddress *HostAddress, I_dxj_DirectPlayAddress *Device)
  543. {
  544. DPL_CONNECTION_SETTINGS desc;
  545. HRESULT hr = S_OK;
  546. GUID guidApp;
  547. GUID guidInst;
  548. WCHAR wszSessionName[MAX_PATH];
  549. WCHAR wszPassword[MAX_PATH];
  550. __try {
  551. ZeroMemory(&desc, sizeof(DPL_CONNECTION_SETTINGS));
  552. desc.dwSize = sizeof(DPL_CONNECTION_SETTINGS);
  553. desc.dwFlags = ConnectionSettings->lFlags;
  554. HostAddress->InternalGetObject((IUnknown**)&desc.pdp8HostAddress);
  555. Device->InternalGetObject((IUnknown**)desc.ppdp8DeviceAddresses);
  556. ZeroMemory(&desc.dpnAppDesc, sizeof(DPN_APPLICATION_DESC));
  557. // Set up our Desc
  558. desc.dpnAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
  559. if (!IsEmptyString(ConnectionSettings->ApplicationDescription.SessionName))
  560. {
  561. wcscpy(wszSessionName,ConnectionSettings->ApplicationDescription.SessionName);
  562. desc.dpnAppDesc.pwszSessionName = wszSessionName;
  563. }
  564. if (!IsEmptyString(ConnectionSettings->ApplicationDescription.Password))
  565. {
  566. wcscpy(wszPassword,ConnectionSettings->ApplicationDescription.Password);
  567. desc.dpnAppDesc.pwszPassword = wszPassword;
  568. }
  569. desc.dpnAppDesc.dwFlags = ConnectionSettings->ApplicationDescription.lFlags;
  570. desc.dpnAppDesc.dwMaxPlayers = ConnectionSettings->ApplicationDescription.lMaxPlayers;
  571. desc.dpnAppDesc.dwCurrentPlayers = ConnectionSettings->ApplicationDescription.lCurrentPlayers;
  572. if (FAILED(hr = DPLAYBSTRtoGUID(&guidApp, ConnectionSettings->ApplicationDescription.guidApplication) ) )
  573. return hr;
  574. desc.dpnAppDesc.guidApplication = guidApp;
  575. if (FAILED(hr = DPLAYBSTRtoGUID(&guidInst, ConnectionSettings->ApplicationDescription.guidInstance) ) )
  576. return hr;
  577. desc.dpnAppDesc.guidInstance = guidInst;
  578. hr= m__dxj_DirectPlayLobbiedApplication->SetConnectionSettings((DPNHANDLE) hTarget, &desc, (DWORD) lFlags);
  579. if( FAILED(hr))
  580. return hr;
  581. }
  582. __except(EXCEPTION_EXECUTE_HANDLER)
  583. {
  584. return E_FAIL;
  585. }
  586. return S_OK;
  587. }
  588. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::GetVBConnSettings(DPL_CONNECTION_SETTINGS *OldCon, DPL_CONNECTION_SETTINGS_CDESC *NewCon)
  589. {
  590. IDirectPlay8Address *dpAdd = NULL;
  591. HRESULT hr;
  592. __try {
  593. NewCon->lSize = OldCon->dwSize;
  594. NewCon->lFlags = OldCon->dwFlags;
  595. NewCon->PlayerName = SysAllocString(OldCon->pwszPlayerName);
  596. ZeroMemory(&NewCon->ApplicationDescription, sizeof(DPN_APPLICATION_DESC_CDESC));
  597. // Set up our Desc
  598. NewCon->ApplicationDescription.lSize = OldCon->dpnAppDesc.dwSize;
  599. NewCon->ApplicationDescription.SessionName = SysAllocString(OldCon->dpnAppDesc.pwszSessionName);
  600. NewCon->ApplicationDescription.Password = SysAllocString(OldCon->dpnAppDesc.pwszPassword);
  601. NewCon->ApplicationDescription.lFlags = OldCon->dpnAppDesc.dwFlags;
  602. NewCon->ApplicationDescription.lMaxPlayers = OldCon->dpnAppDesc.dwMaxPlayers;
  603. NewCon->ApplicationDescription.lCurrentPlayers = OldCon->dpnAppDesc.dwCurrentPlayers;
  604. NewCon->ApplicationDescription.guidApplication = GUIDtoBSTR(&OldCon->dpnAppDesc.guidApplication);
  605. NewCon->ApplicationDescription.guidInstance = GUIDtoBSTR(&OldCon->dpnAppDesc.guidInstance);
  606. }
  607. __except(EXCEPTION_EXECUTE_HANDLER)
  608. {
  609. return E_FAIL;
  610. }
  611. return S_OK;
  612. }
  613. DWORD WINAPI CloseLobbiedAppThreadProc(void* lpParam)
  614. {
  615. // User context for the message handler is a pointer to our class module.
  616. IDirectPlay8LobbiedApplication *lpPeer = (IDirectPlay8LobbiedApplication*)lpParam;
  617. DPF(1,"-----Entering (DPlayLobbiedApp) ClosePeerThreadProc call...\n");
  618. lpPeer->Close(0);
  619. DPF(1,"-----Leaving (DPlayLobbiedApp) ClosePeerThreadProc call ...\n");
  620. return 0;
  621. }