From d6506dc95d0f6750033b93981b2e767117585fb4 Mon Sep 17 00:00:00 2001 From: MikalaiR Date: Sat, 29 Aug 2015 09:34:57 +0000 Subject: forgotten file git-svn-id: http://svn.miranda-ng.org/main/trunk@15085 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/Toaster/src/add_to_start_menu.cpp | 82 +++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 plugins/Toaster/src/add_to_start_menu.cpp (limited to 'plugins') diff --git a/plugins/Toaster/src/add_to_start_menu.cpp b/plugins/Toaster/src/add_to_start_menu.cpp new file mode 100644 index 0000000000..ae2d1e7131 --- /dev/null +++ b/plugins/Toaster/src/add_to_start_menu.cpp @@ -0,0 +1,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 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 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