Solved

How to resolve shell links ("shortcuts")?

Posted on 2004-09-07
2
1,409 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now