Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* |
michael@0 | 2 | * Module : Set.cpp |
michael@0 | 3 | * Purpose: NSIS Plug-in for setting shortcut ApplicationID property |
michael@0 | 4 | * Created: 27/12/2009 |
michael@0 | 5 | * Original code Copyright (c) 2009 Mike Anchor. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | /* |
michael@0 | 9 | * Additional Mozilla contributions: |
michael@0 | 10 | * Unicode support |
michael@0 | 11 | * Jump list deletion on uninstall |
michael@0 | 12 | * Pinned item removal on uninstall |
michael@0 | 13 | * contrib: <jmathies@mozilla.com> |
michael@0 | 14 | */ |
michael@0 | 15 | |
michael@0 | 16 | #define INITGUID |
michael@0 | 17 | |
michael@0 | 18 | #include <windows.h> |
michael@0 | 19 | #include <shlobj.h> |
michael@0 | 20 | #include <propvarutil.h> |
michael@0 | 21 | #include <propkey.h> |
michael@0 | 22 | #include <stdio.h> |
michael@0 | 23 | |
michael@0 | 24 | #pragma comment (lib, "shlwapi.lib") |
michael@0 | 25 | |
michael@0 | 26 | #define MAX_STRLEN 1024 |
michael@0 | 27 | |
michael@0 | 28 | typedef struct _stack_t { |
michael@0 | 29 | struct _stack_t *next; |
michael@0 | 30 | TCHAR text[MAX_PATH]; |
michael@0 | 31 | } stack_t; |
michael@0 | 32 | |
michael@0 | 33 | stack_t **g_stacktop; |
michael@0 | 34 | unsigned int g_stringsize; |
michael@0 | 35 | TCHAR *g_variables; |
michael@0 | 36 | |
michael@0 | 37 | // Indicates that an application supports dual desktop and immersive modes. In Windows 8, this property is only applicable for web browsers. |
michael@0 | 38 | DEFINE_PROPERTYKEY(PKEY_AppUserModel_IsDualMode, 0x9F4C2855, 0x9F79, 0x4B39, 0xA8, 0xD0, 0xE1, 0xD4, 0x2D, 0xE1, 0xD5, 0xF3, 11); |
michael@0 | 39 | |
michael@0 | 40 | int popstring(TCHAR *str, int len); |
michael@0 | 41 | void pushstring(const TCHAR *str, int len); |
michael@0 | 42 | |
michael@0 | 43 | extern "C" void __declspec(dllexport) Set(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop) |
michael@0 | 44 | { |
michael@0 | 45 | g_stringsize = string_size; |
michael@0 | 46 | g_stacktop = stacktop; |
michael@0 | 47 | g_variables = variables; |
michael@0 | 48 | |
michael@0 | 49 | { |
michael@0 | 50 | IPropertyStore *m_pps = NULL; |
michael@0 | 51 | WCHAR wszPath[MAX_PATH]; |
michael@0 | 52 | WCHAR wszAppID[MAX_PATH]; |
michael@0 | 53 | TCHAR szPath[MAX_PATH]; |
michael@0 | 54 | TCHAR szAppID[MAX_PATH]; |
michael@0 | 55 | TCHAR szDualMode[MAX_PATH]; |
michael@0 | 56 | bool success = false; |
michael@0 | 57 | |
michael@0 | 58 | ZeroMemory(wszPath, sizeof(wszPath)); |
michael@0 | 59 | ZeroMemory(wszAppID, sizeof(wszAppID)); |
michael@0 | 60 | ZeroMemory(szPath, sizeof(szPath)); |
michael@0 | 61 | ZeroMemory(szAppID, sizeof(szAppID)); |
michael@0 | 62 | ZeroMemory(szDualMode, sizeof(szDualMode)); |
michael@0 | 63 | |
michael@0 | 64 | popstring(szPath, MAX_PATH); |
michael@0 | 65 | popstring(szAppID, MAX_PATH); |
michael@0 | 66 | bool dualMode = (popstring(szDualMode, MAX_PATH) == 0); // optional |
michael@0 | 67 | #if !defined(UNICODE) |
michael@0 | 68 | MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH); |
michael@0 | 69 | MultiByteToWideChar(CP_ACP, 0, szAppID, -1, wszAppID, MAX_PATH); |
michael@0 | 70 | if (dualMode && stricmp(szDualMode, "true") != 0) { |
michael@0 | 71 | dualMode = false; |
michael@0 | 72 | } |
michael@0 | 73 | #else |
michael@0 | 74 | wcscpy_s(wszPath, szPath); |
michael@0 | 75 | wcscpy_s(wszAppID, szAppID); |
michael@0 | 76 | if (dualMode && _wcsicmp(szDualMode, L"true") != 0) { |
michael@0 | 77 | dualMode = false; |
michael@0 | 78 | } |
michael@0 | 79 | #endif |
michael@0 | 80 | |
michael@0 | 81 | CoInitialize(NULL); |
michael@0 | 82 | |
michael@0 | 83 | if (SUCCEEDED(SHGetPropertyStoreFromParsingName(wszPath, NULL, GPS_READWRITE, IID_PPV_ARGS(&m_pps)))) |
michael@0 | 84 | { |
michael@0 | 85 | PROPVARIANT propvar; |
michael@0 | 86 | if (SUCCEEDED(InitPropVariantFromString(wszAppID, &propvar))) { |
michael@0 | 87 | if (SUCCEEDED(m_pps->SetValue(PKEY_AppUserModel_ID, propvar))) { |
michael@0 | 88 | if (dualMode) { |
michael@0 | 89 | InitPropVariantFromBoolean(true, &propvar); |
michael@0 | 90 | m_pps->SetValue(PKEY_AppUserModel_IsDualMode, propvar); |
michael@0 | 91 | } |
michael@0 | 92 | if (SUCCEEDED(m_pps->Commit())) { |
michael@0 | 93 | success = true; |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | if (m_pps != NULL) |
michael@0 | 99 | m_pps->Release(); |
michael@0 | 100 | |
michael@0 | 101 | CoUninitialize(); |
michael@0 | 102 | |
michael@0 | 103 | pushstring(success == true ? TEXT("0") : TEXT("-1"), MAX_PATH); |
michael@0 | 104 | } |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | extern "C" void __declspec(dllexport) UninstallJumpLists(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop) |
michael@0 | 108 | { |
michael@0 | 109 | g_stringsize = string_size; |
michael@0 | 110 | g_stacktop = stacktop; |
michael@0 | 111 | g_variables = variables; |
michael@0 | 112 | |
michael@0 | 113 | ICustomDestinationList *m_cdl = NULL; |
michael@0 | 114 | WCHAR wszAppID[MAX_PATH]; |
michael@0 | 115 | TCHAR szAppID[MAX_PATH]; |
michael@0 | 116 | bool success = false; |
michael@0 | 117 | |
michael@0 | 118 | ZeroMemory(wszAppID, sizeof(wszAppID)); |
michael@0 | 119 | ZeroMemory(szAppID, sizeof(szAppID)); |
michael@0 | 120 | |
michael@0 | 121 | popstring(szAppID, MAX_PATH); |
michael@0 | 122 | |
michael@0 | 123 | #if !defined(UNICODE) |
michael@0 | 124 | MultiByteToWideChar(CP_ACP, 0, szAppID, -1, wszAppID, MAX_PATH); |
michael@0 | 125 | #else |
michael@0 | 126 | wcscpy_s(wszAppID, szAppID); |
michael@0 | 127 | #endif |
michael@0 | 128 | |
michael@0 | 129 | CoInitialize(NULL); |
michael@0 | 130 | |
michael@0 | 131 | CoCreateInstance(CLSID_DestinationList, NULL, CLSCTX_INPROC_SERVER, |
michael@0 | 132 | IID_ICustomDestinationList, (void**)&m_cdl); |
michael@0 | 133 | |
michael@0 | 134 | if (m_cdl) { |
michael@0 | 135 | if (SUCCEEDED(m_cdl->DeleteList(wszAppID))) { |
michael@0 | 136 | success = true; |
michael@0 | 137 | } |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | if (m_cdl) |
michael@0 | 141 | m_cdl->Release(); |
michael@0 | 142 | |
michael@0 | 143 | CoUninitialize(); |
michael@0 | 144 | |
michael@0 | 145 | pushstring(success == true ? TEXT("0") : TEXT("-1"), MAX_PATH); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | extern "C" void __declspec(dllexport) UninstallPinnedItem(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop) |
michael@0 | 149 | { |
michael@0 | 150 | g_stringsize = string_size; |
michael@0 | 151 | g_stacktop = stacktop; |
michael@0 | 152 | g_variables = variables; |
michael@0 | 153 | |
michael@0 | 154 | IShellItem *pItem = NULL; |
michael@0 | 155 | IStartMenuPinnedList *pPinnedList = NULL; |
michael@0 | 156 | WCHAR wszPath[MAX_PATH]; |
michael@0 | 157 | TCHAR szPath[MAX_PATH]; |
michael@0 | 158 | bool success = false; |
michael@0 | 159 | |
michael@0 | 160 | ZeroMemory(wszPath, sizeof(wszPath)); |
michael@0 | 161 | ZeroMemory(szPath, sizeof(szPath)); |
michael@0 | 162 | |
michael@0 | 163 | popstring(szPath, MAX_PATH); |
michael@0 | 164 | |
michael@0 | 165 | #if !defined(UNICODE) |
michael@0 | 166 | MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH); |
michael@0 | 167 | #else |
michael@0 | 168 | wcscpy_s(wszPath, szPath); |
michael@0 | 169 | #endif |
michael@0 | 170 | |
michael@0 | 171 | CoInitialize(NULL); |
michael@0 | 172 | |
michael@0 | 173 | HRESULT hr; |
michael@0 | 174 | hr = SHCreateItemFromParsingName(wszPath, NULL, IID_PPV_ARGS(&pItem)); |
michael@0 | 175 | |
michael@0 | 176 | if (SUCCEEDED(hr)) { |
michael@0 | 177 | |
michael@0 | 178 | hr = CoCreateInstance(CLSID_StartMenuPin, |
michael@0 | 179 | NULL, |
michael@0 | 180 | CLSCTX_INPROC_SERVER, |
michael@0 | 181 | IID_PPV_ARGS(&pPinnedList)); |
michael@0 | 182 | |
michael@0 | 183 | if (SUCCEEDED(hr)) { |
michael@0 | 184 | hr = pPinnedList->RemoveFromList(pItem); |
michael@0 | 185 | pPinnedList->Release(); |
michael@0 | 186 | success = true; |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | pItem->Release(); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | CoUninitialize(); |
michael@0 | 193 | |
michael@0 | 194 | pushstring(success == true ? TEXT("0") : TEXT("-1"), MAX_PATH); |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | //Function: Removes the element from the top of the NSIS stack and puts it in the buffer |
michael@0 | 198 | int popstring(TCHAR *str, int len) |
michael@0 | 199 | { |
michael@0 | 200 | stack_t *th; |
michael@0 | 201 | if (!g_stacktop || !*g_stacktop) return 1; |
michael@0 | 202 | th=(*g_stacktop); |
michael@0 | 203 | lstrcpyn(str,th->text, len); |
michael@0 | 204 | *g_stacktop=th->next; |
michael@0 | 205 | GlobalFree((HGLOBAL)th); |
michael@0 | 206 | return 0; |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | //Function: Adds an element to the top of the NSIS stack |
michael@0 | 210 | void pushstring(const TCHAR *str, int len) |
michael@0 | 211 | { |
michael@0 | 212 | stack_t *th; |
michael@0 | 213 | |
michael@0 | 214 | if (!g_stacktop) return; |
michael@0 | 215 | th=(stack_t*)GlobalAlloc(GPTR, sizeof(stack_t) + len); |
michael@0 | 216 | lstrcpyn(th->text, str, len); |
michael@0 | 217 | th->next=*g_stacktop; |
michael@0 | 218 | *g_stacktop=th; |
michael@0 | 219 | } |