Solved

Type structure for LNK files

Posted on 1998-07-27
2
358 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending a email via excel using vba 6 119
Run code from text file in vb 1 103
Convert VB6 MSXML2.ServerXMLHTTP process to C# 2 81
add criteria to query in VB, Access 2003 2 49
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…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

751 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