Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Type structure for LNK files

Posted on 1998-07-27
2
Medium Priority
?
361 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 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.
   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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

886 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