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:
TW_UINT16 FAR PASCAL DSM_Entry( pTW_IDENTITY pOrigin,
typedef TW_UINT16 (FAR PASCAL *DSMENTRYPROC)(pTW_IDENTITY, pTW_IDENTITY,
#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
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.
CShowBitmapCtrl is a specialized
CStatic control to display a DIB.