Click here to Skip to main content
13,293,894 members (60,513 online)
Click here to Skip to main content
Add your own
alternative version


142 bookmarked
Posted 23 Apr 2002

Webcam Web Service

, 23 Apr 2002
Rate this:
Please Sign up or sign in to vote.
C++ ATL Web Service delivering a JPEG picture grabbed from a Webcam to an ActiveX client
<!-- Download Links -->



Webcam is a Simple Object Access Protocol (SOAP) project that will permit client's software to get, from a Web Service, JPEG pictures captured by a Webcam. It is my first step experiment in the world of SOAP after several projects using COM/DCOM and ATL.

The project is split in three parts:

  • Server: Active Template Library Server capturing a picture from the Webcam.
  • Web Service: Generated with WSDL Generator from Microsoft®.
  • Client: ActiveX client connecting to Webcam SOAP Server getting pictures and displaying them.


We use Visual C++ and the Active Template Library (ATL) object Wizard to create a Simple COM object, called Camera. It handles the grabbing of the picture and the JPEG compression.

The jpeg compression is achieved with Intel® JPEG Library v1.5. Get it here. You may use other libraries, like IJG JPEG LIBRARY, by using the abstract base class CPicturePacker.

Camera handling is done using Microsoft® Video for Windows® (VFW). Get more information on MSDN / Platform SDK Documentation / Graphics and Multimedia Services / Windows Multimedia / Video Capture.

Trick: VFW needs a handler on a window to attach the camera driver to. As we chose to create a Simple COM object we do not have access to any window handler. Calling GetDesktopWindow() got us one.

// Don't have access to the container's window so just use the desktop.
// RMK: If the desktop is not at least 24bits colors, then the grab will
//      fail
HWND hWnd = ::GetDesktopWindow();

