mirror of
https://github.com/QL-Win/QuickLook.git
synced 2025-09-12 10:19:07 +00:00
Use 32bit helper to interact between 32bit processes
This commit is contained in:
131
QuickLook.Native/QuickLook.Native32/DialogHook.cpp
Normal file
131
QuickLook.Native/QuickLook.Native32/DialogHook.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
#include "stdafx.h"
|
||||
#include "DialogHook.h"
|
||||
#include "WoW64HookHelper.h"
|
||||
#include "HelperMethods.h"
|
||||
|
||||
|
||||
#pragma comment(linker, "/SECTION:.shared,RWS")
|
||||
#pragma data_seg(".shared")
|
||||
|
||||
static HHOOK ghHook = nullptr; // Hook handle
|
||||
static UINT WM_HOOK_NOTIFY = 0;
|
||||
static WCHAR filePathBuffer[MAX_PATH] = {'\0'};
|
||||
|
||||
#pragma data_seg()
|
||||
|
||||
#define SHARED_MEM_NAME L"QUICKLOOK_WOW64HOOKHELPER_MEM"
|
||||
|
||||
void DialogHook::GetSelectedFromCommonDialog(PWCHAR buffer)
|
||||
{
|
||||
auto hwndfg = GetForegroundWindow();
|
||||
DWORD pid = 0;
|
||||
auto tid = GetWindowThreadProcessId(hwndfg, &pid);
|
||||
|
||||
auto hProc = OpenProcess(PROCESS_QUERY_INFORMATION, false, pid);
|
||||
if (hProc == nullptr)
|
||||
return;
|
||||
|
||||
if (WM_HOOK_NOTIFY == 0)
|
||||
WM_HOOK_NOTIFY = RegisterWindowMessage(L"WM_QUICKLOOK_HOOK_NOTIFY_MSG");
|
||||
|
||||
BOOL isTargetWoW64 = false;
|
||||
if (GetProcAddress(GetModuleHandle(L"kernel32.dll"), "IsWow64Process") && !IsWow64Process(hProc, &isTargetWoW64))
|
||||
return;
|
||||
|
||||
BOOL isSelfWoW64 = false;
|
||||
if (GetProcAddress(GetModuleHandle(L"kernel32.dll"), "IsWow64Process") && !IsWow64Process(GetCurrentProcess(), &isSelfWoW64))
|
||||
return;
|
||||
|
||||
if (isTargetWoW64 && !isSelfWoW64)
|
||||
{
|
||||
// if self is 64bit and target is 32bit, do this
|
||||
GetSelectedFromWoW64HookHelper(buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ghHook != nullptr)
|
||||
UnhookWindowsHookEx(ghHook);
|
||||
ghHook = SetWindowsHookEx(WH_CALLWNDPROC, static_cast<HOOKPROC>(MsgHookProc), ModuleFromAddress(MsgHookProc), tid);
|
||||
if (ghHook == nullptr)
|
||||
return;
|
||||
|
||||
SendMessage(hwndfg, WM_HOOK_NOTIFY, 0, 0);
|
||||
wcscpy_s(buffer, wcslen(buffer) - 1, filePathBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
void DialogHook::getSelectedInternal(CComPtr<IShellBrowser> psb, PWCHAR buffer)
|
||||
{
|
||||
CComPtr<IShellView> psv;
|
||||
if (FAILED(psb->QueryActiveShellView(&psv)))
|
||||
return;
|
||||
|
||||
CComPtr<IDataObject> dao;
|
||||
if (FAILED(psv->GetItemObject(SVGIO_SELECTION, IID_IDataObject, reinterpret_cast<void**>(&dao))))
|
||||
return;
|
||||
|
||||
return HelperMethods::ObtainFirstItem(dao, buffer);
|
||||
}
|
||||
|
||||
void DialogHook::GetSelectedFromWoW64HookHelper(PWCHAR buffer)
|
||||
{
|
||||
if (!WoW64HookHelper::CheckStatus())
|
||||
if (!WoW64HookHelper::Launch())
|
||||
return;
|
||||
|
||||
auto hHelperWnd = FindWindowEx(HWND_MESSAGE, nullptr, WoW64HookHelper::GetMsgWindowClassName(), nullptr);
|
||||
if (hHelperWnd == nullptr)
|
||||
return;
|
||||
|
||||
auto hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, MAX_PATH * sizeof WCHAR, SHARED_MEM_NAME);
|
||||
if (hMapFile == nullptr)
|
||||
return;
|
||||
|
||||
auto sharedBuffer = static_cast<PWCHAR>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, MAX_PATH * sizeof WCHAR));
|
||||
if (sharedBuffer == nullptr)
|
||||
{
|
||||
CloseHandle(hMapFile);
|
||||
return;
|
||||
}
|
||||
|
||||
SendMessage(hHelperWnd, WM_HOOK_NOTIFY, 0, 0);
|
||||
|
||||
// the sharedBuffer should now ready
|
||||
wcscpy_s(buffer, MAX_PATH, sharedBuffer);
|
||||
|
||||
UnmapViewOfFile(sharedBuffer);
|
||||
CloseHandle(hMapFile);
|
||||
}
|
||||
|
||||
HMODULE DialogHook::ModuleFromAddress(PVOID pv)
|
||||
{
|
||||
MEMORY_BASIC_INFORMATION mbi;
|
||||
return VirtualQuery(pv, &mbi, sizeof mbi) != 0 ? static_cast<HMODULE>(mbi.AllocationBase) : nullptr;
|
||||
}
|
||||
|
||||
LRESULT DialogHook::MsgHookProc(int nCode, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (nCode < 0)
|
||||
goto CONTINUE;
|
||||
|
||||
auto pMSG = reinterpret_cast<CWPSTRUCT*>(lParam);
|
||||
|
||||
// only response to WM_HOOK_NOTIFY
|
||||
if (pMSG->message != WM_HOOK_NOTIFY)
|
||||
goto CONTINUE;
|
||||
|
||||
UnhookWindowsHookEx(ghHook);
|
||||
ghHook = nullptr;
|
||||
|
||||
// get current selected item
|
||||
auto psb = reinterpret_cast<IShellBrowser*>(SendMessage(GetForegroundWindow(), WM_USER + 7, 0, 0));
|
||||
if (psb == nullptr)
|
||||
goto CONTINUE;
|
||||
|
||||
getSelectedInternal(psb, filePathBuffer);
|
||||
|
||||
return 0;
|
||||
|
||||
CONTINUE:
|
||||
return CallNextHookEx(ghHook, nCode, wParam, lParam);
|
||||
}
|
Reference in New Issue
Block a user