// Modified scavino.org version
#define VERSION 110 // version number, times 100.
#include <windows.h>
#include <assert.h>
#include <memory.h> // memset, memcpy
#include <vfw.h>
#include "ijl.h"
#include "commdlg.h"
#include "twain.h"
#include "jtwain.h"
//------------ Constants and Macros
#define STATIC static
#define INT32 int
#define DATAGROUP unsigned
#define DSM_FILENAME "TWAIN_32.DLL"
#define DSM_ENTRYPOINT "DSM_Entry"
#define IsValidHandle(h) (h!=NULL)
#define HUGEWRITE(fh, pb, bc) ((INT32)_lwrite(fh, pb, bc))
#define FMEMSET(p, v, n) memset(p, v, n);
#define FMEMCPY(p, q, n) memcpy(p, q, n);
typedef enum {
ED_NONE,
ED_START_TRIPLET_ERRS,
ED_CAP_GET, // MSG_GET triplet on a capability failed
ED_CAP_SET, // MSG_SET triplet on capability failed
ED_DSM_FAILURE, // TWAIN DSM returned TWRC_FAILURE
ED_DS_FAILURE, // source returned TWRC_FAILURE
ED_END_TRIPLET_ERRS,
ED_NOT_STATE_4, // operation invoked in wrong state
ED_NULL_HCON, // MSG_GET returned a null container handle
ED_BAD_HCON, // MSG_GET returned an invalid/unlockable container handle
ED_BAD_CONTYPE, // returned container ConType is not valid.
ED_BAD_ITEMTYPE, // returned container ItemType is not valid.
ED_CAP_GET_EMPTY, // returned container has 0 items.
ED_CAP_SET_EMPTY, // trying to restrict a cap to empty set
} ErrorDetail;
const char *pszErrDescrip[] =
{ "[no details available]",
"",
"DAT_CAPABILITY/MSG_GET failed",
"DAT_CAPABILITY/MSG_SET failed",
"Source Manager operation failed",
"DataSource operation failed",
"",
"TWAIN session not in State 4 (Source Open)",
"MSG_GET returned a NULL container handle",
"MSG_GET returned an invalid container handle",
"Returned container is not valid type",
"Returned container has invalid ItemType",
"Returned container is empty",
"App and source found NO values in common",
};
const char *pszRC[] = {
"TWRC_SUCCESS",
"TWRC_FAILURE",
"TWRC_CHECKSTATUS ('tried hard')",
"TWRC_CANCEL",
"TWRC_DSEVENT",
"TWRC_NOTDSEVENT",
"TWRC_XFERDONE",
"TWRC_ENDOFLIST",
"TWRC_INFONOTSUPPORTED",
"TWRC_DATANOTAVAILABLE"
};
const char *pszCC[] = {
"TWCC_SUCCESS",
"TWCC_BUMMER (Failure due to unknown causes)",
"TWCC_LOWMEMORY",
"TWCC_NODS (No Data Source)",
"TWCC_MAXCONNECTIONS (DS is connected to max possible apps)",
"TWCC_OPERATIONERROR (DS/DSM reported error, app shouldn't)",
"TWCC_BADCAP (Unknown capability)",
"7 (undefined)",
"8 (undefined)",
"TWCC_BADPROTOCOL (Unrecognized triplet)",
"TWCC_BADVALUE (Data parameter out of range)",
"TWCC_SEQERROR (Triplet out of sequence)",
"TWCC_BADDEST (Unknown dest. App/Src in DSM_Entry)",
"TWCC_CAPUNSUPPORTED (Cap not supported by source)",
"TWCC_CAPBADOPERATION (Operation not supported by cap)",
"TWCC_CAPSEQERROR (Cap has dependancy on other cap)",
};
const char szInsuffMem[] = "Insufficient Memory"; // error message
#define WM_AUTOSCAN 0x7fa0 // semi-random number
// For the JPEG Support
#define Lock(hdib) ((LPBITMAPINFOHEADER)GlobalLock(hdib))
#define Unlock(hdib) GlobalUnlock(hdib)
// Make this Dynamic
static int nJpegQuality = 75;
// End - Intel JPEG Library globals
//------------ Global variables
STATIC int iAvailable; // TWAIN available: 0:unknown, -1:No, 1:Yes
STATIC int nState = 1; // TWAIN state (per the standard)
STATIC int nErrDetail; // detailed error code
STATIC unsigned nErrRC, nErrCC; // result code and condition code for last error
STATIC char szMsg[256]; // scratch buffer for messages
STATIC DSMENTRYPROC pDSM_Entry; // entry point of Data Source Manager (TWAIN.DLL)
// Modified by Hugo Scavino
//STATIC HANDLE hDSMLib; // handle of DSM
STATIC HMODULE hDSMLib; // handle of DSM
STATIC TW_IDENTITY AppId = { // application identity structure
0, // Id, filled in by DSM
{ 1, 0, TWLG_USA, TWCY_USA, "Unspecified Version"}, // Version
TWON_PROTOCOLMAJOR,
TWON_PROTOCOLMINOR,
DG_IMAGE | DG_CONTROL,
"TWAIN Mfg", // Mfg
"TWAIN Application Family", // Family
"JTWAIN Client Product" // Product
};
STATIC TW_IDENTITY SourceId; // source identity structure
STATIC BOOL bHideUI; // allow source u/i to be hidden
STATIC TW_USERINTERFACE twUI;
STATIC TW_PENDINGXFERS pendingXfers;
STATIC BOOL bBreakModalLoop; // flag to end modal event loop
STATIC unsigned nMemBuffer; // memory transfer buffer counter
STATIC HANDLE hDib; // bitmap returned by native transfer
STATIC TW_INT16 rc; // result code
STATIC HINSTANCE hinstLib; // instance handle for this DLL
STATIC HWND hwndProxy; // proxy window
//------------ Forward declarations
void WINAPI TWAIN_WaitForXfer(HWND hwnd);
static void SetState(int n);
// RMOVED BY SCAVINO
//static HWND GetValidHwnd(HWND hwnd);
static HWND CreateProxyWindow(void);
static TW_UINT32 Intersect(TW_UINT32 wMask, TW_UINT32 nItems, TW_UINT16 far *pItem);
static unsigned FindIndex16(TW_UINT32 nItems, TW_UINT16 far *plist, TW_UINT16 uVal, unsigned nDefault);
static int RecordError(ErrorDetail ed);
static double Fix32ToFloat(TW_FIX32 fix);
static void NativeXferHandler(void);
static void FileXferHandler(void);
static void MemoryXferHandler(void);
static void DoOneTransfer(void);
//------------ Public functions
BOOL WINAPI DllMain(HINSTANCE module,
ULONG ulEvent,
LPVOID lpReserved)
{
// ignore warning
lpReserved = lpReserved;
module = module;
switch (ulEvent) {
case DLL_PROCESS_DETACH:
//TWAIN_UnloadSourceManager();
break;
case DLL_PROCESS_ATTACH:
#ifdef DEBUG
wsprintf(szMsg, "sizeof (TW_IDENTITY) == %d\n", sizeof (TW_IDENTITY));
OutputDebugString(szMsg);
#endif
assert(sizeof (TW_IDENTITY) == 156);
break;
default:
break;
}
return TRUE;
}
//-- Record application information
void WINAPI TWAIN_RegisterApp(
int nMajorNum, // major and incremental revision of application.
int nMinorNum, // e.g. version 4.5: nMajorNum = 4, nMinorNum = 5
int nLanguage, // (human) language (use TWLG_xxx from TWAIN.H)
int nCountry, // country (use TWCY_xxx from TWAIN.H)
LPSTR lpszVersion, // version info string e.g. "1.0b3 Beta release"
LPSTR lpszMfg, // name of mfg/developer e.g. "Crazbat Software"
LPSTR lpszFamily, // product family e.g. "BitStomper"
LPSTR lpszProduct) // specific product e.g. "BitStomper Deluxe Pro"
{
AppId.Id = 0; // init to 0, but Source Manager will assign real value
AppId.Version.MajorNum = (TW_UINT16)nMajorNum;
AppId.Version.MinorNum = (TW_UINT16)nMinorNum;
AppId.Version.Language = (TW_UINT16)nLanguage;
AppId.Version.Country = (TW_UINT16)nCountry;
lstrcpy (AppId.Version.Info, lpszVersion);
AppId.ProtocolMajor = TWON_PROTOCOLMAJOR;
AppId.ProtocolMinor = TWON_PROTOCOLMINOR;
AppId.SupportedGroups = DG_IMAGE | DG_CONTROL;
lstrcpy (AppId.Manufacturer, lpszMfg);
lstrcpy (AppId.ProductFamily, lpszFamily);
lstrcpy (AppId.ProductName, lpszProduct);
} // TWAIN_RegisterApp
int WINAPI TWAIN_SelectImageSource(HWND hwnd)
{
int fSuccess = FALSE;
int nEntryState = nState;
if (nState >= TWAIN_SM_OPEN || TWAIN_OpenSourceManager(hwnd)) {
TW_IDENTITY NewSourceId;
FMEMSET(&NewSourceId, 0, sizeof NewSourceId);
// Post the Select Source dialog
fSuccess = TWAIN_Mgr(DG_CONTROL, DAT_IDENTITY, MSG_USERSELECT, &NewSourceId);
} else {
TWAIN_ErrorBox("Unable to open Source Manager (" DSM_FILENAME ")");
}
if (nEntryState < TWAIN_SM_OPEN) {
TWAIN_CloseSourceManager(hwnd);
if (nEntryState < TWAIN_SM_LOADED) {
TWAIN_UnloadSourceManag