Solved

Type structure for LNK files

Posted on 1998-07-27
2
356 Views
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.
TIA.

Regards,
Tony
0
Comment
Question by:scvb
2 Comments
 
LVL 14

Accepted Solution

by:
waty earned 200 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.
   HRESULT
   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,
                               NULL,
                               CLSCTX_INPROC_SERVER,
                               &IID_IShellLink,
                               (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,
                                            &IID_IPersistFile,
                                            (LPVOID *)&ppf );
         if (SUCCEEDED(hres))
         {
            WORD wsz[MAX_PATH];

   // Convert the given link name string to a wide character string.
            MultiByteToWideChar( CP_ACP, 0,
                                 lpszLinkName,
                                 -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,
                                               MAX_PATH,
                                               &wfd,
                                               SLGP_SHORTPATH );

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




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


   /*PARAMETERS

   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;
   LPITEMIDLIST pidl;
   
   hres = CreateInstance(  CLSID_ShellLink,
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           IID_IShellLink,
                           (LPVOID*)&psl);
   
   if(FAILED(hres))
      goto cleanup;
   
   hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&pPf);
   
   if(FAILED(hres))
      goto cleanup;
   
   hres = psl->SetPath(fname_to_create_link);
   
   if(FAILED(hres))
      goto cleanup;
   
   //place the shortcut on the desktop
   
   SHGetSpecialFolderLocation(hwnd, CSIDL_DESKTOP, &pidl);
   
   SHGetPathFromIDList(pidl, buf);
   
   lstrcat(buf,"\\");
   lstrcat(buf,lnk_fname);
   
   MultiByteToWideChar(CP_ACP, 0, buf, -1, wsz, MAX_PATH);
   
   hres = pPf->Save(wsz, TRUE);
   
   if(FAILED(hres))
      goto cleanup;
   
   GetSystemDirectory(buf, 256);
   
   lstrcat(buf,"\\shell32.dll");
   
   hres = psl->SetIconLocation(buf, 1);
   
   if(FAILED(hres))
      goto cleanup;
   
   hres = psl->GetIconLocation(buf, 256, &id);
   
   if(FAILED(hres))
      goto cleanup;
   
   pPf->Save(wsz, TRUE);
   
   
   cleanup:
   
   if(pPf)
      pPf->Release();
   
   if(psl)
      psl->Release();
   
   }

0
 

Author Comment

by:scvb
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,
   Tony
   tvtech@homemail.com

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

789 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