C++解析lnk

源代码:

#include <shobjidl.h>
#include <shlguid.h>
#include <stdio.h>
#include <locale.h>

static HRESULT ResolveIt(HWND hwnd, TCHAR *lpszLinkFile, TCHAR *lpszPath, int iPathBufferSize) 
{ 
    HRESULT hres; 
    IShellLink* psl; 
    WIN32_FIND_DATA wfd; 
 
    *lpszPath = 0; // Assume failure 

    // Get a pointer to the IShellLink interface. It is assumed that CoInitialize
    // has already been called. 
    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)) 
        {
            // Add code here to check return value from MultiByteWideChar 
            // for success.
 
            // Load the shortcut. 
            hres = ppf->Load(lpszLinkFile, 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(lpszPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_RAWPATH); 
                } 
            } 

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

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

int main()
{
	setlocale(LC_ALL, "chs");

	TCHAR szLinkFilePath[MAX_PATH] = L"C:\\Documents and Settings\\All Users\\桌面\\腾讯QQ.lnk";
	TCHAR szLinkFileExePath[MAX_PATH]={0};

	CoInitialize(NULL);
	ResolveIt(NULL, szLinkFilePath, szLinkFileExePath, MAX_PATH);
	wprintf(L"%s\n", szLinkFileExePath);
	CoUninitialize();

	return 0;
}


你可能感兴趣的:(C++解析lnk)