修复OEM路径查找错误;驱动加载失败时弹出提示对话框

This commit is contained in:
gb 2022-09-08 15:51:02 +08:00
parent 5fa182c891
commit 09916baf44
1 changed files with 72 additions and 64 deletions

View File

@ -1,6 +1,7 @@
#include "pch.h"
#include "load_sane.h"
#include <string>
#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(&reg_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)