From 729c26191771cfcd8f83f1ee079a545b8235900e Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Tue, 8 Aug 2023 16:48:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=8A=A0=E8=BD=BDtwainui?= =?UTF-8?q?=E5=BA=93=E6=96=B9=E5=BC=8F=EF=BC=8C=E4=B8=8A=E4=B8=80=E6=AC=A1?= =?UTF-8?q?=E6=9B=B4=E6=94=B9=E8=A7=A3=E5=86=B3=E4=B8=8D=E4=BA=86=E5=8D=B8?= =?UTF-8?q?=E8=BD=BD=E5=BA=93=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=8C=E8=80=8C?= =?UTF-8?q?=E6=98=AF=E5=8A=A0=E8=BD=BD=E5=BA=93=E5=A4=B1=E8=B4=A5=20:(?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sane/scanner.cpp | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) 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));