Thread: IShellLink::GetPath Method

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    44

    IShellLink::GetPath Method

    ı am writing in microsoft visual c++ 6.0.... ı need get full file path from its shortcut... ı found in msdn but ı dont understand anything.. can explain this method or give simple example??

    http://msdn.microsoft.com/en-us/libr...72(VS.85).aspx
    Last edited by koyboy; 05-07-2008 at 08:28 AM. Reason: rewrite link

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That Path has nothing to do with file system paths, but paths as in the edges of a polygon (for example).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    44
    yes first link isnt wroking ... ı think you dont understand me rags_to_riches.. ı use microsoft visual c++ 6.0 and these

    #include <objidl.h> /* For IPersistFile */
    #include <shlobj.h> /* For IShellLink */


    includes not working on.. :S.. can you give another advice :S

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    So, perhaps an incorrectly installed PlatformSDK? Get a new one. And while you're at it, get Visual C++.Net 2008 Express. It's free, and it's not 12 years old.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    44
    ı have visual studio 2008 full.. thanks for advice .. but if ı write program in vs 2008 its need framework... ı dont want it... ı have do without framework

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Tested and works in VS 2008 (running on XP SP3 in a VMware Fusion VM under Mac OS X):
    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 !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( CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl) ))
            goto cleanup;
    
        if (FAILED( psl->QueryInterface(IID_IPersistFile, (void **) &ppf) ))
            goto cleanup;
    
        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
    You need to create the right kind of project (Visual C++) that doesn't use the CLR.

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    44
    thanks a lot man.. ı get header files from vs 2008 and include c++ 6.0... than thats ok it works but ı have one more questions.. if ı do this in vs 2008 it need framework.. it is true or false?? how can ı do this in vs 2008 and the exe not need the framework :S

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    False. Read the last sentence again:

    You need to create the right kind of project (Visual C++) that doesn't use the CLR.
    The CLR is the Common Language Runtime. I created a Visual C++->Win32->Win32 Console Project.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by koyboy View Post
    yes first link isnt wroking ... ı think you dont understand me rags_to_riches.. ı use microsoft visual c++ 6.0 and these

    #include <objidl.h> /* For IPersistFile */
    #include <shlobj.h> /* For IShellLink */


    includes not working on.. :S.. can you give another advice :S
    Try the Windows forum?

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    44
    ı copy my app.'s code to Visual C++->Win32->Win32 Console Project but ı have too many problemm.. ok thats not important.. ı cont. write in c++ 6.0.. thanks all of you for advice.. ı am sory my english

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Red face Nice explanations.

    Quote Originally Posted by rags_to_riches View Post
    Tested and works in VS 2008 (running on XP SP3 in a VMware Fusion VM under Mac OS X):
    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 !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( CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void **) &psl) ))
            goto cleanup;
    
        if (FAILED( psl->QueryInterface(IID_IPersistFile, (void **) &ppf) ))
            goto cleanup;
    
        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
    You need to create the right kind of project (Visual C++) that doesn't use the CLR.
    It is probably too late to give comment in this topics. But I couldnt resist (I even applied to be a member here) it. With only one reason to start with :
    From many C/C++ programmers codes in the internet, most are avoiding goto statements (because it is against structural principle), as the result thier codes are more unreadable (too many nested if statment). They are forget that the main goal for structural languages are for readable. So goto statments are still usefull as long as we know how to do it properly (like in this example, go to End). Good Job

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Of course there are better way still, Like the ATL but yes this is an ok use of goto.

    Code:
    CComPtr<IShellLink> psl
    Woop?

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Uninteresting thread bump
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 05:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 04:22 PM