Click here to Skip to main content
Click here to Skip to main content

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

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

Share

About the Author

Holger Kloos

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
Generalusing this with hidden window PinmemberRecycleYourLife4-Mar-09 12:01 
QuestionZoomlevel: lower then 100%? Pinmemberrootdial18-Oct-07 5:53 
QuestionThis application can be used digital camer? Pinmemberskyramesh29-Sep-07 1:11 
QuestionCommercial use? Pinmemberrootdial19-Sep-07 21:09 
GeneralSave image as jpeg Pinmembernishap31-Oct-05 5:52 
GeneralVB.net PinmemberTonkutsche.de24-Feb-05 2:12 
Hello!
 
Is there something similar in VB.net?
 
Cheers Florian
GeneralFinding the FILE NAME PinmemberAlex Evans10-Feb-05 18:25 
GeneralERROR: Can't access scan options! PinmemberNico Cuppen21-Jul-04 3:04 
GeneralRe: ERROR: Can't access scan options! Pinmemberavtar3-Aug-04 0:09 
GeneralRe: ERROR: Can't access scan options! PinmemberSeong Yun28-Mar-05 20:25 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 15 Jul 2004
Article Copyright 2004 by Holger Kloos
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid