Sandboxie/SandboxiePlus/SandMan/Helpers/WinHelper.cpp

92 lines
2.8 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-01-29 16:10:11 +00:00
QVariantMap ResolveShortcut(const QString& LinkPath)
{
QVariantMap Link;
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);
if (FAILED(hRes))
return Link;
Link["Path"] = QString::fromWCharArray(szPath);
hRes = psl->GetArguments(szPath, ARRAYSIZE(szPath));
if (FAILED(hRes))
return Link;
Link["Arguments"] = QString::fromWCharArray(szPath);
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;
}