Creating a desktop shortcut

Just starting out? Need help? Post your questions and find answers here.
swan
Enthusiast
Enthusiast
Posts: 227
Joined: Sat Jul 03, 2004 9:04 am
Location: Sydney Australia
Contact:

Creating a desktop shortcut

Post by swan »

Haven't had the need to do this before, I've created a simple installer for some files (just a copy dir really) and to finish it off I'd like to add a shortcut to the main exe that sits on the desktop.
A bit of searching found this: http://www.purebasic.fr/english/viewtopic.php?t=8668
But I can't make it work to see if this helps me.
Anyone else had the need to do this ?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Creating a desktop shortcut

Post by IdeasVacuum »

At what point does the short-cut code fail?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Creating a desktop shortcut

Post by luis »

This should work (I used it in the past)

Code: Select all


Procedure.i CreateShortcut (sTargetPath.s, sLinkPath.s, sTargetArgument.s, sTargetDescription.s, sWorkingDirectory.s, iShowCommand, sIconFile.s, iIconIndexInFile)

;/***P
;*
;* DESC
;* 	Create a shortcut for the specified target.
;*
;* IN
;*  sTargetPath         ; The full pathname of the target.
;*  sLinkPath           ; The full pathname of the shortcut link (the actual .lnk file).
;*  sTargetArgument     ; The argument(s) to be passed to the target.
;*  sTargetDescription  ; A description of the shortcut (it will be visible as a tooltip).
;*  sWorkingDirectory   ; The desired working directory for the target.
;*  iShowCommand        ; Start mode (SW_SHOWNORMAL, SW_SHOWMAXIMIZED, SW_SHOWMINIMIZED)
;*  sIconFile           ; The full pathname of the file containing the icon to be used (usually the target itself).
;*  iIconIndexInFile    ; The index for the icon to be retrieved form the icon file.
;*
;* RET
;*  0 OK
;*  1 FAILED
;*
;* EXAMPLE
;*  CreateShortcut ("C:\temp\program.exe", "C:\temp\program.lnk", "arg","A nice program.","c:\temp", #SW_SHOWMAXIMIZED, "C:\temp\program.exe", 0)
;* 
;* OS
;*  Windows
;***/ 

 Protected psl.IShellLinkW, ppf.IPersistFile
 Protected sBuffer.s
 Protected iRetVal = 1

 CoInitialize_(#Null)
  
 If CoCreateInstance_(?CLSID_ShellLink,0,1,?IID_IShellLink,@psl) = #S_OK
   
    ; The file TO which is linked ( = target for the Link )
    
    psl\SetPath(sTargetPath)
   
    ; Arguments for the Target
    
    psl\SetArguments(sTargetArgument)
   
    ; Working Directory
    
    psl\SetWorkingDirectory(sWorkingDirectory)
   
    ; Description ( also used as Tooltip for the Link )
    
    psl\SetDescription(sTargetDescription)
   
    ; Show command:
    ;   SW_SHOWNORMAL    = Default
    ;   SW_SHOWMAXIMIZED = Maximized
    ;   SW_SHOWMINIMIZED = Minimized
    
    psl\SetShowCmd(iShowCommand)
   
    ; Hotkey (not implemented): 
    ; The virtual key code is in the low-order byte,
    ; and the modifier flags are in the high-order byte.
    ; The modifier flags can be a combination of the following values:
    ;
    ;   HOTKEYF_ALT     = ALT key
    ;   HOTKEYF_CONTROL = CTRL key
    ;   HOTKEYF_EXT     = Extended key
    ;   HOTKEYF_SHIFT   = SHIFT key
   
    psl\SetHotkey(#Null)
   
    ; Icon for the Link:
    ; There can be more than 1 icons in an icon resource file,
    ; so you have to specify the index.
    
    psl\SetIconLocation(sIconFile, iIconIndexInFile)
   

    ; Query IShellLink for the IPersistFile interface for saving the
    ; shortcut in persistent storage.
   
    If psl\QueryInterface(?IID_IPersistFile, @ppf) = 0
    
        ; Ensure that the string is Unicode.
        sBuffer = Space(#MAX_PATH) 
      
        PokeS(@sBuffer, sLinkPath, -1, #PB_Unicode)
      
        ;Save the link by calling IPersistFile::Save.
        ppf\Save(sBuffer, #True)
      
        iRetVal = 0
      
        ppf\Release()
    EndIf
    
    psl\Release()
    
 EndIf
  
 CoUninitialize_()
  
 ProcedureReturn iRetVal
   
 DataSection
 
    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   
   
EndProcedure
"Have you tried turning it off and on again ?"
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Creating a desktop shortcut

Post by IdeasVacuum »

Rashad has published some code too:

http://www.purebasic.fr/english/viewtop ... 89#p340289
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
swan
Enthusiast
Enthusiast
Posts: 227
Joined: Sat Jul 03, 2004 9:04 am
Location: Sydney Australia
Contact:

Re: Creating a desktop shortcut

Post by swan »

Thanx guys.
I chased one down by utopiomania as well. Few choices now. Am favoring the one by Rashad.
And big thanx to all the original coders (danilo, Rashad, utopiomania, luis) ...
Post Reply