调整加载twainui库方式,上一次更改解决不了卸载库的问题,而是加载库失败 :(

This commit is contained in:
gb 2023-08-08 16:48:50 +08:00
parent c13456a44d
commit 729c261917
1 changed files with 36 additions and 1 deletions

View File

@ -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));