Josh Posted March 28, 2023 Share Posted March 28, 2023 My string values in the shortcut file are getting terminated after the first character. Can anyone see the error? #include "windows.h" #include "winnls.h" #include "shobjidl.h" #include "objbase.h" #include "objidl.h" #include "shlguid.h" // https://learn.microsoft.com/en-au/windows/win32/shell/links?redirectedfrom=MSDN#creating-a-shortcut-and-a-folder-shortcut-to-a-file bool CreateShortcut(const WString& path, const WString& dest, const WString& desc) { auto rpath = RealPath(path).Replace("/", "\\"); auto rdest = RealPath(dest).Replace("/", "\\"); LPCWSTR lpszPathObj = rpath.c_str(); LPCWSTR lpszPathLink = rdest.c_str(); LPCWSTR lpszDesc = desc.c_str(); CoInitialize(NULL); HRESULT hres; IShellLinkW* psl; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (SUCCEEDED(hres)) { IPersistFile* ppf; psl->SetPath(lpszPathObj); psl->SetDescription(lpszDesc); hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hres)) { hres = ppf->Save(lpszPathLink, TRUE); ppf->Release(); } psl->Release(); } return hres == S_OK; } // Example: CreateShortcut("Ultra Engine.exe", GetPath(PATH_DESKTOP) + "/Ultra Engine.lnk", "Description"); Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Josh Posted March 28, 2023 Author Share Posted March 28, 2023 Weird. It works if I convert the wide strings to Utf8: bool CreateShortcut(const WString& path, const WString& dest, const WString& desc) { auto rpath = RealPath(path).Replace("/", "\\"); auto rdest = RealPath(dest).Replace("/", "\\"); String s1 = rpath.ToUtf8String(); String s2 = desc.ToUtf8String(); String s3 = ExtractDir(rpath).Replace("/","\\").ToUtf8String(); LPCSTR lpszPathObj = s1.c_str(); LPCWSTR lpszPathLink = rdest.c_str(); LPCSTR lpszDesc = s2.c_str(); CoInitialize(NULL); HRESULT hres; IShellLink* psl; hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl); if (not SUCCEEDED(hres)) return false; IPersistFile* ppf; psl->SetPath(lpszPathObj); psl->SetWorkingDirectory(s3.c_str()); psl->SetDescription(lpszDesc); hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf); if (SUCCEEDED(hres)) { hres = ppf->Save(lpszPathLink, TRUE); ppf->Release(); } psl->Release(); return hres == S_OK; } Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Josh Posted March 28, 2023 Author Share Posted March 28, 2023 And this is the code to get the start menu location to place a link / shortcut in: PIDLIST_ABSOLUTE pidlist; wchar_t path[MAX_PATH]; HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_PROGRAMS, &pidlist); BOOL b = SHGetPathFromIDListW(pidlist, path); WString s = WString(path); Print(s); Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
reepblue Posted March 28, 2023 Share Posted March 28, 2023 I think there is a "LPCSTRW/WLPCSTR" or something that you should be using. Quote Cyclone - Ultra Game System - Component Preprocessor - Tex2TGA - Darkness Awaits Template (Leadwerks) If you like my work, consider supporting me on Patreon! Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.