4 * @brief Implementation file for JumpList helper functions.
10 #pragma warning (push) // prevent "warning C4091: 'typedef ': ignored on left of 'tagGPFIDL_FLAGS' when no variable is declared"
11 #pragma warning (disable:4091) // VC bug when using XP enabled toolsets.
14 #include <propvarutil.h>
22 wchar_t g_exe_path[260];
24 IShellLinkW *CreateShellLink(const std::wstring& app_path, const std::wstring& params, const std::wstring& title, const std::wstring& desc, int icon_index)
26 IShellLinkW *pShellLink = nullptr;
27 if (FAILED(CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
28 IID_IShellLinkW, (void **)&pShellLink)))
31 std::wstring app_path2(app_path);
34 if (g_exe_path[0] == '\0')
35 GetModuleFileNameW(nullptr, g_exe_path, sizeof(g_exe_path)/sizeof(g_exe_path[0]));
36 app_path2 = g_exe_path;
38 pShellLink->SetPath(app_path2.c_str());
39 pShellLink->SetIconLocation(app_path2.c_str(), icon_index);
40 pShellLink->SetArguments(params.c_str());
41 pShellLink->SetDescription(desc.c_str());
43 IPropertyStore *pPS = nullptr;
44 if (SUCCEEDED(pShellLink->QueryInterface(IID_IPropertyStore, (void **)&pPS)))
47 if (SUCCEEDED(InitPropVariantFromString(title.c_str(), &pv)))
49 pPS->SetValue(PKEY_Title, pv);
50 PropVariantClear(&pv);
64 bool SetCurrentProcessExplicitAppUserModelID(const std::wstring& appid)
67 HMODULE hLibrary = GetModuleHandle(_T("shell32.dll"));
68 if (hLibrary == nullptr)
70 HRESULT (__stdcall *pfnSetCurrentProcessExplicitAppUserModelID)(PCWSTR AppID) =
71 (HRESULT (__stdcall *)(PCWSTR))GetProcAddress(hLibrary, "SetCurrentProcessExplicitAppUserModelID");
72 if (pfnSetCurrentProcessExplicitAppUserModelID == nullptr)
74 return pfnSetCurrentProcessExplicitAppUserModelID(appid.c_str()) == S_OK;
77 bool AddToRecentDocs(const String& app_path, const String& params, const String& title, const String& desc, int icon_index)
79 SHARDAPPIDINFOLINK saiil;
80 saiil.pszAppID = g_appid.c_str();
81 saiil.psl = CreateShellLink(app_path, params, title, desc, icon_index);
82 if (saiil.psl == nullptr)
84 SHAddToRecentDocs(SHARD_APPIDINFOLINK, &saiil);
89 std::vector<Item> GetRecentDocs(size_t nMaxItems)
91 std::vector<Item> list;
92 IApplicationDocumentLists *pDocumentLists = nullptr;
93 if (FAILED(CoCreateInstance(CLSID_ApplicationDocumentLists, nullptr, CLSCTX_INPROC_SERVER,
94 IID_IApplicationDocumentLists, (void **)&pDocumentLists)))
96 pDocumentLists->SetAppID(g_appid.c_str());
98 IObjectArray *pObjectArray;
99 if (SUCCEEDED(pDocumentLists->GetList(ADLT_RECENT, static_cast<UINT>(nMaxItems), IID_IObjectArray, (void **)&pObjectArray)))
102 if (SUCCEEDED(pObjectArray->GetCount(&nObjects)))
104 for (UINT i = 0; i < nObjects; ++i)
106 IShellLinkW *pShellLink;
107 if (SUCCEEDED(pObjectArray->GetAt(i, IID_IShellLinkW, (void **)&pShellLink)))
109 wchar_t szPath[MAX_PATH];
110 wchar_t szDescription[MAX_PATH];
111 wchar_t szArguments[MAX_PATH * 6];
112 pShellLink->GetPath(szPath, sizeof(szPath) / sizeof(szPath[0]), nullptr, SLGP_RAWPATH);
113 pShellLink->GetDescription(szDescription, sizeof(szDescription) / sizeof(szDescription[0]));
114 pShellLink->GetArguments(szArguments, sizeof(szArguments) / sizeof(szArguments[0]));
115 IPropertyStore *pPS = nullptr;
116 if (SUCCEEDED(pShellLink->QueryInterface(IID_IPropertyStore, (void **)&pPS)))
119 PropVariantInit(&pv);
120 if (SUCCEEDED(pPS->GetValue(PKEY_Title, &pv)))
122 list.push_back(Item(ucr::toTString(szPath), ucr::toTString(szArguments), ucr::toTString(pv.bstrVal), ucr::toTString(szDescription)));
123 PropVariantClear(&pv);
127 pShellLink->Release();
131 pObjectArray->Release();
133 pDocumentLists->Release();