Set/Get a Twain Capability

Take the following steps to get or set a twain capability:

1.

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

2.

Copy everything in the EZFUNC directory into the EZTWNCAP directory.

3.

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

4.

Remove the EXFunc.cpp 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 lttw2_n.lib file to the project from the LTWINxxx\LIB directory.

7.

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

HTWAINSESSION hSession = NULL;
TW_CAPABILITY twCap;
L_UINT uItemValue;

8.

Add the following definition in the top of the EZFunc.cpp file, before any include statement:

#define USE_NEWTWAIN

9.

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_SELECT_SOURCE

Select Source

 

IDM_GET_CAPABILITY

Get Capability

 

IDM_SET_CAPABILITY

Set Capability

 

IDM_SET_BRIGHTNESS

Set Brightness

 

IDM_ACQUIRE

Acquire

10.

In the InitApplication function change the line:

wcWindowClass.lpszMenuName = NULL;  /* No menu */

to be

wcWindowClass.lpszMenuName = TEXT("MAIN_MENU");

11.

In the EZFunc.cpp file add the following includes:

#include "resource.h"
#include "LTWINxxx\Include\ltTw2.h"

12.

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

   case WM_COMMAND: 
      {
         switch (LOWORD(wParam)) 
         {
         case IDM_SELECT_SOURCE: 
            {
            }
            break; 
         case IDM_GET_CAPABILITY: 
            {
            }
            break; 
         case IDM_SET_CAPABILITY: 
            {
            }
            break; 
         case IDM_SET_BRIGHTNESS:
            {
            }
            break; 
         case IDM_ACQUIRE: 
            {
            }
            break; 
         }
      }
      break;

13.

In the WM_CREATE message handler add the following code at the end of it (after SendMessage (hWnd, WM_QUERYNEWPALETTE, 0, 0L);):

APPLICATIONDATA AppData; 
AppData.uStructSize = sizeof(APPLICATIONDATA);
AppData.hWnd = hWnd; 
lstrcpy (AppData.szManufacturerName, TEXT("LEAD Technologies, Inc.")); 
lstrcpy (AppData.szAppProductFamily, TEXT("LEAD Test Applications"));
lstrcpy (AppData.szVersionInfo, TEXT("Version 1.0"));
lstrcpy (AppData.szAppName, TEXT("TWAIN Test Application"));
nRet = L_TwainInitSession(&hSession, &AppData); 
if (nRet != SUCCESS) 
   return FALSE; 

/* Unlock Document support.
Note that this is a sample key, which will not work in your toolkit. */ L_UnlockSupport(L_SUPPORT_DOCUMENT, TEXT("TestKey"));

14.

In the WM_DESTROY message handler add the following code at the end of it (before the PostQuitMessage (0);):

L_TwainEndCapsNeg(hSession);
L_TwainEndSession
(&hSession);

15.

In the IDM_SELECT_SOURCE menu handler add the following code:

case IDM_SELECT_SOURCE:
   {
      nRet = L_TwainSelectSource(hSession, NULL); 
   }
   break;

16.

In the IDM_GET_CAPABILITY menu handler add the following code:

case IDM_GET_CAPABILITY:
   {
      L_INT  nItemType;

      twCap.Cap = ICAP_XFERMECH;
      twCap.ConType = TWON_DONTCARE16;
      twCap.hContainer = NULL;
      L_TwainGetCapability(hSession, &twCap, LTWAIN_CAPABILITY_GETCURRENT);

      L_TwainGetNumericContainerItemType(&twCap, &nItemType);

      if (nItemType == TWTY_UINT16)
         L_TwainGetNumericContainerUINTValue(&twCap, 0, &uItemValue);
      else
         MessageBox(hWnd, TEXT("Returned bad item type"), TEXT("Get Item Type"), MB_OK);
   }
   break;

17.

In the IDM_SET_CAPABILITY menu handle add the following code:

case IDM_SET_CAPABILITY: 
   {
      if (uItemValue != TWSX_NATIVE)
      {
         TW_CAPABILITY twSetCap;

         twSetCap.Cap = ICAP_XFERMECH; 
         twSetCap.ConType = TWON_ONEVALUE;

         L_TwainCreateNumericContainerOneValue(&twSetCap, TWAINNUMERICTYPE_TW_UINT16, TWSX_NATIVE);

         L_TwainSetCapability(hSession, &twSetCap, LTWAIN_CAPABILITY_SET);
         L_TwainFreeContainer(&twSetCap);
      }
      else
         MessageBox(hWnd, TEXT("The Transfer mode is Native"), TEXT("Set Capability"), MB_OK);
   }
   break;

18.

In the IDM_SET_BRIGHTNESS menu handle add the following code:

case IDM_SET_BRIGHTNESS:
   {
      TW_CAPABILITY twBrightnessCap;
      TW_FIX32      twFix32;
      pTW_ONEVALUE  ptwOneVal;

      twBrightnessCap.Cap = ICAP_BRIGHTNESS;
      twBrightnessCap.ConType = TWON_ONEVALUE;

      L_INT nRet = L_TwainGetCapability (hSession, &twBrightnessCap, LTWAIN_CAPABILITY_GETCURRENT);

      ptwOneVal = (pTW_ONEVALUE)GlobalLock(twCap.hContainer);

      memcpy(&twFix32, (void *)&ptwOneVal->Item, sizeof(TW_FIX32));

      GlobalUnlock(twBrightnessCap.hContainer);
      GlobalFree(twBrightnessCap.hContainer);
      twBrightnessCap.hContainer = NULL;

      if (twFix32.Whole != 128)
      {
         twFix32.Whole = 128;
         twFix32.Frac = 0;
      }

      twBrightnessCap.Cap = ICAP_BRIGHTNESS;
      twBrightnessCap.ConType = TWON_ONEVALUE;
      twBrightnessCap.hContainer = GlobalAlloc(GHND, sizeof(TW_ONEVALUE) + sizeof(TW_FIX32));

      ptwOneVal = (pTW_ONEVALUE)GlobalLock(twCap.hContainer);

      ptwOneVal->ItemType = TWTY_FIX32;
      memcpy((void *)&ptwOneVal->Item, &twFix32, sizeof(TW_FIX32));
      GlobalUnlock(twBrightnessCap.hContainer);

      L_TwainSetCapability (hSession, &twBrightnessCap, LTWAIN_CAPABILITY_SET);
   }
   break;

19.

In the IDM_ACQUIRE menu handler add the following code:

case IDM_ACQUIRE: 
   {
      L_TwainEndCapsNeg(hSession);
      if (LeadBitmap.Flags.Allocated)
      {
         L_FreeBitmap(&LeadBitmap);
          L_InitBitmap(&LeadBitmap, sizeof(BITMAPHANDLE), 0, 0, 0);

         L_TwainAcquire(hSession, &LeadBitmap, sizeof(BITMAPHANDLE), NULL, LTWAIN_SHOW_USER_INTERFACE, NULL, NULL);
      }
   }
   break;

20.

Compile and test the program.