diff --git a/sane/scanner.cpp b/sane/scanner.cpp index 90df8ae..e83802f 100644 --- a/sane/scanner.cpp +++ b/sane/scanner.cpp @@ -357,17 +357,52 @@ namespace callback void (*twain_ui_free)(void* buf) = NULL; + static int load_dll(const char* path_dll, HMODULE* dll) + { + HMODULE h = LoadLibraryA(path_dll); + int ret = GetLastError(); + char info[128] = { 0 }; + + sprintf_s(info, _countof(info) - 1, " = %d\r\n", ret); + OutputDebugStringA(("[TWAIN]Load: " + std::string(path_dll) + info).c_str()); + if (!h && (ret == ERROR_MOD_NOT_FOUND || ret == ERROR_BAD_EXE_FORMAT || ret == 0)) + { + std::string dir(path_dll); + size_t pos = dir.rfind(L'\\'); + wchar_t path[MAX_PATH] = { 0 }; + + GetDllDirectoryW(_countof(path) - 1, path); + if (pos != std::wstring::npos) + dir.erase(pos); + OutputDebugStringA(("[TWAIN]Load: try change directory to " + dir + "\r\n").c_str()); + SetDllDirectoryA(dir.c_str()); + h = LoadLibraryA(path_dll); + // h = LoadLibraryExW(path_dll, NULL, LOAD_WITH_ALTERED_SEARCH_PATH); + ret = GetLastError(); + sprintf_s(info, _countof(info) - 1, " = %d\r\n", ret); + OutputDebugStringA(("[TWAIN]Load: " + std::string(path_dll) + info).c_str()); + OutputDebugStringW((L"[TWAIN]Load: restore directory to " + std::wstring(path) + L"\r\n").c_str()); + SetDllDirectoryW(path); + } + + if (dll) + *dll = h; + + return ret; + } static void init_ui(void) { std::string root(hg_sane_middleware::sane_path()); if (hui) FreeLibrary(hui); + hui = NULL; root += OEM_SHORT_NAME_E; root += "TwainUI.dll"; // hui = LoadLibraryExA(root.c_str(), NULL, LOAD_WITH_ALTERED_SEARCH_PATH); - hui = LoadLibraryA(root.c_str()); + //hui = LoadLibraryA(root.c_str()); + load_dll(root.c_str(), &hui); if (!hui) { std::wstring info(L"Load '" + local_trans::a2u(root.c_str(), CP_UTF8));