Click here to Skip to main content
13,408,259 members (49,003 online)
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 14 Jul 2004

A Copy Utility using TWAIN

, 14 Jul 2004
Rate this:
Please Sign up or sign in to vote.
Example for a simple encapsulation of the TWAIN interface


The TWAIN interface allows the access to most image sources. It is a quite old standard in terms of computer programming and therefore uses a C interface and Windows messages (on Windows systems) for communication. This example shows how to encapsulate the TWAIN interface with a C++ interface.


For details of the TWAIN specification please take a look at the homepage of the TWAIN Working Group. This example I show refers to version 1.9 of this document. It is quite easy to read, but the interface is a broad one also it mainly uses one interface function to control the image source only:

// from twain.h:
#ifdef  _MSWIN_
                                pTW_IDENTITY pDest,
                                TW_UINT32    DG,
                                TW_UINT16    DAT,
                                TW_UINT16    MSG,
                                TW_MEMREF    pData);

                                             TW_UINT32,    TW_UINT16,
                                             TW_UINT16,    TW_MEMREF);
#else   /* _MSWIN_ */
Because there are a lot of commands and messages as possible parameters of this function the interface is quite more complex than it seems on the first look. The complete interface is defined in twain.h. This file is necessary for the compilation of this example but it is not included the sources attached to this document because it is owned by the TWAIN Working Group where it can be downloaded easily.

Using the code

This example only uses a subset of TWAIN commands, but it is easy to enlarge it. I tested it with two different scanners, an Epson Perfection on a Windows 2000 system and a quite old HP DeskScan 3c on a Windows NT 6 system. For easier debugging the program writes a log file with some traces while it is running. It supports two languages, English as default and German. You can switch on the German GUI by entering
in the configuration file TCopy.ini located in the directory of the executable. It also stores the predefined values for brightness and contrast.

The most interesting class of this sample is CTwainPP hiding all the TWAIN stuff. At runtime it maps the Twain Source Manager TWAIN_32.DLL to connect to the TWAIN system. It uses some helper classes. One of them is CTwainEventHandler which receives all the feedback windows messages sent back by the image source and forward them to CTwainPP. For this purpose this class needs a host window where it hooks its own message callback function into. This technique allows to recycle the main dialog window for this purpose but it is easy to replace this mechanism with a hidden additional window.

Beside the TWAIN interface the sample application uses the class CDIBUse for the handling of a Device Independent Bitmap (DIB) and the class CPrinterAccess to control the printer and to print out a DIB. The class CShowBitmapCtrl is a specialized CStatic control to display a DIB.


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

Holger Kloos
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionZoomlevel: lower then 100%? Pin
rootdial18-Oct-07 5:53
memberrootdial18-Oct-07 5:53 

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
Web02 | 2.8.180221.1 | Last Updated 15 Jul 2004
Article Copyright 2004 by Holger Kloos
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid