링크파일(*.lnk)의 대상 추출하기

링크파일(*.lnk)의 대상 추출하기

 

링크파일에서 대상 파일의 Full Path를 찾는 방법이다....

 

    HRESULT hres;
    IShellLink* psl;
    char szGotPath[MAX_PATH];
    char szDescription[MAX_PATH];
    WIN32_FIND_DATA wfd;
 
    // 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. lpszLinkFile 는 Link file 의 full path이다.
            MultiByteToWideChar(CP_ACP, 0, lpszLinkFile, -1, wsz, MAX_PATH);

            // Add code here to check return value from MultiByteWideChar
            // for 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);
                }
            }
            // Release the pointer to the IPersistFile interface.
            ppf->Release();
        }
        // Release the pointer to the IShellLink interface.
        psl->Release();
    }

szGotPath로 lnk파일의 대상의 Full Path가 구해진다.

이외 ShellLinkObject 를 MSDN으로 찾아 보면 다른 정보를 알낼수 있고 혹은 lnk파일을 만들수도 있다. 

위로 스크롤