Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a Windows shortcut (shell link) in C++

I want to programmatically create a Windows shortcut (.lnk file) to a folder. To do this, I tried this code snippet. However, I get the compilation error C2371 'WebBrowser': redefinition; different basic types in C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\um\exdisp.h line 2367.

Is there a C++17 std::filesystem API for this? If not, how can I fix the compilation error from above? Even with cleaned up includes, the error persists:

#include <Windows.h>
#include <shlguid.h>
#include <shobjidl_core.h>

Using the mklink command yields:
The device does not support symbolic links.
So that doesn’t work either, maybe because this is an external SSD.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What else could I try?

>Solution :

The first answer you linked to is the correct way. And it does not rely on WebBrowser at all. It will probably help to define some of the NO_XXX macros before including windows.h.

In my version of ShlGuid.h which is slightly older than yours, I see

#ifndef NO_SHDOCVW_GUIDS

#ifndef GUID_DEFS_ONLY
#include <exdisp.h>
#include <shldisp.h>
#endif

so you can use NO_SHDOCVW_GUIDS to excise the troublesome header that you weren’t wanting anyway.

If you wanted support for WebBrowser and the kitchen sink, another fix might be moving #include <windows.h> after ShlGuid.h, as shown e.g. here: https://stackoverflow.com/a/73422757/103167


Links are something that differ a fair amount from filesystem to filesystem, so difficult to standardize. Shell links (.lnk files) are filesystem-independent, but only work on MS Windows (completely non-portable) which also is a strike against finding support in a standard API.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading