为什么主函数执行完以后,没有继续执行初始化了?在函数初始化的时候加断点,没有执行
请多多指教哦#include <windows.h>
#include <stdio.h>
#include <Dshow.h>
#include <atlbase.h>
#include <iostream.h>
#pragma comment(lib, "strmbasd.lib")
//class ISampleCaptureGraphBuilder;
//class Enumerator;
enum PLAYSTATE {STOPPED, PAUSED, RUNNING, INIT};
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// An application can advertise the existence of its filter graph
// by registering the graph with a global Running Object Table (ROT).
// The GraphEdit application can detect and remotely view the running
// filter graph, allowing you to 'spy' on the graph with GraphEdit.
//
// To enable registration in this sample, define REGISTER_FILTERGRAPH.
//
//------------------------------------------------------------------------------
// Global data
//------------------------------------------------------------------------------
//HWND hwnd;
struct stuff
{
UINT chSVideo;
UINT chCompVideo;
UINT chWebCam;
UINT chFullScreen;
UINT chAlwaysOnTop;
int nVSourceCount;
int nAnalogCount;
//CBrush m_emptyBrush;
DWORD m_dwGraphRegister;
BOOL bDevCheck;
BOOL pDevEnum;
BOOL pClassEnum;
//HWND hwnd;
HWND m_hApp;
//------------------Video--------------------//
IVideoWindow* m_pVW;
IMediaControl* m_pMC;
IMediaEventEx* m_pME;
IGraphBuilder* m_pGraph;
ICaptureGraphBuilder2* m_pCapture;
IBaseFilter* pSrcF;
PLAYSTATE m_psCurrent;
IMoniker* pMoniker;
//----------------For Webcam-----------------//
IVideoWindow* m_pVWMoniker;
IMediaControl* m_pMCMoniker;
IMediaEventEx* m_pMEMoniker;
ICaptureGraphBuilder2* m_pCaptureMoniker;
IFilterGraph2* m_pFilterGraph;// IFilterGraph2 provides AddSourceFileForMoniker()
BOOL bVideo;
BOOL WebCam;
int vType;
}grap;
class CCaptureVideo
{
public:
CCaptureVideo();
~CCaptureVideo();
HRESULT GetInterfacesMoniker();
HRESULT AddCaptureMonikerToGraph(IMoniker* pMoniker);
HRESULT FindCaptureDeviceMoniker(IMoniker** ppMoniker);
HRESULT SetupVideoWindowMoniker();
HRESULT CaptureVideoByMoniker();
// afx_msg HRESULT OnGraphNotify(WPARAM wp , LPARAM lp);
void DisplayMesg(TCHAR* szFormat, ...);
void StartWebcam();
void StopCapture();
void ResizeVideoWindowMoniker();
void ResizeVideoWindow();
HRESULT InitializeVideo(HWND hWnd);
};
//利用消息通知
#define WM_GRAPHNOTIFY WM_USER+1//private message
#ifndef SAFE_RELEASE
#define SAFE_RELEASE( x ) \
if ( NULL != x ) \
{ \
x->Release( ); \
x = NULL; \
}
#endif
//void UnIntializeVideo();
// HRESULT FindCaptureDevice(IBaseFilter** ppSrcFilter);
//HRESULT SetupVideoWindow();
//HRESULT ChangePreviewState(int nShow);
//HRESULT HandleGraphEvent();
//HRESULT CaptureVideo();
//void CloseInterfaces();
// void CloseInterfacesMoniker();
HRESULT GetInterfacesMoniker();
HRESULT AddCaptureMonikerToGraph(IMoniker* pMoniker);
HRESULT FindCaptureDeviceMoniker(IMoniker** ppMoniker);
HRESULT SetupVideoWindowMoniker();
HRESULT CaptureVideoByMoniker();
// afx_msg HRESULT OnGraphNotify(WPARAM wp , LPARAM lp);
void DisplayMesg(TCHAR* szFormat, ...);
void StartWebcam();
void StopCapture();
void ResizeVideoWindowMoniker();
void ResizeVideoWindow();
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
HRESULT CCaptureVideo::InitializeVideo(HWND hWnd)
{
HRESULT hr = NULL ;
grap.m_hApp = hWnd ;
grap.bDevCheck = FALSE;
grap.nVSourceCount =0;
grap.vType = 0;
//COM Library Intialization
if(FAILED(CoInitialize(NULL))) /*, COINIT_APARTMENTTHREADED)))*/
{
MessageBox(NULL,TEXT("CoInitialize Failed!\r\n"),TEXT("The amcap"),NULL);
return hr ;
}
return hr ;
}
CCaptureVideo::CCaptureVideo()
{
// Initialization
grap.m_hApp=NULL;
grap.m_dwGraphRegister=0;
grap.nAnalogCount =0; // Counting Analog devices
grap.m_pVW = NULL;
grap.m_pMC = NULL;
grap.m_pME = NULL;
grap.m_pGraph = NULL; // IFilterGraph2 provides AddSourceFileForMoniker()
grap.m_pCapture = NULL;
grap.pSrcF = NULL ;
/// For Webcam
grap.m_pVWMoniker = NULL;
grap.m_pMCMoniker = NULL;
grap.m_pMEMoniker = NULL;
grap.m_pCaptureMoniker = NULL;
grap.m_pFilterGraph = NULL;
}
CCaptureVideo::~CCaptureVideo()
{
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
grap.m_hApp=hwnd = CreateWindow( szAppName, // window class name
TEXT ("The amcap"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
/* CCaptureVideo1.InitializeVideo();*/
return 0 ;
case WM_PAINT:
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
case WM_GRAPHNOTIFY:
//HandlegraphEvent();
break;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
HRESULT GetInterfacesMoniker()
{
HRESULT hr;
if(grap.m_pFilterGraph == NULL)
{
// Create the filter graph manager
hr = CoCreateInstance (CLSID_FilterGraph, NULL, CLSCTX_INPROC,
IID_IFilterGraph2, (void **) &grap.m_pFilterGraph);
if (FAILED(hr))
return hr;
}
else
{
return E_FAIL ;
}
if(grap.m_pCaptureMoniker == NULL)
{
// Create the capture graph builder
hr = CoCreateInstance (CLSID_CaptureGraphBuilder2 , NULL, CLSCTX_INPROC,
IID_ICaptureGraphBuilder2, (void **) &grap.m_pCaptureMoniker);
if (FAILED(hr))
return hr;
}
else
return E_FAIL ;
// Obtain interfaces for media control and Video Window 查询接口
////查询某个组件是否支持某个特定的接口 接口的IID
hr = grap.m_pFilterGraph->QueryInterface(IID_IMediaControl,(LPVOID *) &grap.m_pMCMoniker);
if (FAILED(hr))
return hr;
hr = grap.m_pFilterGraph->QueryInterface(IID_IVideoWindow, (LPVOID *) &grap.m_pVWMoniker);
if (FAILED(hr))
return hr;
hr = grap.m_pFilterGraph->QueryInterface(IID_IMediaEvent, (LPVOID *) &grap.m_pMEMoniker);
if (FAILED(hr))
return hr;
// Set the window handle used to process graph events
hr = grap.m_pMEMoniker->SetNotifyWindow((OAHWND)grap.m_hApp, WM_GRAPHNOTIFY, 0);
return hr;
}
HRESULT FindCaptureDeviceMoniker(IMoniker **ppMoniker)
{
HRESULT hr;
ULONG cFetched;
if (!ppMoniker)
return E_POINTER;
// Create the system device enumerator
CComPtr <ICreateDevEnum> pDevEnum = NULL;//定义设备系统枚举接口
//创建枚举器接口
hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
IID_ICreateDevEnum, (void ** ) &pDevEnum);
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Couldn't create system enumerator!hr=0x%x"),TEXT("The amcap"),NULL);
return hr;
}
// Create an enumerator for the video capture devices
CComPtr <IEnumMoniker> pClassEnum = NULL;
//获取一种类的枚举类型
hr = pDevEnum->CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &pClassEnum, 0);
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Couldn't create class enumerator!hr=0x%x"),TEXT("The amcap"),NULL);
return hr;
}
// If there are no enumerators for the requested type, then
// CreateClassEnumerator will succeed, but pClassEnum will be NULL.
if (pClassEnum == NULL)
{
MessageBox(NULL,TEXT("No video capture device was detected."),/*MB_OK | MB_ICONINFORMATION*/TEXT("The amcap"),NULL) ;
return E_FAIL;
}
// Use the first video capture device on the device list.
// Note that if the Next() call succeeds but there are no monikers,
// it will return S_FALSE (which is not a failure). Therefore, we
// check that the return code is S_OK instead of using SUCCEEDED() macro.
/// For finding Digital Capture Devices ...
BOOL Found ;
IPin *pP = 0;
IEnumPins *pins=0;
ULONG n;
PIN_INFO pinInfo;
Found = FALSE;
IKsPropertySet *pKs=0;
GUID guid;
DWORD dw;
BOOL fMatch = FALSE;
IMoniker *pMoniker = NULL;
IBaseFilter *pSrc;
//枚举每一个moniker
while (S_OK == (pClassEnum->Next (1, ppMoniker, &cFetched)))
{
pMoniker = ppMoniker[0];
//生成绑定到设备的filter
hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&pSrc);
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Couldn't bind moniker to filter object! hr=0x%x"),TEXT("The amcap"), NULL);
return hr;
}
else
grap.pSrcF = pSrc;
if(SUCCEEDED(pSrc->EnumPins(&pins)))
{
while(!Found && (S_OK == pins->Next(1, &pP, &n)))
{//获得关于pin的信息
if(S_OK == pP->QueryPinInfo(&pinInfo))
{
if(pinInfo.dir == PINDIR_INPUT)
{
// is this pin an ANALOGVIDEOIN input pin?
if(pP->QueryInterface(IID_IKsPropertySet,//查询接口
(void **)&pKs) == S_OK)
{
if(pKs->Get(AMPROPSETID_Pin,
AMPROPERTY_PIN_CATEGORY, NULL, 0,
&guid, sizeof(GUID), &dw) == S_OK)
{
if(guid == PIN_CATEGORY_ANALOGVIDEOIN)
fMatch = TRUE;
}
pKs->Release();
}
if(fMatch)
{
Found = TRUE;
grap.bDevCheck = Found;
grap.nAnalogCount++;
break;
}
}
pinInfo.pFilter->Release();
}
pP->Release();
}
pins->Release();
}
grap.nVSourceCount++;
} // End of While Loop
return S_OK;
}
HRESULT CaptureVideoByMoniker()
{
HRESULT hr;
IMoniker *pMoniker =NULL;
// Get DirectShow interfaces
hr = GetInterfacesMoniker();
if (FAILED(hr))
{
//Monikor Message
MessageBox(NULL,TEXT("Failed to get video interfaces!hr=0x%x"),TEXT("The amcap"),NULL);
return hr;
}
// Use the system device enumerator and class enumerator to find
// a moniker that represents a video capture/preview device,
// such as a desktop USB video camera.
hr = FindCaptureDeviceMoniker(&pMoniker);
if (FAILED(hr))
{
//AfxMessageBox("Webcam is not detected.!") ;
return hr;
}
if( grap.nAnalogCount == grap.nVSourceCount )
{
MessageBox(NULL,TEXT("Webcam(USB) is not detected.!"),TEXT("The amcap"),NULL);
return hr;
}
hr = AddCaptureMonikerToGraph(pMoniker);
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Couldn't add the capture filter to the graph! hr=0x%x\r\n\r\n")
TEXT("If you have a working video capture device, please make sure\r\n")
TEXT("that it is connected and is not being used by another application.\r\n\r\n")
TEXT("The sample will now close."),TEXT("The amcap"),NULL);
return hr;
}
// Release the IMoniker interface for the capture source filter
pMoniker->Release();
// Set video window style and position
hr = SetupVideoWindowMoniker();
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Couldn't initialize video window!hr=0x%x"),TEXT("The amcap"),NULL);
return hr;
}
#ifdef REGISTER_FILTERGRAPH
// Add our graph to the running object table, which will allow
// the GraphEdit application to "spy" on our graph
hr = AddGraphToRotMoniker(m_pFilterGraph, &m_dwGraphRegister);
if (FAILED(hr))
{
DisplayMesg(TEXT("Failed to register filter graph with ROT! hr=0x%x"), hr);
m_dwGraphRegister = 0;
}
#endif
// Start previewing video data
hr = grap.m_pMCMoniker->Run();//只接收不显示
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Couldn't run the graph! hr=0x%x"),TEXT("The amcap"),NULL);
return hr;
}
return S_OK;
}
HRESULT AddCaptureMonikerToGraph(IMoniker *pMoniker)
{
USES_CONVERSION;
HRESULT hr;
IBaseFilter *pBaseFilter=0;
// Get the display name of the moniker
LPOLESTR strMonikerName=0;
hr = pMoniker->GetDisplayName(NULL, NULL, &strMonikerName);
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Couldn't get moniker's display name!hr=0x%x"),TEXT("The amcap"),NULL);
return hr;
}
#ifdef DEBUG
// Get a human-readable string for evaluation during debugging
TCHAR szMonikerName[256];
_tcsncpy(szMonikerName, W2T(strMonikerName), 255);
szMonikerName[255] = 0; // Null-terminate
//DisplayMesg(TEXT("Moniker: %s\r\n"), szMonikerName);
#endif
// Create a bind context needed for working with the moniker
IBindCtx *pContext=0;
hr = CreateBindCtx(0, &pContext);
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Couldn't create a bind context for moniker! hr=0x%x"),TEXT("The amcap"), NULL);
return hr;
}
hr = grap.m_pFilterGraph->AddSourceFilterForMoniker(pMoniker, pContext,
strMonikerName, &pBaseFilter);
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Failed in AddSourceFilterForMoniker()! hr=0x%x"),TEXT("The amcap"), NULL);
return hr;
}
// Attach the filter graph to the capture graph
hr = grap.m_pCaptureMoniker->SetFiltergraph(grap.m_pFilterGraph);
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Failed to set capture filter graph! hr=0x%x"),TEXT("The amcap"),NULL);
return hr;
}
// Render the preview pin on the video capture filter
// Use this instead of m_pGraph->RenderFile
hr = grap.m_pCaptureMoniker->RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
pBaseFilter, NULL, NULL);
if (FAILED(hr))
{
MessageBox(NULL,TEXT("Couldn't render capture stream. ")
TEXT("The device may already be in use.\r\n\r\nhr=0x%x"),TEXT("The amcap"),NULL);
return hr ;
}
SAFE_RELEASE(pContext);
SAFE_RELEASE(pBaseFilter);
return hr;
}
HRESULT SetupVideoWindowMoniker()
{
HRESULT hr;
// Set the video window to be a child of the main window
hr = grap.m_pVWMoniker->put_Owner((OAHWND)grap.m_hApp);
if (FAILED(hr))
return hr;
// Set video window style
hr = grap.m_pVWMoniker->put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);
if (FAILED(hr))
return hr;
// ResizeVideoWindowMoniker();
// Make the video window visible, now that it is properly positioned
hr = grap.m_pVWMoniker->put_Visible(OATRUE);
if (FAILED(hr))
return hr;
hr = grap.m_pVWMoniker->put_MessageDrain((OAHWND)grap.m_hApp);
return hr;
}
void StartWebcam()
{
// Initializing Interfaces and Capture using WebCam
HRESULT hr = CaptureVideoByMoniker();
if(FAILED(hr))
{
MessageBox(NULL,TEXT("No Webcam Found.!"),TEXT("The amcap"),NULL);
return;
}
}