Solved

Type structure for LNK files

Posted on 1998-07-27
2
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

740 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