Click here to Skip to main content
13,352,408 members (39,732 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

Generalusing this with hidden window Pin
RecycleYourLife4-Mar-09 12:01
memberRecycleYourLife4-Mar-09 12:01 
QuestionZoomlevel: lower then 100%? Pin
rootdial18-Oct-07 5:53
memberrootdial18-Oct-07 5:53 

maybe someone may answer me my questionConfused | :confused:

If you want to set the zoom to less then 100%.
Lets say 50%, to show a smaller image, how can
it be done?

So far i did not see how to do it.

Hope someone can answer Roll eyes | :rolleyes:
QuestionThis application can be used digital camer? Pin
skyramesh29-Sep-07 1:11
memberskyramesh29-Sep-07 1:11 
QuestionCommercial use? Pin
rootdial19-Sep-07 21:09
memberrootdial19-Sep-07 21:09 
GeneralSave image as jpeg Pin
nishap31-Oct-05 5:52
membernishap31-Oct-05 5:52 Pin
Tonkutsche.de24-Feb-05 2:12
memberTonkutsche.de24-Feb-05 2:12 
GeneralFinding the FILE NAME Pin
Alex Evans10-Feb-05 18:25
memberAlex Evans10-Feb-05 18:25 
GeneralERROR: Can't access scan options! Pin
Nico Cuppen21-Jul-04 3:04
memberNico Cuppen21-Jul-04 3:04 
GeneralRe: ERROR: Can't access scan options! Pin
avtar3-Aug-04 0:09
memberavtar3-Aug-04 0:09 
GeneralRe: ERROR: Can't access scan options! Pin
Seong Yun28-Mar-05 20:25
memberSeong Yun28-Mar-05 20:25 
GeneralRe: ERROR: Can't access scan options! Pin
Pandele Florin20-Oct-05 5:13
memberPandele Florin20-Oct-05 5:13 
GeneralRe: ERROR: Can't access scan options! Pin
K I V19-Oct-06 21:56
memberK I V19-Oct-06 21:56 
GeneralRe: ERROR: Can't access scan options! Pin
Pandele Florin23-Oct-06 0:08
memberPandele Florin23-Oct-06 0:08 

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