Page 1 of 1

Creating a desktop shortcut

Posted: Wed Dec 01, 2010 6:34 am
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 ?

Re: Creating a desktop shortcut

Posted: Wed Dec 01, 2010 11:06 am
by IdeasVacuum
At what point does the short-cut code fail?

Re: Creating a desktop shortcut

Posted: Thu Dec 02, 2010 6:21 pm
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

Re: Creating a desktop shortcut

Posted: Thu Dec 02, 2010 10:29 pm
by IdeasVacuum
Rashad has published some code too:

http://www.purebasic.fr/english/viewtop ... 89#p340289

Re: Creating a desktop shortcut

Posted: Fri Dec 03, 2010 4:59 am
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) ...