Twain Duplex Tutorial

Take the following steps to create and run a program that implements LEADTOOLS TWAIN features. Remember, the purpose of the TWAIN tutorial is to provide you a quick and easy way to generate a TWAIN program.

1.

Create a new directory in the LTWINxxx\EXAMPLES\DLL directory called EZTWNDuplex.

2.

Copy everything in the EZFUNC directory into the EZTWNDuplex directory.

3.

Rename the EZFunc.c file to be EZFunc.cpp.

4.

Remove the EXFunc.c from the project and add the EZFunc.cpp in its place.

5.

Compile the project as it is and run EZFUNC32.exe to familiarize yourself with the basic program. You can run the exe file from the MS-DOS prompt, or set the image file to load through the Project->Settings->Debug->Program Arguments menu.

6.

On the Insert menu, select Files into Project. Add the Lttwn_n.lib file to the project from the LTWINxxx\LIB directory.

7.

Define the following global variables in EZFUNC.CPP in the EZTWNDuplex directory:

HTWAINSESSION hSession = NULL;

8.

In the project workspace go to the resources tab. Add a menu ("MAIN_MENU") to the application with the following menu items:

 

ID

Caption

 

IDM_ACQUIRE

Acquire

9.

In the InitApplication function change the line:

wcWindowClass.lpszMenuName = NULL;  /* No menu */
to be
wcWindowClass.lpszMenuName = TEXT("MAIN_MENU");

10.

In the EZFunc.cpp add the following includes:

#include "resource.h"

11.

In the MainWndProc function and before the handling of the WM_PALETTECHANGED message add the following code:

   case WM_COMMAND: 
      {
         switch (LOWORD(wParam)) 
         {
         case IDM_ACQUIRE: 
            {
            }
            break; 
      }
      break;

12.

In the IDM_ACQUIRE menu handler add the following code:

         case IDM_ACQUIRE: 
            {
               TwainTestDuplex(hWnd); 
            }

13.

Add the following function to the EZFUNC.CPP:

L_INT L_FAR L_EXPORT TwainAcquireCB(HTWAINSESSION hTwain, pBITMAPHANDLE pBitmap, L_VOID L_FAR * pUserData) 
{
   // ... 
   // set your code here ... 
   // ... 

   return(SUCCESS); 
}

14.

Add the following function to the EZFUNC.CPP:

void TwainTestDuplex(HWND hWnd) 
{
   HTWAINSESSION hSession; 
   APPLICATIONDATA AppData; 
   L_INT nRet; 
   TW_CAPABILITY twCap; 

   memset(&AppData, 0, sizeof(APPLICATIONDATA)); 

   AppData.hWnd = hWnd; 
   lstrcpy (AppData.szManufacturerName, TEXT("LEAD Technologies, Inc.")); 
   lstrcpy (AppData.szAppProductFamily, TEXT("LEAD Sample"));
   lstrcpy (AppData.szVersionInfo,      TEXT("Version 1.0"));
   lstrcpy (AppData.szAppName,          TEXT("LEAD Sample"));
   AppData.uStructSize = sizeof(APPLICATIONDATA); 

   nRet = L_TwainInitSession(&hSession, &AppData); 
   if (nRet != TWAIN_SUCCESS) 
      return; 

   L_TwainSelectSource(hSession, NULL);

   L_TwainStartCapsNeg(hSession); 

   twCap.Cap = CAP_DUPLEX; 
   twCap.ConType = TWON_ONEVALUE; 
   twCap.hContainer = NULL; 

   nRet = L_TwainGetCapability(hSession, &twCap, LTWAIN_CAPABILITY_GETVALUES); 
   if (nRet == TWAIN_SUCCESS) 
   {
      // check if the selected driver supports duplex capability
      pTW_ONEVALUE pOneValue = (pTW_ONEVALUE)GlobalLock(twCap.hContainer); 
      if (pOneValue->ItemType != TWTY_UINT16) 
      {
         GlobalUnlock(twCap.hContainer); 
         L_TwainFreeContainer(&twCap); 
         L_TwainEndCapsNeg(hSession); 
         L_TwainEndSession(&hSession); 
         return; 
      }

      TW_UINT16 item = (TW_UINT16)pOneValue->Item; 

      switch (item) 
      {
      case TWDX_NONE: 
         MessageBox(hWnd, TEXT("Duplex capability is not supported in the selected driver"), TEXT("Notice"), MB_OK); 
         break; 
      case TWDX_1PASSDUPLEX: 
         MessageBox(hWnd, TEXT("1 Pass Duplex capability is supported in the selected driver"), TEXT("Notice"), MB_OK); 
         break; 
      case TWDX_2PASSDUPLEX: 
         MessageBox(hWnd, TEXT("2 Pass Duplex capability is supported in the selected driver"), TEXT("Notice"), MB_OK); 
         break; 
      }

      GlobalUnlock(twCap.hContainer); 
      L_TwainFreeContainer(&twCap); 

      // make sure the duplex capability is enabled
      if (item != TWDX_NONE) 
      {
         BOOL bEnable = TRUE; 

         twCap.Cap = CAP_DUPLEXENABLED; 
         twCap.ConType = TWON_ONEVALUE; 
         twCap.hContainer = GlobalAlloc(GHND, sizeof(TW_ONEVALUE)); 

         pTW_ONEVALUE pOneValue = (pTW_ONEVALUE)GlobalLock(twCap.hContainer); 
         pOneValue->ItemType = TWTY_BOOL; 
         pOneValue->Item = (TW_UINT32)bEnable; 

         GlobalUnlock(twCap.hContainer); 
         nRet = L_TwainSetCapability(hSession, &twCap, LTWAIN_CAPABILITY_SET); 
         if (nRet == TWAIN_SUCCESS) 
            MessageBox(hWnd, TEXT("The duplex capability is enabled"), TEXT("Notice"), MB_OK); 
         else
            MessageBox(hWnd, TEXT("Can't enable duplex capability"), TEXT("Notice"), MB_OK); 
      }
   }
   else
      MessageBox(hWnd, TEXT("Duplex capability is not supported in the selected driver"), TEXT("Notice"), MB_OK); 

   L_TwainEndCapsNeg(hSession); 

   // try to acquire pages using duplex capability
   L_TwainAcquire(hSession, NULL, sizeof(BITMAPHANDLE), TwainAcquireCB, LTWAIN_SHOW_USER_INTERFACE | LTWAIN_MODAL_USER_INTERFACE, NULL, NULL); 

   L_TwainEndSession(&hSession); 
}

15.

Compile and test the program.