修复DLL加载路径问题(好分数)

This commit is contained in:
gb 2022-10-19 18:02:12 +08:00
parent 6deaffbb21
commit 4da3fca44b
1 changed files with 17 additions and 3 deletions

View File

@ -91,20 +91,27 @@ namespace load_sane_util
{ {
HMODULE h = LoadLibraryW(path_dll); HMODULE h = LoadLibraryW(path_dll);
int ret = GetLastError(); int ret = GetLastError();
wchar_t info[128] = { 0 };
swprintf_s(info, _countof(info) - 1, L" = %d\r\n", ret);
OutputDebugStringW((L"[TWAIN]Load: " + std::wstring(path_dll) + info).c_str());
if (!h && ret == ERROR_MOD_NOT_FOUND) if (!h && ret == ERROR_MOD_NOT_FOUND)
{ {
std::wstring dir(path_dll); std::wstring dir(path_dll);
size_t pos = dir.rfind(L'\\'); size_t pos = dir.rfind(L'\\');
wchar_t path[MAX_PATH] = { 0 }; wchar_t path[MAX_PATH] = { 0 };
GetCurrentDirectoryW(_countof(path) - 1, path); GetDllDirectoryW(_countof(path) - 1, path);
if (pos != std::wstring::npos) if (pos != std::wstring::npos)
dir.erase(pos); dir.erase(pos);
SetCurrentDirectoryW(dir.c_str()); OutputDebugStringW((L"[TWAIN]Load: try change directory to " + dir + L"\r\n").c_str());
SetDllDirectoryW(dir.c_str());
h = LoadLibraryW(path_dll); h = LoadLibraryW(path_dll);
ret = GetLastError(); ret = GetLastError();
SetCurrentDirectoryW(path); swprintf_s(info, _countof(info) - 1, L" = %d\r\n", ret);
OutputDebugStringW((L"[TWAIN]Load: " + std::wstring(path_dll) + info).c_str());
OutputDebugStringW((L"[TWAIN]Load: restore directory to " + std::wstring(path) + L"\r\n").c_str());
SetDllDirectoryW(path);
} }
if (dll) if (dll)
@ -142,6 +149,13 @@ namespace load_sane_util
} }
bool is_ok(void) bool is_ok(void)
{ {
wchar_t info[128] = { 0 };
swprintf_s(info, _countof(info) - 1, L"[TWAIN]Load: sane_inst: %s, is_on: %s, init: %s, uninit: %s\r\n"
, sane_inst != NULL ? "ok" : "not found"
, is_on != NULL ? "ok" : "not found"
, init != NULL ? "ok" : "not found"
, uninit != NULL ? "ok" : "not found");
return sane_inst != NULL && is_on != NULL && init != NULL && uninit != NULL; return sane_inst != NULL && is_on != NULL && init != NULL && uninit != NULL;
} }
bool is_online(SCANNERID guid) bool is_online(SCANNERID guid)