Untitled
unknown
plain_text
2 years ago
2.3 kB
6
Indexable
#include <iostream> #include <windows.h> #include <shellapi.h> #include <string> int main() { std::string path = "C:\\Program Files\\MyProgram\\myProgram.exe"; // Path to the shortcut std::string description = "My Program"; // Shortcut description // Pin the shortcut to the taskbar HRESULT result = PinShortcutToTaskbar(path.c_str(), description.c_str()); if (result == S_OK) { std::cout << "Successfully pinned shortcut to the taskbar."; } else { std::cout << "Failed to pin the shortcut to the taskbar."; } return 0; } // Function to pin shortcut to taskbar HRESULT PinShortcutToTaskbar(LPCSTR pszPath, LPCSTR pszDescription) { HRESULT hr = S_OK; IShellLink* psl = NULL; // Get a pointer to the IShellLink interface. hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (SUCCEEDED(hr)) { // Set the path to the shortcut target and add the // description. psl->SetPath(pszPath); psl->SetDescription(pszDescription); // Query IShellLink for the IPersistFile interface for // saving the shortcut in persistent storage. IPersistFile* ppf = NULL; hr = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hr)) { // Build shortcut path WCHAR wsz[MAX_PATH]; GetCurrentDirectory(MAX_PATH, wsz); wcscat_s(wsz, MAX_PATH, L"\\"); wcscat_s(wsz, MAX_PATH, pszDescription); wcscat_s(wsz, MAX_PATH, L".lnk"); // Save the link by calling IPersistFile::Save. hr = ppf->Save(wsz, TRUE); ppf->Release(); // Pin the shortcut to the taskbar CoTaskMemFree(wsz); hr = psl->QueryInterface(IID_IApplicationAssociationRegistration, (LPVOID*)&ppf); if (SUCCEEDED(hr)) { IApplicationAssociationRegistration* pAAR; hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration, NULL, CLSCTX_INPROC, IID_IApplicationAssociationRegistration, (LPVOID*)&pAAR); if (SUCCEEDED(hr)) { // Pin to the taskbar pAAR->SetAppAsDefault(pszDescription, pszPath, AT_PINNEDTOSTARTMENU); pAAR->Release(); } } } psl->Release(); } return hr; }
Editor is loading...