Type structure for LNK files

Posted on 1998-07-27
Medium Priority
Last Modified: 2010-05-18
Hi all

Does anyone know the type structure for WIN95/98 Shell Link (LNK) files? I would like to have the ability to read the executable name contained within the LNK, so I can obtain it's icon.

Question by:scvb
LVL 14

Accepted Solution

waty earned 800 total points
ID: 1466801
You should find on Microsoft MSDN informations about the IShellLink class.

Here ate two samples in C. (I have some sample in VB, but I have to make a search).
The following code shows how to obtain the filename or path and description of a given link file:

   #include <windows.h>
   #include <shlobj.h>

   // GetLinkInfo() fills the filename and path buffer
   // with relevant information.
   // hWnd         - calling application's window handle.
   // lpszLinkName - name of the link file passed into the function.
   // lpszPath     - the buffer that receives the file's path name.
   // lpszDescription - the buffer that receives the file's
   // description.
   GetLinkInfo( HWND    hWnd,
                LPCTSTR lpszLinkName,
                LPSTR   lpszPath,
                LPSTR   lpszDescription)

      HRESULT hres;
      IShellLink *pShLink;
      WIN32_FIND_DATA wfd;

   // Initialize the return parameters to null strings.
      *lpszPath = '\0';
      *lpszDescription = '\0';

   // Call CoCreateInstance to obtain the IShellLink
   // Interface pointer. This call fails if
   // CoInitialize is not called, so it is assumed that
   // CoInitialize has been called.
      hres = CoCreateInstance( &CLSID_ShellLink,
                               (LPVOID *)&pShLink );

      if (SUCCEEDED(hres))
         IPersistFile *ppf;

   // The IShellLink Interface supports the IPersistFile
   // interface. Get an interface pointer to it.
         hres = pShLink->lpVtbl->QueryInterface(pShLink,
                                            (LPVOID *)&ppf );
         if (SUCCEEDED(hres))
            WORD wsz[MAX_PATH];

   // Convert the given link name string to a wide character string.
            MultiByteToWideChar( CP_ACP, 0,
                                 -1, wsz, MAX_PATH );
   // Load the file.
            hres = ppf->lpVtbl->Load(ppf, wsz, STGM_READ );
            if (SUCCEEDED(hres))
   // Resolve the link by calling the Resolve() interface function.
   // This enables us to find the file the link points to even if
   // it has been moved or renamed.
               hres = pShLink->lpVtbl->Resolve(pShLink,  hWnd,
                                               SLR_ANY_MATCH | SLR_NO_UI);
               if (SUCCEEDED(hres))
   // Get the path of the file the link points to.
                  hres = pShLink->lpVtbl->GetPath( pShLink, lpszPath,
                                               SLGP_SHORTPATH );

   // Only get the description if we successfully got the path
   // (We can't return immediately because we need to release ppf &
   //  pShLink.)
   // Get the description of the link.
                    hres = pShLink->lpVtbl->GetDescription(pShLink,
                                                         MAX_PATH );
      return hres;

Following is sample code that creates a shortcut and sets the shortcut's icon to an icon contained in shell32.dll:


   fname_to_create_link  = (e.g.) "c:\\mytextfile.txt"
   lnk_fname = (e.g.) "yourname.lnk"


   void CreateLinkThenChangeIcon(LPTSTR fname_to_create_link,
                                 LPTSTR lnk_fname)
   HRESULT hres;
   IShellLink *psl = NULL;
   IPersistFile *pPf = NULL;
   WORD wsz[256];
   TCHAR buf[256];
   int id;
   hres = CreateInstance(  CLSID_ShellLink,
      goto cleanup;
   hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&pPf);
      goto cleanup;
   hres = psl->SetPath(fname_to_create_link);
      goto cleanup;
   //place the shortcut on the desktop
   SHGetSpecialFolderLocation(hwnd, CSIDL_DESKTOP, &pidl);
   SHGetPathFromIDList(pidl, buf);
   MultiByteToWideChar(CP_ACP, 0, buf, -1, wsz, MAX_PATH);
   hres = pPf->Save(wsz, TRUE);
      goto cleanup;
   GetSystemDirectory(buf, 256);
   hres = psl->SetIconLocation(buf, 1);
      goto cleanup;
   hres = psl->GetIconLocation(buf, 256, &id);
      goto cleanup;
   pPf->Save(wsz, TRUE);


Author Comment

ID: 1466802
I am happy to give you the points:) I would really appreciate the VB code, as I am C less <g>

Looking forward to hearing from you.

Kind regards,


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question