Thread: Gettings the real Filename of a shortcur file

  1. #1
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204

    Gettings the real Filename of a shortcur file

    Hey, does any1 here know how to get the real filename of a shortcut file in windows? .lnk that is

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    You can get the format of a .lnk and sample processing code here.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Here's a function.
    Code:
    #include <windows.h>
    #include <objidl.h>   /* For IPersistFile */
    #include <shlobj.h>   /* For IShellLink */
    
    #if defined(_MSC_VER)
    #pragma comment(lib, "ole32.lib")
    #pragma comment(lib, "uuid.lib")
    #endif
    
    
    /*
     * GetShortcutTarget
     * Retrieves the path that a shortcut file points to.
     *
     * Paramaters:
     *    szShortcutFile    The path to the shortcut file.
     *    szTarget          Pointer to a buffer that will receive the target path.
     *                      The buffer length should be at least MAX_PATH characters.
     *    cchTarget         The size of the szTarget buffer, in characters.
     *
     * Return:
     *    A non-zero value is returned on success. If the function fails zero
     *    will be returned. GetLastError can NOT be called to get an additional error code.
     *
     * Runtime requirements:
     *    CoInitialize or CoInitializeEx must have been called before using this function.
     *
     * Compile requirements:
     *    C++. Include <windows.h>, <objidl.h> & <shlobj.h>. Link "uuid.lib".
     */
    BOOL GetShortcutTarget(LPCTSTR szShortcutFile, LPTSTR szTarget, SIZE_T cchTarget)
    {
        IShellLink*    psl     = NULL;
        IPersistFile*  ppf     = NULL;
        BOOL           bResult = FALSE;
    
        if (FAILED( CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl) ))
            goto cleanup;
    
        if (FAILED( psl->QueryInterface(IID_IPersistFile, (void **) &ppf) ))
            goto cleanup;
    
    #   if !defined(UNICODE)
            WCHAR wsz[MAX_PATH];
            if (0 == MultiByteToWideChar(CP_ACP, 0, szShortcutFile, -1, wsz, MAX_PATH) )
                goto cleanup;
    #   else
            LPCWSTR wsz = szShortcutFile;
    #   endif
    
        if (FAILED( ppf->Load(wsz, STGM_READ) ))
            goto cleanup;
    
        if (NOERROR != psl->GetPath(szTarget, cchTarget, NULL, 0) )
            goto cleanup;
    
        bResult = TRUE;
    
    cleanup:
        if (ppf) ppf->Release();
        if (psl) psl->Release();
        if (!bResult && cchTarget != 0) szTarget[0] = TEXT('\0');
        return bResult;
    }
    
    
    #if 1 /* Test code. */
    #include <stdio.h>
    #include <tchar.h>
    
    int main(void)
    {
        TCHAR szTarget[MAX_PATH];
    
        CoInitialize(NULL);
    
        GetShortcutTarget(
         TEXT("C:\\Documents and Settings\\All Users\\Start Menu\\")
         TEXT("Programs\\Accessories\\Calculator.lnk"),
             szTarget, MAX_PATH);
    
        _tprintf(TEXT("The shortcut target is '%s'.\n"), szTarget);
    
        CoUninitialize();
        getchar();
        return 0;
    }
    #endif

  4. #4
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    wow thks to both, especially for the function its great

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM