asyncflt.cpp
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:4k
源码类别:

VC书籍

开发平台:

Visual C++

  1. //------------------------------------------------------------------------------
  2. // File: AsyncFlt.cpp
  3. //
  4. // Desc: DirectShow sample code - implementation of CAsyncFilter.
  5. //
  6. // Copyright (c) Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8. #include "stdafx.h"
  9. #include <streams.h>
  10. #include "asyncio.h"
  11. #include "asyncrdr.h"
  12. #pragma warning(disable:4710)  // 'function' not inlined (optimization)
  13. #include "asyncflt.h"
  14. //
  15. // Setup data for filter registration
  16. //
  17. const AMOVIESETUP_MEDIATYPE sudOpPinTypes =
  18. { &MEDIATYPE_Stream     // clsMajorType
  19. , &MEDIASUBTYPE_NULL }; // clsMinorType
  20. const AMOVIESETUP_PIN sudOpPin =
  21. { L"Output"          // strName
  22. , FALSE              // bRendered
  23. , TRUE               // bOutput
  24. , FALSE              // bZero
  25. , FALSE              // bMany
  26. , &CLSID_NULL        // clsConnectsToFilter
  27. , L"Input"           // strConnectsToPin
  28. , 1                  // nTypes
  29. , &sudOpPinTypes };  // lpTypes
  30. const AMOVIESETUP_FILTER sudAsync =
  31. { &CLSID_AsyncSample              // clsID
  32. , L"Sample File Source (Async.)"  // strName
  33. , MERIT_UNLIKELY                  // dwMerit
  34. , 1                               // nPins
  35. , &sudOpPin };                    // lpPin
  36. //
  37. //  Object creation template
  38. //
  39. CFactoryTemplate g_Templates[1] = {
  40.     { L"Sample File Source (Async.)"
  41.     , &CLSID_AsyncSample
  42.     , CAsyncFilter::CreateInstance
  43.     , NULL
  44.     , &sudAsync }
  45. };
  46. int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);
  47. ////////////////////////////////////////////////////////////////////////
  48. //
  49. // Exported entry points for registration and unregistration 
  50. // (in this case they only call through to default implementations).
  51. //
  52. ////////////////////////////////////////////////////////////////////////
  53. STDAPI DllRegisterServer()
  54. {
  55.     return AMovieDllRegisterServer2(TRUE);
  56. }
  57. STDAPI DllUnregisterServer()
  58. {
  59.     return AMovieDllRegisterServer2(FALSE);
  60. }
  61. //
  62. // DllEntryPoint
  63. //
  64. extern "C" BOOL WINAPI DllEntryPoint(HINSTANCE, ULONG, LPVOID);
  65. BOOL APIENTRY DllMain(HANDLE hModule, 
  66.                       DWORD  dwReason, 
  67.                       LPVOID lpReserved)
  68. {
  69. return DllEntryPoint((HINSTANCE)(hModule), dwReason, lpReserved);
  70. }
  71. //* Create a new instance of this class
  72. CUnknown * WINAPI CAsyncFilter::CreateInstance(LPUNKNOWN pUnk, HRESULT *phr)
  73. {
  74.     ASSERT(phr);
  75.     //  DLLEntry does the right thing with the return code and
  76.     //  the returned value on failure
  77.     return new CAsyncFilter(pUnk, phr);
  78. }
  79. BOOL CAsyncFilter::ReadTheFile(LPCTSTR lpszFileName)
  80. {
  81.     DWORD dwBytesRead;
  82.     // Open the requested file
  83.     HANDLE hFile = CreateFile(lpszFileName,
  84.                               GENERIC_READ,
  85.                               FILE_SHARE_READ,
  86.                               NULL,
  87.                               OPEN_EXISTING,
  88.                               0,
  89.                               NULL);
  90.     if (hFile == INVALID_HANDLE_VALUE) 
  91.     {
  92.         DbgLog((LOG_TRACE, 2, TEXT("Could not open %sn"), lpszFileName));
  93.         return FALSE;
  94.     }
  95.     // Determine the file size
  96.     ULARGE_INTEGER uliSize;
  97.     uliSize.LowPart = GetFileSize(hFile, &uliSize.HighPart);
  98.     PBYTE pbMem = new BYTE[uliSize.LowPart];
  99.     if (pbMem == NULL) 
  100.     {
  101.         CloseHandle(hFile);
  102.         return FALSE;
  103.     }
  104.     // Read the data from the file
  105.     if (!ReadFile(hFile,
  106.                   (LPVOID) pbMem,
  107.                   uliSize.LowPart,
  108.                   &dwBytesRead,
  109.                   NULL) ||
  110.         (dwBytesRead != uliSize.LowPart))
  111.     {
  112.         DbgLog((LOG_TRACE, 1, TEXT("Could not read filen")));
  113.         delete [] pbMem;
  114.         CloseHandle(hFile);
  115.         return FALSE;
  116.     }
  117.     // Save a pointer to the data that was read from the file
  118.     m_pbData = pbMem;
  119.     m_llSize = (LONGLONG)uliSize.QuadPart;
  120.     // Close the file
  121.     CloseHandle(hFile);
  122.     return TRUE;
  123. }