Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Copy Utility using TWAIN

0.00/5 (No votes)
14 Jul 2004 3  
Example for a simple encapsulation of the TWAIN interface

Introduction

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.

Background

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_
TW_UINT16 FAR PASCAL DSM_Entry( pTW_IDENTITY pOrigin,
                                pTW_IDENTITY pDest,
                                TW_UINT32    DG,
                                TW_UINT16    DAT,
                                TW_UINT16    MSG,
                                TW_MEMREF    pData);

typedef TW_UINT16 (FAR PASCAL *DSMENTRYPROC)(pTW_IDENTITY, pTW_IDENTITY,
                                             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
[Deutsch]
value=1
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.

License

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