Solved

How to resolve shell links ("shortcuts")?

Posted on 2004-09-07
2
1,417 Views
Last Modified: 2008-01-09
I would like to be able to resolve a shell link ("shortcut") to find out what it's target filename is. I have code that does this in C using the COM shell routines. I don't know how to write an equivelent routine in VB.NET - can someone help? Either .NET 1.1 or 2.0 (VS2005) is OK. Thanks.

This is the C code that I currently have to resolve shell links:

HRESULT ResolveIt(HWND hwnd, LPCSTR lpszLinkFile, LPSTR lpszPath)
{
    HRESULT hres;
    IShellLink* psl;
    char szGotPath[MAX_PATH];
    char szDescription[MAX_PATH];
    WIN32_FIND_DATA wfd;
 
    *lpszPath = 0; // assume failure
 
    // Get a pointer to the IShellLink interface.
    hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
                            IID_IShellLink, (LPVOID*)&psl);
    if (SUCCEEDED(hres))
    {
        IPersistFile* ppf;
 
        // Get a pointer to the IPersistFile interface.
        hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
         
        if (SUCCEEDED(hres))
        {
            WCHAR wsz[MAX_PATH];
 
            // Ensure that the string is Unicode.
            MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH);
 
            // TODO: Check return value from MultiByteWideChar to ensure
                     success.
 
            // Load the shortcut.
            hres = ppf->Load(wsz, STGM_READ);
           
            if (SUCCEEDED(hres))
            {
                // Resolve the link.
                hres = psl->Resolve(hwnd, 0);

                if (SUCCEEDED(hres))
                {
                    // Get the path to the link target.
                    hres = psl->GetPath(szGotPath,
                                        MAX_PATH,
                                        (WIN32_FIND_DATA*)&wfd,
                                        SLGP_SHORTPATH);

                    if (SUCCEEDED(hres))
                    {
                        // Get the description of the target.
                        hres = psl->GetDescription(szDescription, MAX_PATH);

                        if (SUCCEEDED(hres))
                        {
                            hres = StringCbCopy(lpszPath, sizeof(lpszPath),
                                                szGotPath);
                            if (SUCCEEDED(hres))
                            {
                                // Handle success
                            }
                            else
                                // application-defined function
                                HandleErr(hres);
                        }
                    }
                }
            }

            // Release the pointer to the IPersistFile interface.
            ppf->Release();
        }

        // Release the pointer to the IShellLink interface.
        psl->Release();
    }
    return hres;
}


  - Bob
0
Comment
Question by:BobLafleur
2 Comments
 
LVL 41

Accepted Solution

by:
graye earned 200 total points
ID: 12002535
There are several ways to do this... but the easiest is to use the Windows Scripting Host (WSH) libraries.

'Add a reference to "Windows Script Host Object Model" (IWshRuntimeLibrary)

    Dim Shell As IWshRuntimeLibrary.IWshShell_Class
    Dim Shortcut As IWshRuntimeLibrary.IWshShortcut_Class

    Shortcut = Shell.CreateShortcut(file)
    debug.writeline("TargetPath = " & Shortcut.TargetPath)
    debug.writeline("WorkingDirectory = " & Shortcut.WorkingDirectory)
    debug.writeline("IconLocation = " & Shortcut.IconLocation)
0
 

Author Comment

by:BobLafleur
ID: 12002934
Thank you. That was very painless!

   - Bob
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

791 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