summaryrefslogtreecommitdiff
path: root/plugins/Toaster/src/utils.cpp
blob: 86062d2f4a9b400dbe7ccc778038c01902360809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "stdafx.h"

#define SHORTCUT_PATH "\\Microsoft\\Windows\\Start Menu\\Programs\\Miranda NG.lnk"

using namespace Microsoft::WRL;

wchar_t* GetShortcutPath()
{
	wchar_t path[MAX_PATH];
	GetEnvironmentVariable(_T("APPDATA"), path, MAX_PATH);
	wcscat_s(path, _T(SHORTCUT_PATH));
	return mir_wstrdup(path);
}

HRESULT InstallShortcut(_In_z_ wchar_t *shortcutPath)
{
	wchar_t exePath[MAX_PATH];
	GetModuleFileName(NULL, exePath, MAX_PATH);

	ComPtr<IShellLink> shellLink;
	CHECKHR(CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink)));

	CHECKHR(shellLink->SetPath(exePath));

	CHECKHR(shellLink->SetArguments(L""));

	ComPtr<IPropertyStore> propertyStore;
	CHECKHR(shellLink.As(&propertyStore));

	PROPVARIANT appIdPropVar;
	HRESULT hr = InitPropVariantFromString(AppUserModelID, &appIdPropVar);
	if (SUCCEEDED(hr))
	{
		hr = propertyStore->SetValue(PKEY_AppUserModel_ID, appIdPropVar);
		if (SUCCEEDED(hr))
		{
			hr = propertyStore->Commit();
			if (SUCCEEDED(hr))
			{
				ComPtr<IPersistFile> persistFile;
				hr = shellLink.As(&persistFile);
				if (SUCCEEDED(hr))
				{
					hr = persistFile->Save(shortcutPath, TRUE);
				}
			}
		}
		PropVariantClear(&appIdPropVar);
	}
	return hr;
}

bool ShortcutExists()
{
	return (GetFileAttributes(ptrW(GetShortcutPath())) < 0xFFFFFFF);
}

HRESULT TryCreateShortcut()
{
	return (ShortcutExists() ? S_OK : InstallShortcut(ptrW(GetShortcutPath())));
}