Leaked source code of windows server 2003
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.

765 lines
23 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_CDESC));
  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 = NULL;
  143. WCHAR* wszDevice = NULL;
  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. wszAddress = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  166. if (FAILED (hr = msg->pdplConnectionSettings->pdp8HostAddress->GetURLW(wszAddress,&dwNumChars) ) )
  167. return hr;
  168. m_dpConnection.dplMsgCon.AddressSenderUrl = SysAllocString(wszAddress);
  169. }
  170. }
  171. __except(EXCEPTION_EXECUTE_HANDLER)
  172. {
  173. // Just skip this part
  174. }
  175. __try {
  176. dwNumChars = 0;
  177. if ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)
  178. {
  179. hr = ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)->GetURLW(NULL, &dwNumChars);
  180. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  181. return hr;
  182. wszDevice = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  183. if (FAILED (hr = ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)->GetURLW(wszDevice,&dwNumChars) ) )
  184. return hr;
  185. m_dpConnection.dplMsgCon.AddressDeviceUrl = SysAllocString(wszDevice);
  186. }
  187. }
  188. __except(EXCEPTION_EXECUTE_HANDLER)
  189. {
  190. // Just skip this part
  191. }
  192. lpEvent->Connect(&m_dpConnection, &fRejectMsg);
  193. if (lpData) //Get rid of the safearray
  194. {
  195. lpData->pvData = lpTemp;
  196. SafeArrayDestroy(lpData);
  197. }
  198. // Get rid of these addresses
  199. if (m_dpConnection.dplMsgCon.AddressSenderUrl)
  200. SysFreeString(m_dpConnection.dplMsgCon.AddressSenderUrl);
  201. if (m_dpConnection.dplMsgCon.AddressDeviceUrl)
  202. SysFreeString(m_dpConnection.dplMsgCon.AddressDeviceUrl);
  203. break;
  204. }
  205. //Disconnect
  206. case DPL_MSGID_DISCONNECT:
  207. {
  208. DPL_MESSAGE_DISCONNECT *msg = (DPL_MESSAGE_DISCONNECT*)pMsgBuffer;
  209. DPNHANDLE m_hDisconnectID;
  210. HRESULT m_hDisconnectReason;
  211. m_hDisconnectID = (long)msg->hDisconnectId;
  212. m_hDisconnectReason = (long) msg->hrReason;
  213. lpEvent->Disconnect(m_hDisconnectID, m_hDisconnectReason);
  214. break;
  215. }
  216. //Status
  217. case DPL_MSGID_SESSION_STATUS:
  218. {
  219. DPL_MESSAGE_SESSION_STATUS *msg = (DPL_MESSAGE_SESSION_STATUS*)pMsgBuffer;
  220. DPNHANDLE m_hSender;
  221. DWORD m_dwSessionStatus;
  222. m_dwSessionStatus = (long)msg->dwStatus;
  223. m_hSender = (long)msg->hSender;
  224. lpEvent->SessionStatus(m_dwSessionStatus,m_hSender);
  225. break;
  226. }
  227. //ConnectionSettings
  228. case DPL_MSGID_CONNECTION_SETTINGS:
  229. {
  230. DPL_MESSAGE_CONNECTION_SETTINGS *msg = (DPL_MESSAGE_CONNECTION_SETTINGS*)pMsgBuffer;
  231. DPL_MESSAGE_CONNECTION_SETTINGS_CDESC dpCon;
  232. WCHAR* wszAddress = NULL;
  233. WCHAR* wszDevice = NULL;
  234. DWORD dwNumChars = 0;
  235. ZeroMemory(&dpCon, sizeof(DPL_MESSAGE_CONNECTION_SETTINGS_CDESC));
  236. lpPeer->GetVBConnSettings(msg->pdplConnectionSettings, &dpCon.dplConnectionSettings);
  237. dpCon.hSender = (long)msg->hSender;
  238. __try {
  239. if (msg->pdplConnectionSettings->pdp8HostAddress)
  240. {
  241. hr = msg->pdplConnectionSettings->pdp8HostAddress->GetURLW(NULL, &dwNumChars);
  242. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  243. return hr;
  244. wszAddress = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  245. if (FAILED (hr = msg->pdplConnectionSettings->pdp8HostAddress->GetURLW(wszAddress,&dwNumChars) ) )
  246. return hr;
  247. dpCon.dplConnectionSettings.AddressSenderUrl = SysAllocString(wszAddress);
  248. }
  249. }
  250. __except(EXCEPTION_EXECUTE_HANDLER)
  251. {
  252. // Just skip this part
  253. }
  254. __try {
  255. dwNumChars = 0;
  256. if ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)
  257. {
  258. hr = ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)->GetURLW(NULL, &dwNumChars);
  259. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  260. return hr;
  261. wszDevice = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  262. if (FAILED (hr = ((IDirectPlay8Address*)*msg->pdplConnectionSettings->ppdp8DeviceAddresses)->GetURLW(wszDevice,&dwNumChars) ) )
  263. return hr;
  264. dpCon.dplConnectionSettings.AddressDeviceUrl = SysAllocString(wszDevice);
  265. }
  266. }
  267. __except(EXCEPTION_EXECUTE_HANDLER)
  268. {
  269. // Just skip this part
  270. }
  271. lpEvent->ConnectionSettings(&dpCon);
  272. // Get rid of these addresses
  273. if (dpCon.dplConnectionSettings.AddressSenderUrl)
  274. SysFreeString(dpCon.dplConnectionSettings.AddressSenderUrl);
  275. if (dpCon.dplConnectionSettings.AddressDeviceUrl)
  276. SysFreeString(dpCon.dplConnectionSettings.AddressDeviceUrl);
  277. break;
  278. }
  279. }
  280. __try {
  281. if (lpPeer->m_pEventStream)
  282. // clean up marshaled packet
  283. CoReleaseMarshalData(lpPeer->m_pEventStream);
  284. }
  285. __except(EXCEPTION_EXECUTE_HANDLER)
  286. {
  287. lpPeer->m_fHandleEvents = FALSE;
  288. return S_OK;
  289. }
  290. if (fCallCoUninit)
  291. CoUninitialize();
  292. DPF(1,"-----Leaving (DPlayLobbiedApp) MessageHandler call...\n");
  293. if (fRejectMsg != VARIANT_FALSE)
  294. return E_FAIL;
  295. return S_OK;
  296. }
  297. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::RegisterMessageHandler(I_dxj_DirectPlayLobbyEvent *lobbyEvent, long *lDPNHandle)
  298. {
  299. HRESULT hr=S_OK;
  300. LPSTREAM pStm=NULL;
  301. IUnknown *pUnk=NULL;
  302. DPF(1,"-----Entering (DPlayLobbiedApp) RegisterMessageHandler call...\n");
  303. if (!lobbyEvent) return E_INVALIDARG;
  304. if (!m_fHandleEvents)
  305. {
  306. if (m_pEventStream)
  307. m_pEventStream->Release();
  308. // Create a global stream. The stream needs to be global so we can
  309. // marshal once, and unmarshal as many times as necessary
  310. hr = CreateStreamOnHGlobal(NULL, TRUE, &pStm);
  311. if FAILED(hr) return hr;
  312. // Now we can marshal our IUnknown interface. We use MSHLFLAGS_TABLEWEAK
  313. // so we can unmarshal any number of times
  314. hr = CoMarshalInterface(pStm, IID_I_dxj_DirectPlayLobbyEvent, lobbyEvent, MSHCTX_INPROC, NULL, MSHLFLAGS_TABLEWEAK);
  315. if FAILED(hr) return hr;
  316. // Now we need to set the seek location of the stream to the beginning
  317. LARGE_INTEGER l;
  318. l.QuadPart = 0;
  319. pStm->Seek(l, STREAM_SEEK_SET, NULL);
  320. m_pEventStream=pStm;
  321. if (!m_fInit)
  322. {
  323. if (FAILED ( hr = m__dxj_DirectPlayLobbiedApplication->Initialize( this, DirectPlayLobbiedAppMessageHandler, (DPNHANDLE*) lDPNHandle, 0 ) ) )
  324. return hr;
  325. m_fInit = TRUE;
  326. }
  327. m_fHandleEvents = TRUE;
  328. }
  329. else
  330. return DPNERR_ALREADYINITIALIZED;
  331. return hr;
  332. }
  333. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::RegisterProgram(DPL_PROGRAM_DESC_CDESC *ProgramDesc,long lFlags)
  334. {
  335. HRESULT hr;
  336. DPL_PROGRAM_DESC dpProg;
  337. GUID guidApp;
  338. __try {
  339. DPF(1,"-----Entering (DPlayLobbiedApp) RegisterProgram call...\n");
  340. ZeroMemory(&guidApp, sizeof(GUID));
  341. if (FAILED (hr = BSTRtoGUID(&guidApp, ProgramDesc->guidApplication) ) )
  342. return hr;
  343. ZeroMemory(&dpProg, sizeof(DPL_PROGRAM_DESC) );
  344. // Fill out our struct
  345. dpProg.dwSize = sizeof(DPL_PROGRAM_DESC);
  346. dpProg.dwFlags = ProgramDesc->lFlags;
  347. dpProg.guidApplication = guidApp;
  348. dpProg.pwszApplicationName = ProgramDesc->ApplicationName;
  349. dpProg.pwszCommandLine = ProgramDesc->CommandLine;
  350. dpProg.pwszCurrentDirectory = ProgramDesc->CurrentDirectory;
  351. dpProg.pwszDescription = ProgramDesc->Description;
  352. dpProg.pwszExecutableFilename = ProgramDesc->ExecutableFilename;
  353. dpProg.pwszExecutablePath = ProgramDesc->ExecutablePath;
  354. dpProg.pwszLauncherFilename = ProgramDesc->LauncherFilename;
  355. dpProg.pwszLauncherPath = ProgramDesc->LauncherPath;
  356. if (FAILED( hr = m__dxj_DirectPlayLobbiedApplication->RegisterProgram(&dpProg, (DWORD) lFlags) ) )
  357. return hr;
  358. }
  359. __except(EXCEPTION_EXECUTE_HANDLER)
  360. {
  361. return E_FAIL;
  362. }
  363. return S_OK;
  364. }
  365. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::UnRegisterProgram(BSTR guidApplication,long lFlags)
  366. {
  367. HRESULT hr;
  368. GUID guidApp;
  369. __try {
  370. DPF(1,"-----Entering (DPlayLobbiedApp) UnregisterProgram call...\n");
  371. ZeroMemory(&guidApp, sizeof(GUID));
  372. if (FAILED (hr = BSTRtoGUID(&guidApp, guidApplication) ) )
  373. return hr;
  374. if (FAILED (hr = m__dxj_DirectPlayLobbiedApplication->UnRegisterProgram(&guidApp,(DWORD) lFlags) ) )
  375. return hr;
  376. }
  377. __except(EXCEPTION_EXECUTE_HANDLER)
  378. {
  379. return E_FAIL;
  380. }
  381. return S_OK;
  382. }
  383. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::Send(long Target,SAFEARRAY **Buffer,long lBufferSize,long lFlags)
  384. {
  385. HRESULT hr;
  386. DWORD dwBufferSize = 0;
  387. __try {
  388. DPF(1,"-----Entering (DPlayLobbiedApp) Send call...\n");
  389. dwBufferSize = (DWORD)lBufferSize;
  390. hr = m__dxj_DirectPlayLobbiedApplication->Send((DPNHANDLE) Target, (BYTE*)((SAFEARRAY*)*Buffer)->pvData, dwBufferSize, (DWORD) lFlags);
  391. if ((hr != DPNERR_PENDING) && FAILED(hr))
  392. return hr;
  393. }
  394. __except(EXCEPTION_EXECUTE_HANDLER)
  395. {
  396. return E_FAIL;
  397. }
  398. return S_OK;
  399. }
  400. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::SetAppAvailable(VARIANT_BOOL fAvailable, long lFlags)
  401. {
  402. HRESULT hr;
  403. __try {
  404. DPF(1,"-----Entering (DPlayLobbiedApp) SetAppAvailable call...\n");
  405. if (fAvailable == VARIANT_TRUE)
  406. {
  407. if (FAILED (hr = m__dxj_DirectPlayLobbiedApplication->SetAppAvailable(TRUE, (DWORD) lFlags) ) )
  408. return hr;
  409. }
  410. else
  411. {
  412. if (FAILED (hr = m__dxj_DirectPlayLobbiedApplication->SetAppAvailable(FALSE, (DWORD) lFlags) ) )
  413. return hr;
  414. }
  415. }
  416. __except(EXCEPTION_EXECUTE_HANDLER)
  417. {
  418. return E_FAIL;
  419. }
  420. return S_OK;
  421. }
  422. #if 0
  423. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::WaitForConnection(long lMilliseconds)
  424. {
  425. HRESULT hr;
  426. __try {
  427. DPF(1,"-----Entering (DPlayLobbiedApp) WaitForConnection call...\n");
  428. if (FAILED (hr = m__dxj_DirectPlayLobbiedApplication->WaitForConnection((DWORD) lMilliseconds, 0 ) ) )
  429. return hr;
  430. }
  431. __except(EXCEPTION_EXECUTE_HANDLER)
  432. {
  433. return E_FAIL;
  434. }
  435. return S_OK;
  436. }
  437. #endif
  438. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::UpdateStatus(long LobbyClient, long lStatus)
  439. {
  440. HRESULT hr;
  441. __try {
  442. DPF(1,"-----Entering (DPlayLobbiedApp) UpdateStatus call...\n");
  443. if (FAILED (hr = m__dxj_DirectPlayLobbiedApplication->UpdateStatus((DPNHANDLE) LobbyClient, (DWORD) lStatus, 0 ) ) )
  444. return hr;
  445. }
  446. __except(EXCEPTION_EXECUTE_HANDLER)
  447. {
  448. return E_FAIL;
  449. }
  450. return S_OK;
  451. }
  452. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::Close()
  453. {
  454. HRESULT hr;
  455. BOOL bGotMsg = FALSE;
  456. BOOL bWait = FALSE;
  457. DWORD dwObj = 0;
  458. int i=0;
  459. MSG msg;
  460. __try {
  461. DPF(1,"-----Entering (DPlayLobbiedApp) Close call...\n");
  462. HANDLE hThread = NULL;
  463. DWORD dwThread = 0;
  464. hThread = CreateThread(NULL, 0, &CloseLobbiedAppThreadProc, this->m__dxj_DirectPlayLobbiedApplication, 0, &dwThread);
  465. msg.message = WM_NULL;
  466. while ((WM_QUIT != msg.message) && (!bWait))
  467. {
  468. bGotMsg = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
  469. i++;
  470. if ((!bGotMsg) || (i>10))
  471. {
  472. dwObj = WaitForSingleObject(hThread, 10);
  473. bWait = (dwObj == WAIT_OBJECT_0);
  474. i = 0;
  475. }
  476. if (bGotMsg)
  477. {
  478. TranslateMessage( &msg );
  479. DispatchMessage( &msg );
  480. }
  481. bGotMsg = FALSE;
  482. }
  483. }
  484. __except(EXCEPTION_EXECUTE_HANDLER)
  485. {
  486. return E_FAIL;
  487. }
  488. return S_OK;
  489. }
  490. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::UnRegisterMessageHandler()
  491. {
  492. DPF(1,"-----Entering (DPlayLobbiedApp) UnregisterMessageHandler call...\n");
  493. m_fHandleEvents = FALSE;
  494. return S_OK;
  495. }
  496. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::GetConnectionSettings(long hLobbyClient, long lFlags, DPL_CONNECTION_SETTINGS_CDESC *ConnectionSettings)
  497. {
  498. DPL_CONNECTION_SETTINGS *desc = NULL;
  499. DWORD dwSize = 0;
  500. HRESULT hr = S_OK;
  501. WCHAR* wszAddress = NULL;
  502. WCHAR* wszDevice = NULL;
  503. DWORD dwNumChars = 0;
  504. __try {
  505. hr= m__dxj_DirectPlayLobbiedApplication->GetConnectionSettings((DPNHANDLE) hLobbyClient, NULL, &dwSize, (DWORD) lFlags);
  506. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  507. return hr;
  508. desc = (DPL_CONNECTION_SETTINGS*)new BYTE[dwSize];
  509. desc->dwSize = sizeof(DPL_CONNECTION_SETTINGS);
  510. hr= m__dxj_DirectPlayLobbiedApplication->GetConnectionSettings((DPNHANDLE) hLobbyClient, desc, &dwSize, (DWORD) lFlags);
  511. if( FAILED(hr))
  512. return hr;
  513. __try {
  514. hr = desc->pdp8HostAddress->GetURLW(NULL, &dwNumChars);
  515. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  516. return hr;
  517. wszAddress = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  518. if (FAILED (hr = desc->pdp8HostAddress->GetURLW(wszAddress,&dwNumChars) ) )
  519. return hr;
  520. ConnectionSettings->AddressSenderUrl = SysAllocString(wszAddress);
  521. }
  522. __except(EXCEPTION_EXECUTE_HANDLER)
  523. {
  524. // Just skip this part
  525. }
  526. __try {
  527. dwNumChars = 0;
  528. hr = ((IDirectPlay8Address*)*desc->ppdp8DeviceAddresses)->GetURLW(NULL, &dwNumChars);
  529. if( FAILED(hr) && hr != DPNERR_BUFFERTOOSMALL)
  530. return hr;
  531. wszDevice = (WCHAR*)_alloca(sizeof(WCHAR) * dwNumChars);
  532. if (FAILED (hr = ((IDirectPlay8Address*)*desc->ppdp8DeviceAddresses)->GetURLW(wszDevice,&dwNumChars) ) )
  533. return hr;
  534. ConnectionSettings->AddressDeviceUrl = SysAllocString(wszDevice);
  535. }
  536. __except(EXCEPTION_EXECUTE_HANDLER)
  537. {
  538. // Just skip this part
  539. }
  540. GetVBConnSettings(desc, ConnectionSettings);
  541. }
  542. __except(EXCEPTION_EXECUTE_HANDLER)
  543. {
  544. return E_FAIL;
  545. }
  546. return S_OK;
  547. }
  548. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::SetConnectionSettings(long hTarget, long lFlags, DPL_CONNECTION_SETTINGS_CDESC *ConnectionSettings, I_dxj_DirectPlayAddress *HostAddress, I_dxj_DirectPlayAddress *Device)
  549. {
  550. DPL_CONNECTION_SETTINGS desc;
  551. HRESULT hr = S_OK;
  552. GUID guidApp;
  553. GUID guidInst;
  554. WCHAR wszSessionName[MAX_PATH];
  555. WCHAR wszPassword[MAX_PATH];
  556. __try {
  557. ZeroMemory(&desc, sizeof(DPL_CONNECTION_SETTINGS));
  558. desc.dwSize = sizeof(DPL_CONNECTION_SETTINGS);
  559. desc.dwFlags = ConnectionSettings->lFlags;
  560. HostAddress->InternalGetObject((IUnknown**)&desc.pdp8HostAddress);
  561. Device->InternalGetObject((IUnknown**)desc.ppdp8DeviceAddresses);
  562. ZeroMemory(&desc.dpnAppDesc, sizeof(DPN_APPLICATION_DESC));
  563. // Set up our Desc
  564. desc.dpnAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
  565. if (!IsEmptyString(ConnectionSettings->ApplicationDescription.SessionName))
  566. {
  567. wcscpy(wszSessionName,ConnectionSettings->ApplicationDescription.SessionName);
  568. desc.dpnAppDesc.pwszSessionName = wszSessionName;
  569. }
  570. if (!IsEmptyString(ConnectionSettings->ApplicationDescription.Password))
  571. {
  572. wcscpy(wszPassword,ConnectionSettings->ApplicationDescription.Password);
  573. desc.dpnAppDesc.pwszPassword = wszPassword;
  574. }
  575. desc.dpnAppDesc.dwFlags = ConnectionSettings->ApplicationDescription.lFlags;
  576. desc.dpnAppDesc.dwMaxPlayers = ConnectionSettings->ApplicationDescription.lMaxPlayers;
  577. desc.dpnAppDesc.dwCurrentPlayers = ConnectionSettings->ApplicationDescription.lCurrentPlayers;
  578. if (FAILED(hr = DPLAYBSTRtoGUID(&guidApp, ConnectionSettings->ApplicationDescription.guidApplication) ) )
  579. return hr;
  580. desc.dpnAppDesc.guidApplication = guidApp;
  581. if (FAILED(hr = DPLAYBSTRtoGUID(&guidInst, ConnectionSettings->ApplicationDescription.guidInstance) ) )
  582. return hr;
  583. desc.dpnAppDesc.guidInstance = guidInst;
  584. hr= m__dxj_DirectPlayLobbiedApplication->SetConnectionSettings((DPNHANDLE) hTarget, &desc, (DWORD) lFlags);
  585. if( FAILED(hr))
  586. return hr;
  587. }
  588. __except(EXCEPTION_EXECUTE_HANDLER)
  589. {
  590. return E_FAIL;
  591. }
  592. return S_OK;
  593. }
  594. HRESULT C_dxj_DirectPlayLobbiedApplicationObject::GetVBConnSettings(DPL_CONNECTION_SETTINGS *OldCon, DPL_CONNECTION_SETTINGS_CDESC *NewCon)
  595. {
  596. IDirectPlay8Address *dpAdd = NULL;
  597. HRESULT hr;
  598. __try {
  599. NewCon->lSize = OldCon->dwSize;
  600. NewCon->lFlags = OldCon->dwFlags;
  601. NewCon->PlayerName = SysAllocString(OldCon->pwszPlayerName);
  602. ZeroMemory(&NewCon->ApplicationDescription, sizeof(DPN_APPLICATION_DESC_CDESC));
  603. // Set up our Desc
  604. NewCon->ApplicationDescription.lSize = OldCon->dpnAppDesc.dwSize;
  605. NewCon->ApplicationDescription.SessionName = SysAllocString(OldCon->dpnAppDesc.pwszSessionName);
  606. NewCon->ApplicationDescription.Password = SysAllocString(OldCon->dpnAppDesc.pwszPassword);
  607. NewCon->ApplicationDescription.lFlags = OldCon->dpnAppDesc.dwFlags;
  608. NewCon->ApplicationDescription.lMaxPlayers = OldCon->dpnAppDesc.dwMaxPlayers;
  609. NewCon->ApplicationDescription.lCurrentPlayers = OldCon->dpnAppDesc.dwCurrentPlayers;
  610. NewCon->ApplicationDescription.guidApplication = GUIDtoBSTR(&OldCon->dpnAppDesc.guidApplication);
  611. NewCon->ApplicationDescription.guidInstance = GUIDtoBSTR(&OldCon->dpnAppDesc.guidInstance);
  612. }
  613. __except(EXCEPTION_EXECUTE_HANDLER)
  614. {
  615. return E_FAIL;
  616. }
  617. return S_OK;
  618. }
  619. DWORD WINAPI CloseLobbiedAppThreadProc(void* lpParam)
  620. {
  621. // User context for the message handler is a pointer to our class module.
  622. IDirectPlay8LobbiedApplication *lpPeer = (IDirectPlay8LobbiedApplication*)lpParam;
  623. DPF(1,"-----Entering (DPlayLobbiedApp) ClosePeerThreadProc call...\n");
  624. lpPeer->Close(0);
  625. DPF(1,"-----Leaving (DPlayLobbiedApp) ClosePeerThreadProc call ...\n");
  626. return 0;
  627. }