Click here to Skip to main content
15,879,239 members
Articles / Multimedia / DirectX
Tip/Trick

WDM / VFW ...my definitive solution...

Rate me:
Please Sign up or sign in to vote.
4.68/5 (5 votes)
20 Aug 2013CPOL2 min read 15.3K   1.1K   10   2
A class to handle WDM (Windows Driver Mode).

Introduction   

When I was trying to implement video preview in my programs, I have been found some documentation an example to use VFW (Video For Windows) tecnology and it seems to be more simple than DirectShow and taking some code from here and from there, I wrote my C++ VFW class. But on some Windows version combined with some camera drivers, often, it shows the source selection window instead the preview window and it was impossible to use it. So.... I have decided to start writing another class to handle WDM (Windows Driver Mode).

Finally, I have written two classes, one for VFW and one for WDM. For now this is a basic approach, this code implements only Video Preview (...for now). For writing this classes I used MSDN help and Your help too, so, I hope it can be usefull for who is starting using Video Capture (like me...). Everyone can add features to this class, like video analizing, capturing and so on.

I develope my Windows programs with Builder C++ XE2 compiler but these class are written using the most compatibility win32 api, so, they can be used with any compilers (I hope it).

The peculiarity of this code is that the public functions of both classes are the same, so, You can write only one program to handle VFW of WDM class. 

I have commented the code in Doxygen mode, compiled and added it to the downloadable file, so You can open the index.html page in html folder to consult classes documentation. 

Using the code:

1. Include the header file:   
C++
#include "VfwVideoCap.h"  

or

C++
#include "WdmVideoCap.h" 
2. Declare a pointer to the class ( I suggest You to declare it global)
C++
DVfwCap *cap; // for vfw class

 or  

C++
DWdmCap *cap; // for wdm class

3. Create a class instance, you must specify the handle of the window on which You want to see the preview

C++
cap=new DVfwCap(hWnd); // for vfw class

 or   

C++
cap=new DWdmCap(hWnd); // for wdm class 

4. From here You can use the same calls to handle the preview window

C++
// Create the capture window connected to the first camera in the system (if any)
// starting from 0,0 (relative to the window) and 320x240 size in
// the window passed in constructor 
cap->CreateCapWindow(0,        // device index (0 is the first)
                     0,        // X start
                     0,        // Y start
                     320,      // Width
                     240 );    // Height// Start previewing
cap->StartPreview();  

...That's all, it's easy 

You can also handle the previewing using:
C++
cap->StopPreview(); // To stop previewing
cap->PausePreview() // To pause the preview
cap->SetCapSize(0,0,640,480); // To change the preview size 

before exit program don't forget to destroy the class instance

C++
delete cap; 

List of funcions that can be used in both classes:

int EnumerateVideoCaptureDevices(void);
int EnumerateVideoCompressorDevices(void);
char* GetVideoCaptureDeviceName(unsigned int DeviceId);
char* GetVideoCompressorDeviceName(unsigned int DeviceId);
HRESULT CreateCapWindow(HWND hwndParent, int iDeviceID, int X, int Y, int iWidth, int iHeight);
HRESULT CreateCapWindow(int iDeviceID, int X, int Y, int Width, int Height);
HRESULT CreateCapWindow(void);
HRESULT SetCapSize(int X, int Y, int Width, int Height);
HRESULT SetLowRes(void);
HRESULT SetMidRes(void);
HRESULT SetHiRes(void);
bool StartPreview(void);
bool StopPreview(void);
bool PausePreview(void);
void ShowPreviewWindow(void);
void HidePreviewWindow(void); 

See the class documenation for using its.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi20-Aug-13 6:18
professionalAmir Mohammad Nasrollahi20-Aug-13 6:18 
GeneralRe: My vote of 4 Pin
Fabio Durigon20-Aug-13 7:15
Fabio Durigon20-Aug-13 7:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.