C/C++ Sample Program

The following console 'C' / C++ program is all that is required to do the following (the console program was written just for illustrative purposes.  You can use DTWAIN when creating console or GUI applications):


#include <dtwain.h>

int main()
    DTWAIN_SOURCE TheSource; /* The TWAIN Data Source */
    LONG Status;

    /* Check if TWAIN exists. Return if TWAIN is not installed */
    if ( DTWAIN_IsTwainAvailable( ) == 0 )

    /* Initialize DTWAIN DLL, return if there is a problem */
    if ( DTWAIN_SysInitialize( ) == 0 )

    /* Select the Twain Source */
    TheSource = DTWAIN_SelectSource( );

    /* If no Source selected or there is a problem, uninitialize DLL and return */
    if ( TheSource == NULL )
        DTWAIN_SysDestroy( );

    /* Proceed to acquire image using native mode and save to a bmp file */
    RetVal = DTWAIN_AcquireFile(TheSource, /* Source */
                                "test.bmp", /* File name */
                                DTWAIN_BMP, /* File type is BMP */
                       DTWAIN_USENATIVE | DTWAIN_USENAME, /* acquisition options */
                                DTWAIN_PT_DEFAULT, /* use default color type */
                                1, /* Acquire 1 page */
                                TRUE, /* Show the device's user interface */
                                FALSE. /* Do not keep Source open when finished. */
                                &Status); /* returned status */

    /* Close down TWAIN, Data Sources, etc. */
    DTWAIN_SysDestroy( );


The example above is a C or C++ program that demonstrates some of the power of DTWAIN. The only functions that are DTWAIN specific are the following:

Five calls to do the job! If you didn't call DTWAIN_IsTwainAvailable, DTWAIN still checks if your application can attempt to acquire an image in a "TWAIN-less" system, making this only four calls necessary to acquire an image. Imagine the time these few lines of code would save you instead of implementing TWAIN using the traditional, error-prone methods. The state-transition logic, the management of the TWAIN message loop, the TWAIN triplets, etc. is all handled by DTWAIN. Remember that the example shows only some of the functionality of DTWAIN. 

Even though this example is written in the 'C' language, any language that is able to call DLL functions will get the same benefits.  Note that DTWAIN has the ability to save the images to a file (as in the example above shows), or if your application wants the raw image data, a "handle" to the image data can be returned instead of writing the image data to a file.

If you are using C++, you can use the traditional DTWAIN interface as the program above demonstrates, or the DTWAIN C++ class wrapper. Here is a console program using the C++ class wrapper that essentially does the same thing as the program above:

#include "cdtwain.h"
using namespace DTWAIN;

int main( )
    DTwainInterface TwainInterface; // Initialize DTWAIN
    if ( TwainInterface.IsValid( ) ) // Check if valid interface
        DTwainSource Source = DTwainSource::Select( );
        if ( Source.IsValid( ) )
            DTwainAcquirer( Source ).Acquire("test.bmp" );
} // shuts down TWAIN, DTWAIN automatically


Note how much shorter using the DTWAIN class wrapper code is. This code

This is a lot of power in just a few lines of C++ code.