From 09916baf4459fb476bbb37d02b95508b9c6fcf18 Mon Sep 17 00:00:00 2001 From: gb <741021719@qq.com> Date: Thu, 8 Sep 2022 15:51:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DOEM=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E6=9F=A5=E6=89=BE=E9=94=99=E8=AF=AF=EF=BC=9B=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=A4=B1=E8=B4=A5=E6=97=B6=E5=BC=B9=E5=87=BA?= =?UTF-8?q?=E6=8F=90=E7=A4=BA=E5=AF=B9=E8=AF=9D=E6=A1=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- twain/load_sane.cpp | 136 +++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 64 deletions(-) diff --git a/twain/load_sane.cpp b/twain/load_sane.cpp index 8140a3c..0b61d8a 100644 --- a/twain/load_sane.cpp +++ b/twain/load_sane.cpp @@ -1,6 +1,7 @@ #include "pch.h" #include "load_sane.h" #include +#include "../../sdk/include/huagao/brand.h" namespace load_sane_util { @@ -11,69 +12,6 @@ namespace load_sane_util static int(__stdcall* init)(void*) = NULL; static int(__stdcall* uninit)(void*) = NULL; - static std::wstring reg_read(HKEY root, const wchar_t* path, const wchar_t* name) - { - HKEY key = NULL; - - RegOpenKeyW(root, path, &key); - if (!key) - return L""; - - wchar_t* buf = NULL; - DWORD len = 0; - DWORD type = REG_SZ; - std::wstring ret(L""); - - RegQueryValueExW(key, name, NULL, &type, (LPBYTE)buf, &len); - if (len) - { - buf = new wchar_t[len + 4]; - memset(buf, 0, (len + 4) * sizeof(*buf)); - RegQueryValueExW(key, name, NULL, &type, (LPBYTE)buf, &len); - ret = buf; - delete[] buf; - } - RegCloseKey(key); - - return ret; - } - static std::wstring reg_get_app_installing_path(void) - { -#ifdef OEM_HANWANG - std::wstring path(L"SOFTWARE\\HanvonScan"); -#elif defined(OEM_LISICHENG) - std::wstring path(L"SOFTWARE\\LanxumScan"); -#else - std::wstring path(L"SOFTWARE\\HuaGoScan"); -#endif - - return reg_read(HKEY_LOCAL_MACHINE, path.c_str(), L"AppDirectory"); - } - static int load_dll(const wchar_t* path_dll, HMODULE* dll) - { - HMODULE h = LoadLibraryW(path_dll); - int ret = GetLastError(); - - if (!h && ret == ERROR_MOD_NOT_FOUND) - { - std::wstring dir(path_dll); - size_t pos = dir.rfind(L'\\'); - wchar_t path[MAX_PATH] = { 0 }; - - GetCurrentDirectoryW(_countof(path) - 1, path); - if (pos != std::wstring::npos) - dir.erase(pos); - SetCurrentDirectoryW(dir.c_str()); - h = LoadLibraryW(path_dll); - ret = GetLastError(); - SetCurrentDirectoryW(path); - } - - if (dll) - *dll = h; - - return ret; - } static std::string u2m(const wchar_t* u, int page) { char* ansi = NULL; @@ -104,11 +42,78 @@ namespace load_sane_util return u; } + static std::wstring reg_read(HKEY root, const wchar_t* path, const wchar_t* name) + { + HKEY key = NULL; + + RegOpenKeyW(root, path, &key); + if (!key) + return L""; + + wchar_t* buf = NULL; + DWORD len = 0; + DWORD type = REG_SZ; + std::wstring ret(L""); + + RegQueryValueExW(key, name, NULL, &type, (LPBYTE)buf, &len); + if (len) + { + buf = new wchar_t[len + 4]; + memset(buf, 0, (len + 4) * sizeof(*buf)); + RegQueryValueExW(key, name, NULL, &type, (LPBYTE)buf, &len); + ret = buf; + delete[] buf; + } + RegCloseKey(key); + + return ret; + } + static std::wstring reg_get_app_installing_path(std::wstring* rp = NULL) + { + std::wstring path(m2u(PRODUCT_VENDOR, CP_ACP)), key(L"DriverPath"); + + path.insert(0, L"Software\\"); + path += L"Scan"; + + if (sizeof(void*) != 4) + key += L"64"; + + if (rp) + *rp = path + L"\\" + key; + + return reg_read(HKEY_LOCAL_MACHINE, path.c_str(), key.c_str()); + } + static int load_dll(const wchar_t* path_dll, HMODULE* dll) + { + HMODULE h = LoadLibraryW(path_dll); + int ret = GetLastError(); + + if (!h && ret == ERROR_MOD_NOT_FOUND) + { + std::wstring dir(path_dll); + size_t pos = dir.rfind(L'\\'); + wchar_t path[MAX_PATH] = { 0 }; + + GetCurrentDirectoryW(_countof(path) - 1, path); + if (pos != std::wstring::npos) + dir.erase(pos); + SetCurrentDirectoryW(dir.c_str()); + h = LoadLibraryW(path_dll); + ret = GetLastError(); + SetCurrentDirectoryW(path); + } + + if (dll) + *dll = h; + + return ret; + } bool initialize(HMODULE me) { + std::wstring reg_path(L""); bool ret = false; - sane_path = reg_get_app_installing_path(); + sane_path = reg_get_app_installing_path(®_path); if (!sane_path.empty()) { sane_path += L"\\sane.dll"; @@ -125,6 +130,9 @@ namespace load_sane_util } } + if (!ret) + MessageBoxW(NULL, (reg_path + L": " + sane_path).c_str(), L"Load scanner driver failed:", MB_OK); + return ret; } bool is_ok(void)