From d87f9ff6f923ae344ffe00465e97e71a001de2df Mon Sep 17 00:00:00 2001 From: MikalaiR Date: Fri, 28 Aug 2015 12:07:44 +0000 Subject: AddToStartMenu: initial commit git-svn-id: http://svn.miranda-ng.org/main/trunk@15054 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/AddToStartMenu/src/main.cpp | 141 ++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 plugins/AddToStartMenu/src/main.cpp (limited to 'plugins/AddToStartMenu/src/main.cpp') diff --git a/plugins/AddToStartMenu/src/main.cpp b/plugins/AddToStartMenu/src/main.cpp new file mode 100644 index 0000000000..66f2b8e30e --- /dev/null +++ b/plugins/AddToStartMenu/src/main.cpp @@ -0,0 +1,141 @@ +#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) +{ + return &pluginInfo; +} + +extern "C" int __declspec(dllexport) Load(void) +{ + mir_getLP(&pluginInfo); + + CreateServiceFunction(MODULENAME "/Add", Service); + + 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(); +} + +HRESULT TryCreateShortcut() +{ + wchar_t shortcutPath[MAX_PATH]; + DWORD charWritten = GetEnvironmentVariable(L"APPDATA", shortcutPath, MAX_PATH); + HRESULT hr = charWritten > 0 ? S_OK : E_INVALIDARG; + + if (SUCCEEDED(hr)) + { + errno_t concatError = wcscat_s(shortcutPath, ARRAYSIZE(shortcutPath), L"\\Microsoft\\Windows\\Start Menu\\Programs\\Miranda NG.lnk"); + hr = concatError == 0 ? S_OK : E_INVALIDARG; + if (SUCCEEDED(hr)) + { + DWORD attributes = GetFileAttributes(shortcutPath); + bool fileExists = attributes < 0xFFFFFFF; + + if (!fileExists) + { + hr = InstallShortcut(shortcutPath); + } + else + { + hr = S_FALSE; + } + } + } + return hr; +} + +HRESULT InstallShortcut(_In_z_ wchar_t *shortcutPath) +{ + wchar_t exePath[MAX_PATH]; + + DWORD charWritten = GetModuleFileNameEx(GetCurrentProcess(), nullptr, exePath, ARRAYSIZE(exePath)); + + HRESULT hr = charWritten > 0 ? S_OK : E_FAIL; + + if (SUCCEEDED(hr)) + { + ComPtr shellLink; + 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 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 persistFile; + hr = shellLink.As(&persistFile); + if (SUCCEEDED(hr)) + { + hr = persistFile->Save(shortcutPath, TRUE); + } + } + } + PropVariantClear(&appIdPropVar); + } + } + } + } + } + } + return hr; +} -- cgit v1.2.3