Home > OS >  Creating a Windows shortcut (shell link) in C
Creating a Windows shortcut (shell link) in C

Time:01-11

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.

What else could I try?

CodePudding user response:

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.

  • Related