Search code examples
c++windowsshortcut-file

how to manually create a .lnk file like .desktop files in Linux


I want something like the .desktop files in Linux; I want to write it as a text file and I don't know the syntax

when I try to open it [the .lnk] as a text file it opens the actual binary, I could get something with the "cat file.Lnk" command but some words were unreadable


Solution

  • I want to write it as a text file and I don't know the syntax

    Simple, you can't.

    Unlike Linux's .desktop files, Windows' .lnk files are not text but have their data represented using byte structures.

    So no, you can't create them as easily, you will have to write an actual program if you want to create these files manually, which will compute the bytes that compose a .lnk file that has the properties of your choice if you want to achieve what you want.


    How to manually create a .lnk file

    First off, Microsoft's Shell Link API allows you to create such links (As pointed out by commenter @RemyLebeau).

    There also exists a library called liblnk that lets you interact with lnk files.

    Assuming that by asking how to manually create them, you mean doing it yourself, they host on their repository the documentation of the LNK format (permalink).

    This documentation is also available directly on Microsoft's website (archive), which also features documents for previous versions of the LNK format.

    Using this specification, and assuming you do have decent C++ experience (considering you tagged c++ to your question), you should be able to create your own .lnk shortcuts manually.


    I could get something with the "cat file.Lnk" command but some words were unreadable

    If you want to see the raw bytes of a .lnk, here are some steps to follow:

    • Create a shortcut using Windows' explorer
    • Open a Command Prompt in the directory you created your shortcut with
    • Enter the command copy TheNameOfYourShortcut.lnk TheNameOfYourShortcut.bin, replace TheNameOfYourShortcut with the name of your shortcut of course
    • You can now open the newly created .bin file in any hex editor, like HxD for example.

    If you try to directly read the bytes of the .lnk file without following these steps, the Windows API will actually make you read the content of the file the shortcut pointed to (not sure what happens if it's a directory shortcut though)

    So you need to duplicate it into a different filename with the extension changed to avoid this issue, which is exactly what these steps do.

    Just keep in mind, you will never be able to decently read those files with a simple command like cat because it simply isn't just text, it is structured data in the form of raw bytes.