Shortcuts unchanged, but its target object path changed, how

Just starting out? Need help? Post your questions and find answers here.
User avatar
gurj
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

Shortcuts unchanged, but its target object path changed, how

Post by gurj »

Shortcuts unchanged, but its target object path changed, how to read and edit the original shortcut

I have some files in a folder ,and created shortcuts for them in the start menu, now I moved this folder, want to modify these paths in these shortcuts, but the read path is blank:

Code: Select all

;{ShortcutTarget()
 ;   Description: Adds support for reading the object path of a shortcut file it links to
;        Author: Jilocasin (Axolotl: updated for support windows 10 and unicode)
;          Date: 2016-05-26
;            OS: Windows
; English-Forum: 
;  French-Forum: 
;  German-Forum: http://www.purebasic.fr/german/viewtopic.php?p=335517#p335517
; -----------------------------------------------------------------------------
CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
  CompilerError "Supported OS are only: Windows"
CompilerEndIf

Procedure.s ShortcutTarget(ShortcutFile.s)

  Protected Result.s, *buffer

  If Right(LCase(ShortcutFile), 4) <> ".lnk"
    ProcedureReturn ShortcutFile
  EndIf
  CoInitialize_(0)

  Protected ShellLink.IShellLinkW  ;' A..W (unicode!)
  Protected LinkFile.IPersistFile

  If CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLink, @ShellLink) = #S_OK
    If ShellLink\QueryInterface(?IID_IPersistFile, @LinkFile) = #S_OK
      *buffer = AllocateMemory(1024)
      If *buffer
        If LinkFile\Load(ShortcutFile, 0) = #S_OK
          If ShellLink\Resolve(0, 1) = #S_OK
            ShellLink\GetPath(*buffer, 1024, 0, 0)
            Result = PeekS(*buffer)
          EndIf
        EndIf
        FreeMemory(*buffer)
      EndIf
      LinkFile\Release()
    EndIf
    ShellLink\Release()
  EndIf
  CoUninitialize_()
  ProcedureReturn Result
EndProcedure

DataSection ;{ CLSID_ShellLink, IID_IPersistFile, ...
  CLSID_ShellLink:
  ; 00021401-0000-0000-C000-000000000046
  Data.l $00021401
  Data.w $0000,$0000
  Data.b $C0,$00,$00,$00,$00,$00,$00,$46

  IID_IPersistFile:
  ; 0000010b-0000-0000-C000-000000000046
  Data.l $0000010B
  Data.w $0000,$0000
  Data.b $C0,$00,$00,$00,$00,$00,$00,$46

  CompilerIf #PB_Compiler_Unicode = 0

  IID_IShellLink:
  ; DEFINE_SHLGUID(IID_IShellLinkA, 0x000214EEL, 0, 0);
  ; C000-000000000046
  Data.l $000214EE
  Data.w $0000,$0000
  Data.b $C0,$00,$00,$00,$00,$00,$00,$46

  CompilerElse

  IID_IShellLink: ; {000214F9-0000-0000-C000-000000000046}
  Data.l $000214F9
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46

  CompilerEndIf
 EndDataSection
 ;}
 ;TODO   C:\a.exe to C:\a.lnk,and move C:\a.exe to C:\1\
Debug ShortcutTarget("C:\a.lnk")

; IDE Options = PureBasic 5.71 LTS (Windows - x86)
; CursorPosition = 79
; FirstLine = 56
; Folding = --
; EnableXP
; DPIAware
my pb for chinese:
http://ataorj.ys168.com
User avatar
gurj
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

Re: Shortcuts unchanged, but its target object path changed,

Post by gurj »

change a '=' to <>, ok
If ShellLink\Resolve(0, 1) <> #S_OK
my pb for chinese:
http://ataorj.ys168.com
User avatar
gurj
Enthusiast
Enthusiast
Posts: 664
Joined: Thu Jan 22, 2009 3:48 am
Location: china
Contact:

Re: Shortcuts unchanged, but its target object path changed,

Post by gurj »

read and edit path:

Code: Select all

;{ShortcutTarget()
 ;   Description: Adds support for reading the object path of a shortcut file it links to
;        Author: Jilocasin (Axolotl: updated for support windows 10 and unicode)
;          Date: 2016-05-26
;            OS: Windows
; English-Forum: 
;  French-Forum: 
;  German-Forum: http://www.purebasic.fr/german/viewtopic.php?p=335517#p335517
; -----------------------------------------------------------------------------
CompilerIf #PB_Compiler_OS <> #PB_OS_Windows
  CompilerError "Supported OS are only: Windows"
CompilerEndIf

Procedure.s ShortcutTarget(ShortcutFile.s)

  Protected Result.s, *buffer

  If Right(LCase(ShortcutFile), 4) <> ".lnk"
    ProcedureReturn ShortcutFile
  EndIf
  CoInitialize_(0)

  Protected ShellLink.IShellLinkW  ;' A..W (unicode!)
  Protected LinkFile.IPersistFile

  If CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLink, @ShellLink) = #S_OK
    If ShellLink\QueryInterface(?IID_IPersistFile, @LinkFile) = #S_OK
      *buffer = AllocateMemory(1024)
      If *buffer
    If LinkFile\Load(ShortcutFile, 0) = #S_OK
      ShellLink\GetPath(*buffer, 1024, 0, 0)
      Result = PeekS(*buffer)
     If ShellLink\Resolve(0, 1) <> #S_OK
      result=ReplaceString(result,"C:\","C:\1\",#PB_String_NoCase ,1,1 )
      ShellLink\SetPath(result)
      LinkFile\Save(ShortcutFile, #True)
     EndIf
    EndIf
        FreeMemory(*buffer)
      EndIf
      LinkFile\Release()
    EndIf
    ShellLink\Release()
  EndIf
  CoUninitialize_()
  ProcedureReturn Result
EndProcedure

DataSection ;{ CLSID_ShellLink, IID_IPersistFile, ...
  CLSID_ShellLink:
  ; 00021401-0000-0000-C000-000000000046
  Data.l $00021401
  Data.w $0000,$0000
  Data.b $C0,$00,$00,$00,$00,$00,$00,$46

  IID_IPersistFile:
  ; 0000010b-0000-0000-C000-000000000046
  Data.l $0000010B
  Data.w $0000,$0000
  Data.b $C0,$00,$00,$00,$00,$00,$00,$46

  CompilerIf #PB_Compiler_Unicode = 0

  IID_IShellLink:
  ; DEFINE_SHLGUID(IID_IShellLinkA, 0x000214EEL, 0, 0);
  ; C000-000000000046
  Data.l $000214EE
  Data.w $0000,$0000
  Data.b $C0,$00,$00,$00,$00,$00,$00,$46

  CompilerElse

  IID_IShellLink: ; {000214F9-0000-0000-C000-000000000046}
  Data.l $000214F9
  Data.w $0000, $0000
  Data.b $C0, $00, $00, $00, $00, $00, $00, $46

  CompilerEndIf
 EndDataSection
 ;}
 ;TODO   C:\a.exe to C:\a.lnk,and move C:\a.exe to C:\1\
Debug ShortcutTarget("C:\a.lnk")
my pb for chinese:
http://ataorj.ys168.com
Post Reply