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.

169 lines
4.0 KiB

  1. //=================================================================
  2. //
  3. // VolumeChange.cpp --
  4. //
  5. // Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  6. //
  7. //=================================================================
  8. #include "precomp.h"
  9. #include <dbt.h>
  10. #include "VolumeChange.h"
  11. //=================================================================
  12. //
  13. // CFactoryRouter
  14. //
  15. // provides for registration and instance creation
  16. //
  17. //
  18. //=================================================================
  19. // Implements a VolumeChangeProvider
  20. IUnknown * CVolumeChangeFactory::CreateInstance (
  21. REFIID a_riid ,
  22. LPVOID FAR *a_ppvObject
  23. )
  24. {
  25. return static_cast<IWbemProviderInit *>(new CVolumeChangeEvent) ;
  26. }
  27. //=================================================================
  28. //
  29. // CVolumeChangeEvent
  30. //
  31. // provides for eventing of power management events
  32. //
  33. //
  34. //=================================================================
  35. //
  36. // CWmiProviderInit needs the class name
  37. BSTR CVolumeChangeEvent::GetClassName()
  38. {
  39. return SysAllocString(VOLUME_CHANGE_EVENT);
  40. }
  41. // CWmiEventProvider signals us to begin providing for events
  42. void CVolumeChangeEvent::ProvideEvents()
  43. {
  44. if (!m_bRegistered)
  45. {
  46. m_bRegistered = TRUE;
  47. CWinMsgEvent::RegisterForMessage( WM_DEVICECHANGE ) ;
  48. }
  49. }
  50. // CWinMsgEvent signals that a message event has arrived
  51. void CVolumeChangeEvent::WinMsgEvent(
  52. IN HWND a_hWnd,
  53. IN UINT a_message,
  54. IN WPARAM a_wParam,
  55. IN LPARAM a_lParam,
  56. OUT E_ReturnAction &a_eRetAction,
  57. OUT LRESULT &a_lResult
  58. )
  59. {
  60. DEV_BROADCAST_HDR *pHdr = (DEV_BROADCAST_HDR *)a_lParam;
  61. if (
  62. (
  63. (a_wParam == DBT_DEVICEARRIVAL) ||
  64. (a_wParam == DBT_DEVICEREMOVECOMPLETE)
  65. ) &&
  66. (pHdr->dbch_devicetype == DBT_DEVTYP_VOLUME)
  67. )
  68. {
  69. HandleEvent( a_wParam, (DEV_BROADCAST_VOLUME *) pHdr ) ;
  70. }
  71. }
  72. void CVolumeChangeEvent::HandleEvent( WPARAM wParam, DEV_BROADCAST_VOLUME *pVol )
  73. {
  74. HRESULT hr = S_OK; // Note that this result is NOT sent back from this function
  75. // since I don't have any place to send it TO.
  76. if ( ( pVol->dbcv_flags == 0 ) || ( pVol->dbcv_flags & DBTF_MEDIA ) )
  77. {
  78. IWbemObjectSinkPtr t_pHandler(CEventProvider::GetHandler(), false);
  79. IWbemClassObjectPtr t_pClass(CEventProvider::GetClass(), false);
  80. if( t_pClass != NULL && t_pHandler != NULL )
  81. {
  82. IWbemClassObjectPtr t_pInst;
  83. if( SUCCEEDED( hr = t_pClass->SpawnInstance( 0L, &t_pInst ) ) )
  84. {
  85. DWORD dwUnitMask = pVol->dbcv_unitmask;
  86. for (DWORD i = 0; i < 26; ++i)
  87. {
  88. if (dwUnitMask & 0x1)
  89. {
  90. WCHAR l[3];
  91. l[0] = i + L'A';
  92. l[1] = L':';
  93. l[2] = L'\0';
  94. variant_t vValue(l);
  95. variant_t vEventType;
  96. switch (wParam)
  97. {
  98. case DBT_DEVICEARRIVAL:
  99. {
  100. vEventType = (long)2;
  101. break;
  102. }
  103. case DBT_DEVICEREMOVECOMPLETE:
  104. {
  105. vEventType = (long)3;
  106. break;
  107. }
  108. default:
  109. {
  110. hr = WBEM_E_FAILED;
  111. break;
  112. }
  113. }
  114. if ( SUCCEEDED(hr) &&
  115. SUCCEEDED( hr = t_pInst->Put( L"DriveName", 0, &vValue, 0 ) ) &&
  116. SUCCEEDED( hr = t_pInst->Put( L"EventType", 0, &vEventType, 0 ) )
  117. )
  118. {
  119. // We can't use t_pInst here, cuz the operator(cast) for this smartptr
  120. // will FREE the pointer before passing it in, under the assumption
  121. // that Indicate is going to POPULATE this pointer.
  122. IWbemClassObject *p2 = t_pInst;
  123. hr = t_pHandler->Indicate ( 1, &p2 ) ;
  124. }
  125. }
  126. dwUnitMask = dwUnitMask >> 1;
  127. }
  128. }
  129. }
  130. }
  131. }
  132. //
  133. void CVolumeChangeEvent::OnFinalRelease()
  134. {
  135. if (m_bRegistered)
  136. {
  137. CWinMsgEvent::UnRegisterMessage( WM_DEVICECHANGE ) ;
  138. }
  139. delete this;
  140. }