XBase++ Floating Point DTWAIN Examples

Top  Previous  Next

Here are some examples of usage of XBase++ and handling of floating point in DTWAIN:

 

Example 1: Set the current resolution value:

 

#include "Dll.ch"

#include "DTWAIN32.CH"

 

PROCEDURE Main

  LOCAL TwainSource, nDLLHandle, res, returnValue

  LOCAL retStr := Space(254)

 

  /* Load the DTWAIN DLL */

  nDLLHandle := DllLoad( "DTWAIN32.DLL" )

 

  /* Initialize DTWAIN */

  DLLCall (nDLLHandle,DLL_STDCALL, "DTWAIN_SysInitialize")

 

  /* Get a TWAIN Source from Select Source Dialog */

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

 

  /* If user selected a source, attempt to set the resolution to 300 */

  IF  TwainSource <> 0

      returnValue := DLLCall(nDLLHandle,DLL_STDCALL,"DTWAIN_SetResolutionString", TwainSource, "300.0")

      IF returnValue <> 0

          /* Resolution set successfully */

          DLLCall(nDLLHandle, DLL_STDCALL, "DTWAIN_GetResolutionString", TwainSource, @retStr)

      ENDIF

  ENDIF

 

  /* Uninitialize DTWAIN and unload the DLL */

  DLLCall(nDLLHandle,DLL_STDCALL,"DTWAIN_SysDestroy")

 

  DllUnload( nDLLHandle )

 

RETURN

 


 

In Example 1, the function DTWAIN_SetResolutionString and DTWAIN_GetResolutionString act exactly the same as DTWAIN_SetResolution and DTWAIN_GetResolution, with the lone exception being that the last parameter of the DTWAIN_xxxString() function is a string value instead of a DTWAIN_FLOAT.  This allows XBase++ programmers to use string values instead of floating point variables that may not be compatible to Windows 64-bit types.

 


 

Example 2:  Get all of the resolution values.  This example shows conversion of an array of floating point values to fixed point values.

 

#include "Dll.ch"

#include "DTWAIN32.CH"

 

PROCEDURE Main

  LOCAL TwainSource, nDLLHandle, res, returnValue, fixedPtArray, floatArray, numValues, whole, frac

  LOCAL strValue

  floatArray := 1

  whole := 1

  frac := 1

 

  /* Load the DTWAIN DLL */

  nDLLHandle := DllLoad( "DTWAIN32.DLL" )

 

  /* Initialize DTWAIN */

  DLLCall( nDLLHandle,DLL_STDCALL, "DTWAIN_SysInitialize" )

 

  /* Get a TWAIN Source from Select Source Dialog */

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

 

  /* If user selected a source, attempt to get all of the resolution values */

  IF  TwainSource <> 0

      DLLCall ( nDLLHandle, DLL_STDCALL, "DTWAIN_EnumResolutionValues", TwainSource, @floatArray, 1 )

 

      /* get the number of values in the array */

      numValues := DLLCall ( nDLLHandle, DLL_STDCALL, "DTWAIN_ArrayGetCount" ,floatArray )

 

      /* Convert to fixed point array */

      fixedPtArray := DllCall( nDLLHandle, DLL_STDCALL, "DTWAIN_ArrayConvertFloatToFix32", floatArray)

 

      SET DEVICE TO SCREEN

      @ 0, 0 SAY "Resolution Values are as follows:"

 

      FOR i := 1 TO numValues

          // whole and frac contain the values in the array

          DLLCall( nDLLHandle, DLL_STDCALL, "DTWAIN_ArrayFix32GetAt", fixedPtArray, i-1, @whole, @frac)

          strValue := LTrim(Str(whole)) + "." + LTrim(Str(frac))

          @ i, 0 SAY strValue

      NEXT

      Inkey(0)

  ENDIF

 

  /* Uninitialize DTWAIN and unload the DLL */

  DLLCall(nDLLHandle,DLL_STDCALL,"DTWAIN_SysDestroy")

  DllUnload( nDLLHandle )

RETURN


 

Note that the call to DTWAIN_ArrayConvertFloatToFix32 converts the array of 64-bit floating point values to an array of fixed point values (The device resolution values are potentially floating point value, thus the need to use floating point types to store this information).  Once that's done, to get the whole number and fractional portion of the fixed point number, the DTWAIN_ArrayFix32GetAt function returns the whole number and fractional portion of the desired number.

 

Please note:

There are functions named F2Bin( ) and Bin2F( ) defined for XBase++.  However with our experience using these functions, the floating point values passed to the DTWAIN functions do not convert to proper IEEE 64-bit values that adhere to the same layout that C or C++ expects.  Therefore it is best to have DTWAIN functions interpret the values in a safe, sure way, and that is to use the string versions of the DTWAIN functions that require floating point data.