TWAIN Error Handling
Posted: Wed Jul 27, 2016 3:02 pm
Greetings,
I am having a problem trying to use the TWAIN functionality provided as part of the SDK. The issue I'm having is my call to SXC_InitTwain is returning failure, however I cannot for the life of me work out how to get the cause of the error out of the HRESULT. Would you be able to provide me with any assistance?
I've attached my code below, note it is still a work in progress - since I'm just trying to get SXC_InitTwain to work before I can do any further work.
Cheers, Stew
I am having a problem trying to use the TWAIN functionality provided as part of the SDK. The issue I'm having is my call to SXC_InitTwain is returning failure, however I cannot for the life of me work out how to get the cause of the error out of the HRESULT. Would you be able to provide me with any assistance?
I've attached my code below, note it is still a work in progress - since I'm just trying to get SXC_InitTwain to work before I can do any further work.
Cheers, Stew
Code: Select all
bool scanner::perform_scan_twain(std::vector<std::string>& files)
{
typedef TwainObject(FAR PASCAL createTwainObject)(LPCSTR, LPCSTR);
typedef VOID(FAR PASCAL deleteTwainObject)(TwainObject);
typedef HRESULT(FAR PASCAL initTwain)(TwainObject);
typedef HRESULT(FAR PASCAL setProc)(TwainObject, pGetData , DWORD_PTR );
typedef HRESULT(FAR PASCAL selectSourceGUI)(TwainObject);
typedef HRESULT(FAR PASCAL openSource)(TwainObject);
typedef HRESULT(FAR PASCAL enableSource)(TwainObject, HWND, BOOL);
const raii_loadlibrary libraryloader("xcscan40.dll", "xcscan40.dll");
auto lib_inst = libraryloader.get_hinstance();
auto fn_createTwainObject = (createTwainObject FAR*)GetProcAddress(lib_inst, "SXC_CreateTwainObject");
auto fn_deleteTwainObject = (deleteTwainObject FAR*)GetProcAddress(lib_inst, "SXC_DeleteTwainObject");
auto fn_initTwain = (initTwain FAR*)GetProcAddress(lib_inst, "SXC_InitTwain");
auto fn_setProc = (setProc FAR*)GetProcAddress(lib_inst, "SXC_SetProc");
auto fn_selectSourceGUI = (selectSourceGUI FAR*)GetProcAddress(lib_inst, "SXC_SelectSourceGUI");
auto fn_openSource = (openSource FAR*)GetProcAddress(lib_inst, "SXC_OpenSource");
auto fn_enableSource = (enableSource FAR*)GetProcAddress(lib_inst, "SXC_EnableSource");
TwainObject twain_obj = fn_createTwainObject("<private>", "<private>");
if (!twain_obj)
throw std::exception("Unable to create TWAIN object.");
// Ensure the twain_obj is deleted when we exit scope.
auto cleanup_twain = gsl::finally([&twain_obj, &fn_deleteTwainObject]() {fn_deleteTwainObject(twain_obj); });
HRESULT hr = fn_initTwain(twain_obj);
if (IS_DS_FAILED(hr))
throw std::exception("Unable to initialise TWAIN.");
if (IS_DS_FAILED(fn_setProc(twain_obj, [](DWORD_PTR u, const BYTE* b, DWORD s) { return reinterpret_cast<scanner*>(u)->get_data_callback(b, s); }, reinterpret_cast<DWORD_PTR>(this))))
throw std::exception("Failed to set data-retrieval procedure.");
if (IS_DS_FAILED(fn_selectSourceGUI(twain_obj)))
throw std::exception("Unable to select source GUI.");
if (IS_DS_FAILED(fn_openSource(twain_obj)))
throw std::exception("Unable to open source.");
if (IS_DS_FAILED(fn_enableSource(twain_obj, wxo_win_mainhwnd, true)))
throw std::exception("Unable to enable source.");
return true;
}