summaryrefslogtreecommitdiff
path: root/plugins/Toaster/src/add_to_start_menu.cpp
blob: ae2d1e7131ad7382f0ae77706ff8fd5206cc30d2 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "stdafx.h"

using namespace Microsoft::WRL;

wchar_t* GetShortcutPath()
{
	wchar_t shortcutPath[MAX_PATH];
	GetEnvironmentVariable(_T("APPDATA"), shortcutPath, MAX_PATH);
	wcscat_s(shortcutPath, ARRAYSIZE(shortcutPath), L"\\Microsoft\\Windows\\Start Menu\\Programs\\Miranda NG.lnk");

	return mir_wstrdup(shortcutPath);
}

HRESULT ShortcutExists()
{
	HRESULT hr;
	DWORD attributes = GetFileAttributes(ptrW(GetShortcutPath()));
	bool fileExists = attributes < 0xFFFFFFF;

	if (!fileExists)
	{
		hr = S_OK;
	}
	else
	{
		hr = S_FALSE;
	}
	return hr;
}

HRESULT TryCreateShortcut()
{
	if (!ShortcutExists())
		return InstallShortcut(ptrW(GetShortcutPath()));
	return S_OK;
}

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

	ComPtr<IShellLink> shellLink;
	HRESULT hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink));
	if (SUCCEEDED(hr))
	{
		hr = shellLink->SetPath(exePath);
		if (SUCCEEDED(hr))
		{
			hr = shellLink->SetArguments(L"");
			if (SUCCEEDED(hr))
			{
				ComPtr<IPropertyStore> propertyStore;
				hr = shellLink.As(&propertyStore);
				if (SUCCEEDED(hr))
				{
					PROPVARIANT appIdPropVar;
					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;
}