home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / bpcsusp.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  4.6 KB  |  124 lines

  1. #pragma option push -b -a8 -pc -A- /*P_O_Push*/
  2. //// bpcsuspend.h - header file for interface that allows external apps
  3. //       to request that the bpc video server release all of its devices and
  4. //       shutdown its directshow graph.  
  5.  
  6. // USAGE:
  7. // in order to request that the bpc subsystem release its devices
  8. // create an instance of the CBPCSuspend class
  9. // to check if this succeeded use the IsBPCSuspended member function.  if IsBPCSuspended returns false 
  10. // then that means that there are active bpc video clients and you must treat this as you would a
  11. // device busy or device open type of failure.
  12. // when you are done with the devices destroy the CBPCSuspend class and this will notify vidsvr
  13. // that it can resume using the devices and return to background data capturing
  14. //
  15. // NOTE: you must compile vidsvr.odl and include the resulting .h before including this file
  16. // NOTE: you must have initialized com prior on this thread prior to using this object.
  17. // 
  18. // CLSID_BPCSuspend comes from the header file generated from compiling vidsvr.odl
  19. // IBPCSuspended comes from the header file generated from compiling vidsvr.odl
  20.  
  21. // theory of operation:
  22. // by using GetActiveObject instead of CoCreateInstance we don't force vidsvr to be loaded just to find
  23. // out that it wasn't running in the first place.
  24. // by returning an object that must be released to free the devices so that vidsvr can continue background
  25. // data capture we utilize COM to manage this resource.  this means that if the external app that requested
  26. // the devices crashes or leaks then the suspension object will be automatically released and 
  27. // vidsvr can resume using the devices without requiring a system reboot or some other unfriendly intervention.
  28.  
  29. #ifndef _MSBPCVideo_H_
  30. #error you must include the .h generated from compiling vidsvr.odl before including this file
  31. #endif
  32.  
  33. #ifndef BPCSUSP_H
  34. #define BPCSUSP_H
  35. #pragma once
  36.  
  37. #include <oleauto.h>
  38.  
  39. #ifdef _CPPUNWIND
  40. #pragma message("bpcsusp.h using exceptions")
  41. #define BPCTRY try {
  42. #ifdef _DEBUG
  43. #define BPCCATCH } catch(...) { OutputDebugString("CBPCSuspend exception\r\n");}
  44. #else
  45. #define BPCCATCH } catch(...) {}
  46. #endif
  47. #define BPCNOTHROW throw()    
  48. #else
  49. #define BPCTRY
  50. #define BPCCATCH
  51. #define BPCNOTHROW
  52. #endif
  53.  
  54. class CBPCSuspend {
  55.     IDispatch* m_pSuspended;
  56.     bool m_fBPCExists;
  57. public:
  58.    inline CBPCSuspend() BPCNOTHROW : m_pSuspended(NULL), m_fBPCExists(false) {
  59.    BPCTRY
  60. #ifdef _DEBUG
  61.         OutputDebugString("CBPCSuspend()::CBPCSuspend()\r\n");
  62.         TCHAR msgtemp[256];
  63. #endif
  64.         IUnknown *pUnkSuspendor = NULL;
  65.         DWORD dwReserved;
  66.         HRESULT hr = GetActiveObject(CLSID_BPCSuspend, &dwReserved, &pUnkSuspendor);
  67.         if (SUCCEEDED(hr)  && pUnkSuspendor) {
  68.             IBPCSuspend *pSuspendor = NULL;
  69.             hr = pUnkSuspendor->QueryInterface(IID_IBPCSuspend, reinterpret_cast<void **>(&pSuspendor));
  70.             pUnkSuspendor->Release();
  71.             if (SUCCEEDED(hr) && pSuspendor) {
  72.  
  73. #ifdef _DEBUG
  74.                 OutputDebugString("CBPCSuspend()::CBPCSuspend() BPC exists\r\n");
  75. #endif
  76.                 m_fBPCExists = true;
  77.                 hr = pSuspendor->DeviceRelease(0L, &m_pSuspended);
  78.                 if (FAILED(hr) || !m_pSuspended) {
  79. #ifdef _DEBUG
  80.                     wsprintf(msgtemp, "CBPCSuspend()::CBPCSuspend() Suspendor->DeviceRelease() rc = %lx\r\n", hr);
  81.                     OutputDebugString(msgtemp);
  82. #endif
  83.                     ASSERT(!m_pSuspended);
  84.                 }
  85. #ifdef _DEBUG
  86.                 else {
  87.                     wsprintf(msgtemp, "CBPCSuspend()::CBPCSuspend() BPC video server suspended\r\n");
  88.                     OutputDebugString(msgtemp);
  89.                 }
  90. #endif
  91.                 pSuspendor->Release();
  92.             }
  93.  
  94.  
  95.         } else {
  96. #ifdef _DEBUG
  97.             wsprintf(msgtemp, "CBPCSuspend()::CBPCSuspend() GetActiveObject() rc = %lx\r\n", hr);
  98.             OutputDebugString(msgtemp);
  99. #endif
  100.         }
  101.    BPCCATCH
  102.    }
  103.    inline ~CBPCSuspend() BPCNOTHROW {
  104.        BPCTRY 
  105.            if (m_fBPCExists && m_pSuspended) {
  106.                m_pSuspended->Release();
  107.                m_pSuspended = NULL;
  108.            }
  109.        BPCCATCH
  110.    }
  111.    inline bool IsBPCSuspended() BPCNOTHROW {
  112.        // if m_fBPCExists but we weren't able to retrieve a suspension object then
  113.        // there are active video clients and you must treat this as a device busy/failed to open type error
  114.        if (m_fBPCExists && !m_pSuspended) {
  115.            return false;
  116.        }
  117.        return true;
  118.    }
  119. };
  120.  
  121. #endif
  122. // end of file - bpcsusp.h
  123. #pragma option pop /*P_O_Pop*/
  124.