C++解析lnk

原文转载于:https://blog.csdn.net/feier7501/article/details/11103729

源代码:

[cpp]  view plain  copy
  1. #include <shobjidl.h>  
  2. #include <shlguid.h>  
  3. #include <stdio.h>  
  4. #include <locale.h>  
  5.   
  6. static HRESULT ResolveIt(HWND hwnd, TCHAR *lpszLinkFile, TCHAR *lpszPath, int iPathBufferSize)   
  7. {   
  8.     HRESULT hres;   
  9.     IShellLink* psl;   
  10.     WIN32_FIND_DATA wfd;   
  11.    
  12.     *lpszPath = 0; // Assume failure   
  13.   
  14.     // Get a pointer to the IShellLink interface. It is assumed that CoInitialize  
  15.     // has already been called.   
  16.     hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);   
  17.     if (SUCCEEDED(hres))   
  18.     {   
  19.         IPersistFile* ppf;   
  20.    
  21.         // Get a pointer to the IPersistFile interface.   
  22.         hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);   
  23.           
  24.         if (SUCCEEDED(hres))   
  25.         {  
  26.             // Add code here to check return value from MultiByteWideChar   
  27.             // for success.  
  28.    
  29.             // Load the shortcut.   
  30.             hres = ppf->Load(lpszLinkFile, STGM_READ);   
  31.               
  32.             if (SUCCEEDED(hres))   
  33.             {   
  34.                 // Resolve the link.   
  35.                 hres = psl->Resolve(hwnd, 0);   
  36.   
  37.                 if (SUCCEEDED(hres))   
  38.                 {   
  39.                     // Get the path to the link target.   
  40.                     hres = psl->GetPath(lpszPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_RAWPATH/*SLGP_SHORTPATH*/);   
  41.                 }   
  42.             }   
  43.   
  44.             // Release the pointer to the IPersistFile interface.   
  45.             ppf->Release();   
  46.         }   
  47.   
  48.         // Release the pointer to the IShellLink interface.   
  49.         psl->Release();   
  50.     }   
  51.     return hres;   
  52. }  
  53.   
  54. int main()  
  55. {  
  56.     setlocale(LC_ALL, "chs");  
  57.   
  58.     TCHAR szLinkFilePath[MAX_PATH] = L"C:\\Documents and Settings\\All Users\\桌面\\腾讯QQ.lnk";  
  59.     TCHAR szLinkFileExePath[MAX_PATH]={0};  
  60.   
  61.     CoInitialize(NULL);  
  62.     ResolveIt(NULL, szLinkFilePath, szLinkFileExePath, MAX_PATH);  
  63.     wprintf(L"%s\n", szLinkFileExePath);  
  64.     CoUninitialize();  
  65.   
  66.     return 0;  
  67. }  

猜你喜欢

转载自blog.csdn.net/business122/article/details/80768835