if ( hWnd )
{ ...
All functionalities needed to grab a picture on a Webcam are done in the class CWebCam. For example the method GrabFrame:
bool CWebCam::GrabFrame( CPicturePacker * pPacker,
                         unsigned char  ** ppPackedPicture,
                         unsigned long  * pnPackedPictureBytes )
Use a reference to a CPicturePacker base class to pack the original picture's bits, permitting to extend the packing to whatever format you want, as mentioned before. In this sample I have written the class CIntelJpegPacker inheriting from CPicturePacker and using the Intel® JPEG Library v1.5 to pack the picture grabbed. You may consider using GDI+ for example to do the same.

The rest of the source is dealing with COM and is nothing special.

Server Interface

Our server needs to achieve two simple operations: grabbing a picture from a Webcam and compressing it as a JPEG picture according to a compression ratio defined by the caller. This is achieved in the GrabFrame method.

HRESULT GrabFrame( [in] short nQuality, 
                   [out, retval] SAFEARRAY(unsigned char) * ppGrabbedPicture );
The input parameter 'nQuality' represents the jpeg compression ratio, from 1 to 99. A value of one meaning lowest quality (highest compression) and a value of ninety-nine meaning highest quality (lowest compression).

As an output return value the client get the jpeg picture in a SAFEARRAY of
. We use this data type because it is fully supported by SOAP (See faced problem 1 and 2).

Our COM object supports the interface IErrorInfo as indicated by the ISupportErrorInfo interface. It permits sending back to the client information about possible issues encountered by the server. The good point is that it is fully automatic for us. Read more about that point in Microsoft SOAP User Guide: "Understanding the SOAP Fault <detail> Contents".

Before writing your interfaces check out the different types supported by SOAP in "Data Type Mappings" and there equivalence in programming languages. You find them in the Microsoft SOAP User Guide.

Web Service

We simply use Microsoft WSDL Generator to wrap our COM object, Camera, into a SOAP Web Service.

On the welcome page click Next.

On the dialog "Select the COM .dll to analyze":

Enter the name of your Web Service, i.e. webcam. 

Browse to select your dll.

Click Next.

On the dialog "Select the services you would like to expose":

Expand the list and check the GrabFrame method.

Click Next.


On the dialog "SOAP listener information":

In the URI text box, type the url of your webservice, i.e. http://localhost/webservices/webcam.

Choose ISAPI Listener type.

Then select 2001 as XSD Schema Namespace.

Click Next.

On the dialog "Specify the location for the new WSDL and WSML files":

Select UTF-8 as character set.

Then select the place you want to store the new files, i.e. c:\inetpub\WebServices\webcam.

Click Next.

Click Finish.


Now we have our Web Service! You must also define a Virtual Directory called WebServices in IIS.
In the case you want to change the location of the Web Service you need to change the WSDL file generated by Microsoft WSDL Generator.


We use Visual C++ and Active Template Library (ATL) object Wizard to create an ActiveX, called Webcam. This ActiveX is the client part of the Web Service Webcam. It is connecting to the Webcam Web Service, receiving back a jpeg picture and displaying it.

To be able to compile the project you must have installed SOAP Toolkit on your computer. Find it here. For the client you must also have WTL installed. If It is not the case donwload it here.

Client Interface

To be able to grab a picture on the Webcam Web Service, we need to specify the location of the Web Service and a compression ratio. We define this interface:

HRESULT GrabFrame([in] BSTR strWeb ServiceURL, [in] short nQuality);
  • Input parameter 'strWeb ServiceURL' represents Web Service's URL.
  • Input parameter 'nQuality' represents the jpeg compression ratio, from 1 to 99. A value of one meaning lowest quality (highest compression) and a value of ninety-nine meaning highest quality (lowest compression).

If we get an error, we display it in a tooltip. To create and display the tooltip we use Windows Template Library (WTL).


You may test this Web Service after installing client ActiveX on this page.

Problems Faced

  • When adding the "GrabFrame" method to Webcam Server using the ATL "Add Method to Interface", the method was created correctly in .idl and .h files but the not in the .cpp. We can find a kind of explanation on MSDN: Q198017.
  • Before SOAP SDK SP2, SAFEARRAY(unsigned char) was not correctly handled. You need the SP2 to get a correct wrapping from SAFEARRAY(unisgned char) to base64Binary.


Version 1.00 December 4, 2001
First release.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Laurent Kempé
France France

Laurent Kempé is the editor, founder, and primary contributor of Tech Head Brothers, a French portal about Microsoft .NET technologies.

He is currently employed by Innoveo Solutions since 10/2007 as a Senior Solution Architect and certified Scrum Master.

Founder, owner and Managing Partner of Jobping, which provides a unique and efficient platform for connecting Microsoft skilled job seekers with employers using Microsoft technologies.

Laurent is awarded by Microsoft since Avril 2002: Most Valuable Professional (MVP).

You may also be interested in...


Comments and Discussions

Questionmy vote of 5 Pin
muhamad yousef4-Jun-12 20:45
membermuhamad yousef4-Jun-12 20:45 
GeneralIt's about time to leave the VFW and move to DirectShow Pin
uvik30-Sep-10 12:20
memberuvik30-Sep-10 12:20 
GeneralRe: It's about time to leave the VFW and move to DirectShow Pin
Laurent Kempé30-Sep-10 21:23
memberLaurent Kempé30-Sep-10 21:23 
GeneralA thank you Pin
uvik1-Oct-10 0:54
memberuvik1-Oct-10 0:54 
Dear Laurent,

Certainly your VFW component was great and popular for a lot of years.
For this I would like to thank you Big Grin | :-D

My reply was not to you but to people that still arrive to your article
to open their mind to new options.

An Idea, Visual C 6 is still very popular among a lot of Computer
Vision developers.

There are components that contain the DirectShow inside even here at
the CodeProject site but they were made for .NET

How about that you will contribute a new article to the CodeProject that will fill the gap and will implement a component to VS6.

Best regards,

QuestionHow to creat the ".DLL" file?? Pin
8200151830-Jul-09 22:57
member8200151830-Jul-09 22:57 
Generalit helps me! Pin
7916711529-Jul-09 20:40
member7916711529-Jul-09 20:40 
QuestionSOAP SDK v2.0 required? Pin
Dan Thurman13-Jul-06 17:04
memberDan Thurman13-Jul-06 17:04 
AnswerRe: SOAP SDK v2.0 required? Pin
Dan Thurman14-Jul-06 11:02
memberDan Thurman14-Jul-06 11:02 
Questionhow to caopture images in C++ using webcam Pin
richytany12-Jul-06 4:10
memberrichytany12-Jul-06 4:10 
QuestionHow to get array of int, or byte without the safearray Pin
guocang11-Jul-06 12:59
memberguocang11-Jul-06 12:59 
Questionusing camera example for a printer file Pin
david barnwell16-May-06 11:13
memberdavid barnwell16-May-06 11:13 
Questionvirtual WebCam Pin
olariuadrian17-Feb-06 2:00
memberolariuadrian17-Feb-06 2:00 
GeneralMicrosoft WSDL Generator Pin
Anonymous14-Jul-05 11:54
sussAnonymous14-Jul-05 11:54 
GeneralYahoo Protocol Pin
Manpreet Chadha25-Oct-04 20:01
memberManpreet Chadha25-Oct-04 20:01 
GeneralRe: Yahoo Protocol Pin
pengdingfu24-Dec-04 18:01
memberpengdingfu24-Dec-04 18:01 
GeneralHelp -- Compile Error for Client Pin
Zhijie Wang27-Aug-04 12:34
memberZhijie Wang27-Aug-04 12:34 
GeneralWeb Service Pin
Anonymous12-Aug-04 0:11
sussAnonymous12-Aug-04 0:11 
Questionis .NET framework required ? Pin
hspc30-Jul-04 20:18
memberhspc30-Jul-04 20:18 
GeneralWebcam Pin
hungrau28-Apr-04 18:33
memberhungrau28-Apr-04 18:33 
QuestionCamera pilotable via interface web et ActiveX de la carte de capture video ? Pin
ALIBAS26-Apr-04 21:25
memberALIBAS26-Apr-04 21:25 
QuestionCamera pilotable via interface web et pour l'ActiveX de la carte de capture video ? Pin
ALIBAS26-Apr-04 5:46
memberALIBAS26-Apr-04 5:46 
Questionweb page sample doesn't exist ? Pin
orenboskila19-Feb-04 14:37
memberorenboskila19-Feb-04 14:37 
AnswerRe: web page sample doesn't exist ? Pin
Laurent Kempé19-Feb-04 22:20
memberLaurent Kempé19-Feb-04 22:20 
Questionhow to save a flash file created with a object as a gif/jpg/bmp file? Pin
zhongxunyang24-Sep-03 17:49
memberzhongxunyang24-Sep-03 17:49 
AnswerRe: how to save a flash file created with a object as a gif/jpg/bmp file? Pin
Nishant S24-Sep-03 18:26
editorNishant S24-Sep-03 18:26 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 24 Apr 2002
Article Copyright 2002 by Laurent Kempé
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid