ResolveIt - Uses the Shell's IShellLink and IPersistFile interfaces to retrieve the path and description from an existing shortcut. Adapted from: https://learn.microsoft.com/en-us/windows/win32/shell/links#resolving-a-shortcut Returns the result of calling the member functions of the interfaces. Parameters: hwnd - A handle to the parent window. The Shell uses this window to display a
| 154 | // target, including the file name. |
| 155 | // iPathBufferSize - Size of lpszPath in bytes. |
| 156 | _Check_return_ |
| 157 | HRESULT ResolveIt(_In_opt_ HWND hwnd, _In_ LPCWSTR lpszLinkFile, _Out_ LPWSTR lpszPath, _In_ size_t iPathBufferSize) { |
| 158 | HRESULT hres{}; |
| 159 | winrt::com_ptr<IShellLink> psl; |
| 160 | |
| 161 | *lpszPath = 0; // Assume failure |
| 162 | |
| 163 | // Get a pointer to the IShellLink interface. It is assumed that CoInitialize |
| 164 | // has already been called. |
| 165 | hres = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_IShellLink, reinterpret_cast<LPVOID*>(&psl)); |
| 166 | if (SUCCEEDED(hres)) { |
| 167 | winrt::com_ptr<IPersistFile> ppf; |
| 168 | // Get a pointer to the IPersistFile interface. |
| 169 | hres = psl->QueryInterface(IID_IPersistFile, reinterpret_cast<void**>(&ppf)); |
| 170 | if (SUCCEEDED(hres)) { |
| 171 | // Load the shortcut. |
| 172 | hres = ppf->Load(lpszLinkFile, STGM_READ); |
| 173 | if (SUCCEEDED(hres)) { |
| 174 | // Resolve the link. |
| 175 | hres = psl->Resolve(hwnd, 0); |
| 176 | if (SUCCEEDED(hres)) { |
| 177 | WCHAR szGotPath[MAX_PATH]; |
| 178 | // Get the path to the link target. |
| 179 | hres = psl->GetPath(szGotPath, ARRAYSIZE(szGotPath), nullptr, SLGP_RAWPATH); |
| 180 | if (SUCCEEDED(hres)) { |
| 181 | hres = StringCbCopy(lpszPath, iPathBufferSize, szGotPath); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | return hres; |
| 188 | } |
| 189 | |
| 190 | // Adapted from code generated with Google Gemini 2.0 Flash |
| 191 | _Check_return_ _Success_return_ |