summaryrefslogtreecommitdiff
path: root/plugins/AddToStartMenu/src/main.cpp
blob: 6b3a86ebaa1e425d58c9cef737fc9b8634f0dd1b (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "stdafx.h"

using namespace Microsoft::WRL;

int hLangpack;
HINSTANCE g_hInst;

PLUGININFOEX pluginInfo = 
{
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__AUTHOREMAIL,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {61D5BB60-2249-4E3D-B23E-8FB86F04ED40}
	{ 0x61d5bb60, 0x2249, 0x4e3d, { 0xb2, 0x3e, 0x8f, 0xb8, 0x6f, 0x4, 0xed, 0x40 } }

};

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD, LPVOID)
{
	g_hInst = hInstance;
	return TRUE;
}

extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD)
{
	if (IsWinVer7Plus())
		return &pluginInfo;
	return NULL;
}

extern "C" int __declspec(dllexport) Load(void)
{
	mir_getLP(&pluginInfo);

	CreateServiceFunction(MODULENAME "/Add", Service);

	if (!ShortcutExists())
	{
		CMenuItem mi;
		mi.position = -0x7FFFFFFF;
		//mi.hIcolibItem = icon.hIcolib;
		mi.name.a = LPGEN("Add to start menu");
		mi.pszService = MODULENAME "/Add";
		Menu_AddMainMenuItem(&mi);
	}

	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	return 0;
}

INT_PTR Service(WPARAM, LPARAM)
{
	return (INT_PTR)TryCreateShortcut();
}

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_FALSE;
}

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;
}