Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.1 KiB

  1. #include "femgrate.h"
  2. #include <objbase.h>
  3. #include <shellapi.h>
  4. #include <shlguid.h>
  5. #include <comdef.h>
  6. HRESULT FixPathInLink(LPCTSTR pszShortcutFile, LPCTSTR lpszOldSubStr,LPCTSTR lpszNewSubStr)
  7. {
  8. HRESULT hres;
  9. IShellLink *psl;
  10. TCHAR szGotPath [MAX_PATH];
  11. TCHAR szNewPath [MAX_PATH];
  12. WIN32_FIND_DATA wfd;
  13. CoInitialize(NULL);
  14. // Get a pointer to the IShellLink interface.
  15. hres = CoCreateInstance (CLSID_ShellLink,
  16. NULL,
  17. CLSCTX_INPROC_SERVER,
  18. IID_IShellLink,
  19. (void **)&psl);
  20. if (SUCCEEDED (hres)) {
  21. IPersistFile *ppf;
  22. // Get a pointer to the IPersistFile interface.
  23. hres = psl->QueryInterface (IID_IPersistFile, (void **)&ppf);
  24. if (SUCCEEDED (hres)) {
  25. // Load the shortcut.
  26. hres = ppf->Load (pszShortcutFile, STGM_READWRITE );
  27. if (SUCCEEDED (hres)) {
  28. // Resolve the shortcut.
  29. hres = psl->Resolve (NULL, SLR_NO_UI | SLR_UPDATE);
  30. if (SUCCEEDED (hres)) {
  31. lstrcpy (szGotPath, pszShortcutFile);
  32. // Get the path to the shortcut target.
  33. hres = psl->GetPath (szGotPath,
  34. MAX_PATH,
  35. (WIN32_FIND_DATA *)&wfd,
  36. SLGP_SHORTPATH);
  37. if (! SUCCEEDED (hres)) {
  38. DebugMsg((DM_VERBOSE, TEXT("FixPathInLink: GetPath %s Error = %d\n"), szGotPath,hres));
  39. } else {
  40. DebugMsg((DM_VERBOSE, TEXT("FixPathInLink: GetPath %s OK \n"), szGotPath));
  41. }
  42. if (ReplaceString(szGotPath,lpszOldSubStr, lpszNewSubStr, szNewPath)) {
  43. hres = psl->SetPath (szNewPath);
  44. if (! SUCCEEDED (hres)) {
  45. DebugMsg((DM_VERBOSE, TEXT("FixPathInLink: SetPath %s Error = %d\n"), szGotPath,hres));
  46. } else {
  47. hres = ppf->Save (pszShortcutFile,TRUE);
  48. if (! SUCCEEDED (hres)) {
  49. DebugMsg((DM_VERBOSE, TEXT("FixPathInLink: Save %s Error = %d\n"), pszShortcutFile,hres));
  50. } else {
  51. DebugMsg((DM_VERBOSE, TEXT("FixPathInLink: Save %s OK = %d\n"), pszShortcutFile,hres));
  52. }
  53. }
  54. } else {
  55. DebugMsg((DM_VERBOSE, TEXT("FixPathInLink: No match ! %s , %s, %s = %d\n"), szGotPath,lpszOldSubStr, lpszNewSubStr));
  56. }
  57. }
  58. } else {
  59. DebugMsg((DM_VERBOSE, TEXT("FixPathInLink: Load %s Error = %d\n"), pszShortcutFile,hres));
  60. }
  61. // Release the pointer to IPersistFile.
  62. ppf->Release ();
  63. }
  64. // Release the pointer to IShellLink.
  65. psl->Release ();
  66. }
  67. CoUninitialize();
  68. return hres;
  69. }