Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: generated shortcut .lnk file doesnt work ... what am I missing?  (Read 5643 times)

0 Members and 1 Guest are viewing this topic.

DaveLembke

    Topic Starter


    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Trying to help out this person at this other thread at link below and figured it would be an easy quick program to code up. Coded up and compiled fine, but oddly I am able to create .lnk shortcuts but the shortcuts dont bring you to the destination. Nothing happens when clicking on the shortcut.  ???

On my system I created structure similar to this other person who is looking for help with:

C:\MyProjects\

I then created a Project folder of Project123

so C:\MyProjects\Project123  exists

I then had added FolderA and FolderC subdirectories at Project123

When running my program below and telling it Project123 it runs to the path of C:\MyProjects\Project123\FolderC\ and creates a shortcut pathed to C:\MyProjects\Project123\FolderA\ named Goto_FolderA.lnk  the program writes to this correctly however the .lnk doesnt work as a .lnk shortcut even though Windows 7 64-bit recognizes it as a shortcut.

Additionally I am unable to look into this .lnk to see whats in it as you can do with shortcuts generated by windows, so i flipped the extension to .txt instead of .lnk to verify that the correct path info was being written and it is .... what I noticed also is that the shortcut I generate is 35 bytes and ones created by Windows are larger such as below:

Quote
C:\2017\programming>dir
 Volume in drive C has no label.
 Volume Serial Number is 125F-376A

 Directory of C:\2017\programming

04/26/2017  11:15 PM    <DIR>          .
04/26/2017  11:15 PM    <DIR>          ..
04/26/2017  11:14 PM             1,326 Goto_FolderA - Shortcut.lnk
04/26/2017  09:07 PM               317 Project1.cpp
04/26/2017  09:07 PM           475,586 Project1.exe
04/26/2017  11:14 PM               460 Project2.cpp
04/26/2017  11:14 PM           500,780 Project2.exe
04/26/2017  10:59 PM               456 Project2a.txt
04/26/2017  09:07 PM                35 test.txt
               7 File(s)        978,960 bytes
               2 Dir(s)   5,009,920,000 bytes free

C:\2017\programming>cd\.

C:\>cd myprojects

C:\MyProjects>cd project123

C:\MyProjects\Project123>cd folderC

C:\MyProjects\Project123\FolderC>dir
 Volume in drive C has no label.
 Volume Serial Number is 125F-376A

 Directory of C:\MyProjects\Project123\FolderC

04/26/2017  11:07 PM    <DIR>          .
04/26/2017  11:07 PM    <DIR>          ..
04/26/2017  11:07 PM                35 Goto_FolderA.lnk
04/26/2017  11:07 PM                35 Goto_FolderA.txt
               2 File(s)             70 bytes
               2 Dir(s)   5,009,920,000 bytes free

C:\MyProjects\Project123\FolderC>

So what magic does Windows add to this file to bloat it to 1,326 bytes from 35 bytes and make it an active working shortcut? ... yet opening both files shows the same path of C:\MyProjects\Project123\FolderA\ and nothing else?

Here is the program I slapped together quickly that allows creation of Project_Name pathed shortcuts to be planted into FolderC for a quick hop to FolderA for same project... but the .lnk shortcuts are non functional and much smaller than shortcuts created under windows.

Code: [Select]
#include <iostream>
#include <string>
#include <fstream>
int main() {
  std::string Project_Name;
  std::cout<<"Enter Project Name\n";
  std::cin>>Project_Name;

  std::string filepath = "C:\\MyProjects\\";
 
  std::ofstream outfile;
  outfile.open((filepath.append(Project_Name+"\\FolderC\\Goto_FolderA.lnk")).c_str(),std::ios_base::trunc);
  outfile <<"C:\\MyProjects\\"<<Project_Name<<"\\FolderA\\\n";

  outfile.close();
  return 0;
}


Here is the link to the project that I was going to help out at, but found out that now I too need help with the fact that I guess I might not completely understand how shortcuts are generated in Windows. For years I always thought a path in a text file that is saved as a .lnk instead of .txt would function as a shortcut. Maybe years ago it was the case and Windows evolved to not allow this or there is more to it than a path contained in a file under a .lnk file extension.  :-\
http://www.computerhope.com/forum/index.php/topic,160548.0/topicseen.html

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: generated shortcut .lnk file doesnt work ... what am I missing?
« Reply #1 on: April 26, 2017, 10:09:53 PM »
Shortcuts are "Shell Links". They will include path information, but they also include a bunch of other data such as the Shell PIDL of the target (Pointer to an Item ID List). These aren't merely pointers to other locations on the file system, they are pointers to "Shell Items". While things like files and folders are shell items and have a file path, things like Control Panel are not-  those are separate namespaces, but the Shell Links have to work and be able to resolve those as well.

The proper way of creating shortcuts programmatically in say C++ would be to use the Shell Link reference. An example is found on thisStack Overflow post.

I'm not sure how you are creating shortcuts, but I can't seem to create one that only include a target path, even on Win95 it's got a good deal of binary data.

You can create them fairly easily with VBScript and Powershell, though.

Code: [Select]
SaveShortcut = "C:\MyProjects\Project123\FolderA\FolderC.lnk"
TargetPath = "C:\MyProjects\Project123\FolderC"
set wsh = CreateObject("WScript.Shell")
Set ShellLink = wsh.CreateShortCut(SaveShortCut)
ShellLink.TargetPath = TargetPath
ShellLink.Save

Powershell does the same thing more or less:

Code: [Select]
powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('C:\MyProjects\Project123\FolderA\FolderC.lnk');$s.TargetPath='C:\MyProjects\Project123\FolderC';$s.Save()"

I was trying to dereference Null Pointers before it was cool.