I attach de code but the solution for this is not urgent for me as I can finish my aplication (I will do that first) and after I will try to optimize the battery life.
Code: Select all
//--------------------------------------------------------------------
// FILENAME: BasicScan.c
//
// Copyright(c) 2002 Symbol Technologies Inc. All rights reserved.
//
// DESCRIPTION: This simple scanning application demonstrates
// how to use the Symbol Scan Library.
// The API's used are as follows:
//
// SCAN_Open
// SCAN_Enable
// SCAN_AllocateBuffer
// SCAN_ReadLabelMsg
// SCAN_Flush
// SCAN_Disable
// SCAN_DeallocateBuffer
// SCAN_Close
// SCAN_SetSoftTrigger
//
//
//
//
//--------------------------------------------------------------------
#include <windows.h>
#include <windowsx.h>
#include <ScanCAPI.h>
#include "resource.h"
#define countof(x) sizeof(x)/sizeof(x[0])
// Define user messages
enum tagUSERMSGS
{
UM_SCAN = WM_USER + 0x200,
UM_STARTSCANNING,
UM_STOPSCANNING
};
// Global variables
HINSTANCE hInst = NULL;
HANDLE hScanner = NULL;
LPSCAN_BUFFER lpScanBuffer = NULL;
TCHAR szScannerName[MAX_PATH] = TEXT("SCN1:"); // default scanner name
DWORD dwScanSize = 7095; // default scan buffer size
DWORD dwScanTimeout = 0; // default timeout value (0 means no timeout)
BOOL bUseText = TRUE;
BOOL bTriggerFlag = FALSE;
BOOL bRequestPending = FALSE;
BOOL bStopScanning = FALSE;
// Forward declarations
LRESULT CALLBACK BasicScanProc(HWND,UINT,WPARAM,LPARAM);
void ErrorExit(HWMD, UINT, LPTSTR);
LPTSTR LoadMsg(UINT, LPTSTR, int);
//----------------------------------------------------------------------------
//
// FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
//
// PURPOSE: Entry point function, initializes the application, instance,
// and then launches the message loop.
//
// PARAMETERS:
// hInstance - handle that uniquely identifies this instance of the
// application
// hPrevInstance - always zero in Win32
// lpszCmdLine - any command line arguements pass to the program
// nCmdShow - the state which the application shows itself on
// startup
//
// RETURN VALUE:
// (int) Returns the value from PostQuitMessage().
//
//----------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpszCmdLine,
int nCmdShow)
{
int nResult;
hInst = hInstance; // save the instance handle to a global variable
nResult = DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_SSCAN), NULL,
BasicScanProc);
return nResult;
}
//----------------------------------------------------------------------------
//
// FUNCTION: BasicScanProc(HINSTANCE, HINSTANCE, LPSTR, int)
//
// PURPOSE: Application-defined callback function that processes messages
// sent to BasicScan dialog.
//
// PARAMETERS:
// hwnd - handle to the dialog box.
// uMsg - specifies the message.
// wParam - specifies additional message-specific information.
// lParam - specifies additional message-specific information.
//
// RETURN VALUE:
// (BOOL) return TRUE if it processed the message, and FALSE if it did not.
//
//----------------------------------------------------------------------------
LRESULT CALLBACK BasicScanProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
DWORD dwResult;
TCHAR szLabelType[10];
TCHAR szLen[MAX_PATH];
TCHAR szMsgBuf[256];
LPSCAN_BUFFER lpScanBuf;
HWND hctl_data, hctl_length, hctl_type;
switch(uMsg)
{
case WM_INITDIALOG:
PostMessage(hwnd,UM_STARTSCANNING,0,0L);
break;
case UM_STARTSCANNING:
// Open scanner, prepare for scanning,
// and submit the first read request
dwResult = SCAN_Open(szScannerName, &hScanner);
if ( dwResult != E_SCN_SUCCESS )
{
ErrorExit(hwnd, IDS_FAILURE, TEXT("SCAN_Open"));
break;
}
dwResult = SCAN_Enable(hScanner);
if ( dwResult != E_SCN_SUCCESS )
{
ErrorExit(hwnd, IDS_FAILURE, TEXT("SCAN_Enable"));
break;
}
lpScanBuffer = SCAN_AllocateBuffer(bUseText, dwScanSize);
if (lpScanBuffer == NULL)
{
ErrorExit(hwnd, IDS_FAILURE, TEXT("SCAN_AllocateBuffer"));
return TRUE;
}
dwResult = SCAN_ReadLabelMsg(hScanner,
lpScanBuffer,
hwnd,
UM_SCAN,
dwScanTimeout,
NULL);
if ( dwResult != E_SCN_SUCCESS )
ErrorExit(hwnd, IDS_FAILURE, TEXT("SCAN_ReadLabelMsg"));
else
bRequestPending = TRUE;
break;
return TRUE;
case UM_STOPSCANNING:
// We stop scanning in two steps: first, cancel any pending read
// request; second, after there is no more pending request, disable
// and close the scanner. We may need to do the second step after
// a UM_SCAN message told us that the cancellation was completed.
if (!bStopScanning && bRequestPending)
SCAN_Flush(hScanner);
if (!bRequestPending)
{
SCAN_Disable(hScanner);
if (lpScanBuffer)
SCAN_DeallocateBuffer(lpScanBuffer);
SCAN_Close(hScanner);
EndDialog(hwnd, 0);
}
bStopScanning = TRUE;
return TRUE;
case WM_ACTIVATE:
// In foreground scanning mode, we need to cancel read requests
// when the application is deactivated, and re-submit read request
// when the application is activated again.
switch(LOWORD(wParam))
{
case WA_INACTIVE:
hctl_data = GetDlgItem(hwnd,IDC_EDIT_DATA);
hctl_length = GetDlgItem(hwnd,IDC_EDIT_LEN);
hctl_type = GetDlgItem(hwnd,IDC_EDIT_TYPE);
Edit_SetText(hctl_data, LoadMsg(IDS_INACTIVE,
szMsgBuf, countof(szMsgBuf)));
Edit_SetText(hctl_length, TEXT(""));
Edit_SetText(hctl_type, TEXT(""));
// Cancel any pending request since we are going to lose focus
if (bRequestPending)
dwResult = SCAN_Flush(hScanner);
// Do not set bRequestPending to FALSE until
// we get the UM_SCAN message
break;
default: // activating
hctl_data = GetDlgItem(hwnd,IDC_EDIT_DATA);
Edit_SetText(hctl_data, LoadMsg(IDS_READY,
szMsgBuf, countof(szMsgBuf)));
if (!bRequestPending && lpScanBuffer != NULL && !bStopScanning)
{
// Submit a read request if no request pending
dwResult = SCAN_ReadLabelMsg(hScanner,
lpScanBuffer,
hwnd,
UM_SCAN,
dwScanTimeout,
NULL);
if ( dwResult != E_SCN_SUCCESS )
ErrorExit(hwnd, IDS_FAILURE, TEXT("SCAN_ReadLabelMsg"));
else
bRequestPending = TRUE;
}
break;
}
break;
case UM_SCAN:
bRequestPending = FALSE;
// Clear the soft trigger
bTriggerFlag = FALSE;
SCAN_SetSoftTrigger(hScanner,&bTriggerFlag);
// Get scan result from the scan buffer, and display it
lpScanBuf = (LPSCAN_BUFFER)lParam;
if ( lpScanBuf == NULL )
ErrorExit(hwnd, IDS_ERR_BUF, 0);
hctl_data = GetDlgItem(hwnd,IDC_EDIT_DATA);
hctl_length = GetDlgItem(hwnd,IDC_EDIT_LEN);
hctl_type = GetDlgItem(hwnd,IDC_EDIT_TYPE);
switch (SCNBUF_GETSTAT(lpScanBuf))
{
case E_SCN_DEVICEFAILURE:
Edit_SetText(hctl_data, LoadMsg(IDS_DEVICE_FAILURE,
szMsgBuf, countof(szMsgBuf)));
break;
case E_SCN_READPENDING:
Edit_SetText(hctl_data, LoadMsg(IDS_READ_PENDING,
szMsgBuf, countof(szMsgBuf)));
break;
case E_SCN_READCANCELLED:
if (bStopScanning)
{ // complete the second step of UM_STOPSCANNING
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
return TRUE;
}
if (!GetFocus())
break; // Do nothing if read was cancelled while deactivation
Edit_SetText(hctl_data, LoadMsg(IDS_READ_CANCELLED,
szMsgBuf, countof(szMsgBuf)));
break;
case E_SCN_SUCCESS:
Edit_SetText(hctl_data, (LPTSTR)SCNBUF_GETDATA(lpScanBuffer));
// Format label type as a hex constant for display
wsprintf(szLabelType, TEXT("0x%.2X"), SCNBUF_GETLBLTYP(lpScanBuf));
Edit_SetText(hctl_type, szLabelType);
wsprintf(szLen, TEXT("%d"), SCNBUF_GETLEN(lpScanBuf));
Edit_SetText(hctl_length, szLen);
break;
}
// Submit next read request if we are foreground
if (GetFocus())
{
dwResult = SCAN_ReadLabelMsg(hScanner,
lpScanBuffer,
hwnd,
uMsg,
dwScanTimeout,
NULL);
if ( dwResult != E_SCN_SUCCESS )
ErrorExit(hwnd, IDS_FAILURE, TEXT("SCAN_ReadLabelMsg"));
else
bRequestPending = TRUE;
}
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_BUTTON_SOFTTRIGGER:
// Clear the state first before we set it to TRUE
bTriggerFlag = FALSE;
SCAN_SetSoftTrigger(hScanner,&bTriggerFlag);
bTriggerFlag = TRUE;
SCAN_SetSoftTrigger(hScanner,&bTriggerFlag);
break;
case IDOK: // fall through
case IDCANCEL:
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
break;
}
return TRUE;
}
return FALSE;
}
//----------------------------------------------------------------------------
//
// FUNCTION: ErrorExit(HWND, UINT, LPTSTR)
//
// PURPOSE: Handle critical errors and exit dialog.
//
// PARAMETERS:
// hwnd - handle to the dialog box.
// uID - ID of the message string to be displayed
// szFunc - function name if it's an API function failure
//
// RETURN VALUE:
// None.
//
//----------------------------------------------------------------------------
void ErrorExit(HWND hwnd, UINT uID, LPTSTR szFunc)
{
TCHAR szMsg[256];
TCHAR szBuf[256];
if (szFunc == NULL)
wcscpy(szMsg, LoadMsg(uID, szBuf, countof(szBuf)));
else
wsprintf(szMsg, TEXT("%s %s"), szFunc,
LoadMsg(uID, szBuf, countof(szBuf)));
MessageBox(NULL, szMsg, NULL, MB_OK);
SendMessage(hwnd,UM_STOPSCANNING,0,0L);
}
//----------------------------------------------------------------------------
//
// FUNCTION: LoadMsg(UINT, LPTSTR, int)
//
// PURPOSE: Load a message string for the string table
//
// PARAMETERS:
// uID - ID of the message string to be loaded
// lpBuffer - buffer to hold the message string
// nBufSize - size of lpBuffer
//
// RETURN VALUE:
// (LPTSTR) pointer to lpBuffer
//
//----------------------------------------------------------------------------
LPTSTR LoadMsg(UINT uID, LPTSTR lpBuffer, int nBufSize)
{
if (!LoadString(hInst, uID, lpBuffer, nBufSize))
wcscpy(lpBuffer, TEXT(""));
return lpBuffer;
}