XBase++ Sample: Acquire and save to BMP file

Top  Previous  Next

The following XBase++ example is all that is required to do the following:

 

Check if TWAIN is installed.
Initialize the DTWAIN Dynamic Link Library.
Select a Source
Acquire a page using the Native transfer mode
Save page to a BMP file called "test.bmp"
Shut down any open TWAIN Source's and DTWAIN itself

 


#include "Dll.ch"

 

// Include the DTWAIN constants

#include "DTWAIN32.CH"

 

PROCEDURE Main

  LOCAL TwainSource, nDLLHandle, TwainOK, status, fileOption

 

  fileOption := DTWAIN_USENATIVE + DTWAIN_USELONGNAME

  status := 0

 

  // Load the DTWAIN DLL

  nDLLHandle := DLLLoad( "DTWAIN32.DLL" )

 

  // Check if TWAIN is available

  TwainOK := DLLCall( nDLLHandle, DLL_STDCALL, "DTWAIN_IsTwainAvailable" )

 

  IF TwainOK == 1

       // Initialize DTWAIN

      DLLCall ( nDLLHandle, DLL_STDCALL, "DTWAIN_SysInitialize" )

 

      // Select a TWAIN Source

      TwainSource := DLLCall( nDLLHandle, DLL_STDCALL, "DTWAIN_SelectSource" )

 

      IF  TwainSource <> 0

            // Acquire a file

            DLLCall ( nDLLHandle, DLL_STDCALL, "DTWAIN_AcquireFile", TwainSource, "Test.bmp", DTWAIN_BMP, fileOption, DTWAIN_PT_DEFAULT, 1, 1, 1, @status )

      ENDIF

 

      // Uninitialize DTWAIN

      DLLCall( nDLLHandle, DLL_STDCALL, "DTWAIN_SysDestroy" )

 

  ENDIF

   // Unload the DLL

   DLLUnload( nDLLHandle )

RETURN

 


In the example above, note that all the calls to DLLCall requires a DLL_STDCALL calling convention.  The "stdcall"  calling convention is the one used by the DTWAIN DLL, and you must specify to XBase++ that the DTWAIN functions use this convention.

 

Also, the example calls DLLLoad and DLLUnload.  It is highly recommended that these two XBase++ functions are done only once during the running of the application.  Therefore your actual application would place the call to DllLoad in a function that's called on startup of the application, and DllUnload at application termination.  Otherwise, repeatedly calling DllLoad and DllUnload during the running of your application will cause unnecessary overhead, since the DLL has to be reinitialized.

 

For XBase++, you must include the "DTWAIN32.CH" header file.  This header file defines the DTWAIN constants that will be used.

 

In addition, the section on Floating Point Issues is recommended for XBase++ users who want to call DTWAIN functions that require floating point parameters.