home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / bpcsusp.h < prev    next >
C/C++ Source or Header  |  1998-04-25  |  4KB  |  121 lines

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