Sandboxie/SandboxiePlus/SandMan/Helpers/WinHelper.cpp

130 lines
4.2 KiB
C++
Raw Normal View History

2022-12-16 12:08:49 +00:00
#include "stdafx.h"
#include "WinHelper.h"
2022-12-16 12:32:48 +00:00
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QtWin>
#else
2022-12-16 12:08:49 +00:00
#include <windows.h>
2022-12-16 12:32:48 +00:00
#endif
2022-12-16 12:08:49 +00:00
#include <Shlwapi.h>
#include <Shlobj.h>
2023-08-12 08:15:17 +01:00
2023-01-29 16:10:11 +00:00
QVariantMap ResolveShortcut(const QString& LinkPath)
{
2023-08-12 08:15:17 +01:00
QVariantMap Link;
2023-01-29 16:10:11 +00:00
HRESULT hRes = E_FAIL;
IShellLink* psl = NULL;
// buffer that receives the null-terminated string
// for the drive and path
TCHAR szPath[0x1000];
// structure that receives the information about the shortcut
WIN32_FIND_DATA wfd;
// Get a pointer to the IShellLink interface
hRes = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&psl);
if (SUCCEEDED(hRes))
{
// Get a pointer to the IPersistFile interface
IPersistFile* ppf = NULL;
psl->QueryInterface(IID_IPersistFile, (void **) &ppf);
// Open the shortcut file and initialize it from its contents
hRes = ppf->Load(LinkPath.toStdWString().c_str(), STGM_READ);
if (SUCCEEDED(hRes))
{
hRes = psl->Resolve(NULL, SLR_NO_UI | SLR_NOSEARCH | SLR_NOUPDATE);
if (SUCCEEDED(hRes))
{
// Get the path to the shortcut target
hRes = psl->GetPath(szPath, ARRAYSIZE(szPath), &wfd, SLGP_RAWPATH);
2023-08-12 08:15:17 +01:00
if (hRes == S_OK)
Link["Path"] = QString::fromWCharArray(szPath);
else
2023-08-12 08:29:23 +01:00
{
PIDLIST_ABSOLUTE pidl;
hRes = psl->GetIDList(&pidl);
if (SUCCEEDED(hRes))
{
LPWSTR url = nullptr;
SHGetNameFromIDList(pidl, SIGDN_URL, &url);
if (url)
{
QUrl Url = QString::fromWCharArray(url);
if (Url.isLocalFile())
Link["Path"] = Url.path().mid(1).replace("/", "\\");
else
Link["Path"] = Url.toString();
CoTaskMemFree(url);
}
CoTaskMemFree(pidl);
}
}
2023-01-29 16:10:11 +00:00
hRes = psl->GetArguments(szPath, ARRAYSIZE(szPath));
2023-08-12 08:15:17 +01:00
if (!FAILED(hRes))
Link["Arguments"] = QString::fromWCharArray(szPath);
2023-01-29 16:10:11 +00:00
2023-05-27 16:12:22 +01:00
hRes = psl->GetWorkingDirectory(szPath, ARRAYSIZE(szPath));
if (!FAILED(hRes))
Link["WorkingDir"] = QString::fromWCharArray(szPath);
2023-01-29 16:10:11 +00:00
int IconIndex;
hRes = psl->GetIconLocation(szPath, ARRAYSIZE(szPath), &IconIndex);
if (FAILED(hRes))
return Link;
Link["IconPath"] = QString::fromWCharArray(szPath);
Link["IconIndex"] = IconIndex;
// Get the description of the target
hRes = psl->GetDescription(szPath, ARRAYSIZE(szPath));
if (FAILED(hRes))
return Link;
Link["Info"] = QString::fromWCharArray(szPath);
}
}
}
return Link;
}
2022-12-21 09:14:31 +00:00
QPixmap LoadWindowsIcon(const QString& Path, quint32 Index)
2022-12-16 12:08:49 +00:00
{
std::wstring path = QString(Path).replace("/", "\\").toStdWString();
HICON icon = ExtractIconW(NULL, path.c_str(), Index);
2022-12-16 12:32:48 +00:00
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
2022-12-21 09:14:31 +00:00
QPixmap Icon = QtWin::fromHICON(icon);
2022-12-16 12:32:48 +00:00
#else
2022-12-21 09:14:31 +00:00
QPixmap Icon = QPixmap::fromImage(QImage::fromHICON(icon));
2022-12-16 12:32:48 +00:00
#endif
2022-12-16 12:08:49 +00:00
DestroyIcon(icon);
return Icon;
}
bool PickWindowsIcon(QWidget* pParent, QString& Path, quint32& Index)
{
wchar_t iconPath[MAX_PATH] = { 0 };
Path.toWCharArray(iconPath);
BOOL Ret = PickIconDlg((HWND)pParent->window()->winId(), iconPath, MAX_PATH, (int*)&Index);
Path = QString::fromWCharArray(iconPath);
return !!Ret;
2024-04-20 13:32:15 +01:00
}
void ProtectWindow(void* hWnd)
{
typedef BOOL(*LPSETWINDOWDISPLAYAFFINITY)(HWND, DWORD);
static LPSETWINDOWDISPLAYAFFINITY pSetWindowDisplayAffinity = NULL;
if (!pSetWindowDisplayAffinity)
pSetWindowDisplayAffinity = (LPSETWINDOWDISPLAYAFFINITY)GetProcAddress(LoadLibraryA("user32.dll"), "SetWindowDisplayAffinity");
if (pSetWindowDisplayAffinity)
pSetWindowDisplayAffinity((HWND)hWnd, 0x00000011);
2022-12-16 12:08:49 +00:00
}