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.

219 lines
4.3 KiB

  1. #include "stdafx.h"
  2. #include "dccsink.h"
  3. //#include "findleak.h"
  4. #ifdef ATTEMPT_DEVICE_CONNECTION_NOTIFICATION
  5. #include "cewmdmdbt.h"
  6. #endif
  7. #define SAFE_CLOSEHANDLE(h) CloseHandle((h))
  8. //DECLARE_THIS_FILE;
  9. //
  10. // Construction/Destruction
  11. //
  12. CDCCSink::CDCCSink() :
  13. m_hThread(NULL),
  14. m_hThreadEvent(NULL),
  15. m_dwThreadId(0),
  16. m_dwDccSink(-1),
  17. m_hrRapi( 0 )
  18. {
  19. }
  20. HRESULT CDCCSink::Initialize()
  21. {
  22. HRESULT hr = S_OK;
  23. m_hThreadEvent = CreateEvent(NULL, FALSE, FALSE, NULL );
  24. if( NULL == m_hThreadEvent )
  25. {
  26. hr = HRESULT_FROM_WIN32( GetLastError() );
  27. }
  28. if( SUCCEEDED( hr ) )
  29. {
  30. m_hThread = CreateThread( NULL, 0, CreateDCCManProc, this, 0, &m_dwThreadId );
  31. if( NULL == m_hThread )
  32. {
  33. hr = HRESULT_FROM_WIN32( GetLastError() );
  34. }
  35. }
  36. if( SUCCEEDED( hr ) )
  37. {
  38. if( WAIT_OBJECT_0 != WaitForSingleObject( m_hThreadEvent, INFINITE ) )
  39. {
  40. hr = E_FAIL;
  41. }
  42. else
  43. {
  44. hr = m_hrRapi;
  45. }
  46. }
  47. return( hr );
  48. }
  49. void CDCCSink::FinalRelease()
  50. {
  51. if( m_hThread )
  52. {
  53. SAFE_CLOSEHANDLE( m_hThread );
  54. }
  55. if( m_hThreadEvent )
  56. {
  57. SAFE_CLOSEHANDLE( m_hThreadEvent );
  58. }
  59. }
  60. //
  61. // IDccManSink
  62. //
  63. STDMETHODIMP CDCCSink::OnLogActive ( void )
  64. {
  65. m_hrRapi = CeRapiInit();
  66. return( S_OK );
  67. }
  68. STDMETHODIMP CDCCSink::OnLogAnswered ( void )
  69. {
  70. return( S_OK );
  71. }
  72. STDMETHODIMP CDCCSink::OnLogDisconnection ( void )
  73. {
  74. DeviceDisconnected();
  75. return( S_OK );
  76. }
  77. STDMETHODIMP CDCCSink::OnLogError ( void )
  78. {
  79. DeviceDisconnected();
  80. return( S_OK );
  81. }
  82. STDMETHODIMP CDCCSink::OnLogInactive ( void )
  83. {
  84. DeviceDisconnected();
  85. return( S_OK );
  86. }
  87. STDMETHODIMP CDCCSink::OnLogIpAddr ( DWORD dwIpAddr )
  88. {
  89. #ifdef ATTEMPT_DEVICE_CONNECTION_NOTIFICATION
  90. if( !_Module.g_fInitialAttempt )
  91. {
  92. ACTIVESYNC_DEV_BROADCAST_USERDEFINED dbu;
  93. DWORD dwRecips = BSM_APPLICATIONS;
  94. memset( &dbu, 0, sizeof(dbu) );
  95. dbu.DBHeader.dbch_size = sizeof(dbu);
  96. dbu.fConnected = TRUE;
  97. strcpy( dbu.szName, g_szActiveSyncDeviceName );
  98. BroadcastSystemMessage( BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOHANG, &dwRecips, WM_DEVICECHANGE, DBT_USERDEFINED, (LPARAM)&dbu );
  99. }
  100. #endif
  101. _Module.g_fDeviceConnected = TRUE;
  102. return( S_OK );
  103. }
  104. STDMETHODIMP CDCCSink::OnLogListen ( void )
  105. {
  106. return( S_OK );
  107. }
  108. STDMETHODIMP CDCCSink::OnLogTerminated ( void )
  109. {
  110. DeviceDisconnected();
  111. return( S_OK );
  112. }
  113. //
  114. // Helper functions
  115. //
  116. void CDCCSink::DeviceDisconnected()
  117. {
  118. if( SUCCEEDED( m_hrRapi ) )
  119. {
  120. _Module.g_fDeviceConnected = FALSE;
  121. CeRapiUninit();
  122. m_hrRapi = E_FAIL;
  123. #ifdef ATTEMPT_DEVICE_CONNECTION_NOTIFICATION
  124. ACTIVESYNC_DEV_BROADCAST_USERDEFINED dbu;
  125. DWORD dwRecips = BSM_APPLICATIONS;
  126. memset( &dbu, 0, sizeof(dbu) );
  127. dbu.DBHeader.dbch_size = sizeof(dbu);
  128. dbu.fConnected = FALSE;
  129. strcpy( dbu.szName, g_szActiveSyncDeviceName );
  130. BroadcastSystemMessage( BSF_POSTMESSAGE | BSF_FORCEIFHUNG | BSF_NOHANG, &dwRecips, WM_DEVICECHANGE, DBT_USERDEFINED, (LPARAM)&dbu );
  131. #endif
  132. }
  133. }
  134. void CDCCSink::Shutdown()
  135. {
  136. if( m_hThread )
  137. {
  138. PostThreadMessage( m_dwThreadId, WM_QUIT, 0, 0 );
  139. WaitForSingleObject( m_hThread, INFINITE );
  140. }
  141. }
  142. DWORD WINAPI CDCCSink::CreateDCCManProc(LPVOID lpParam )
  143. {
  144. HRESULT hr = S_OK;
  145. MSG msg;
  146. _ASSERTE( lpParam );
  147. CDCCSink *pThis = (CDCCSink *)lpParam;
  148. CoInitialize(NULL);
  149. if( SUCCEEDED( hr ) )
  150. {
  151. hr = CoCreateInstance(CLSID_DccMan, NULL, CLSCTX_ALL, IID_IDccMan, (LPVOID *)&(pThis->m_spDccMan) );
  152. }
  153. if( SUCCEEDED( hr ) )
  154. {
  155. hr = pThis->m_spDccMan->Advise(pThis, &(pThis->m_dwDccSink) );
  156. }
  157. pThis->m_hrRapi = hr;
  158. SetEvent( pThis->m_hThreadEvent );
  159. while( GetMessage( &msg, NULL, 0, 0 ) )
  160. {
  161. DispatchMessage( &msg );
  162. }
  163. if( NULL != pThis->m_spDccMan.p )
  164. {
  165. if( (DWORD)-1 != pThis->m_dwDccSink )
  166. {
  167. pThis->m_spDccMan->Unadvise( pThis->m_dwDccSink );
  168. }
  169. }
  170. CoUninitialize();
  171. return( 0 );
  172. }