Creating Desktop Shortcut with COM

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Creating Desktop Shortcut with COM

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.

This is Danilo's code using the official IShellLink COM interface.
I searched the forum for it, but I didn't find it, maybe it's lost since we had a Forum-Crash back in October.
Found Danilo's code on my HD and will post it here for your convenience.
Danilo, thanks again for your great code :)

Code broken, see post below for a working one.

Have a nice day...

Franco
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Danilo.

Its still there... archived
http://www.curvesoftware.co.uk/purebasi ... IC_ID=1971

cya,
...Danilo

(registered PureBasic user)
CherokeeStalker
User
User
Posts: 66
Joined: Fri Oct 17, 2003 2:42 am

Post by CherokeeStalker »

Not There Any More ! Error 404
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

anyway, the post is about 2 years old.
Fred
Administrator
Administrator
Posts: 18207
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Code updated for 5.20+

Here is the same code, using interfaces instead of CallCOM:

Code: Select all

Procedure CreateLink(PATH$, LINK$, Argument$, DESCRIPTION$, WorkingDirectory$, ShowCommand.l, HotKey.l, IconFile$, IconIndexInFile.l)
  
  result = 0
  CoInitialize_(0)
  If CoCreateInstance_(?CLSID_ShellLink,0,1,?IID_IShellLink,@ShellLink.IShellLinkA) = 0
    
    ;-Set_ShellLink_preferences:
    
    ; The file TO which is linked ( = target for the Link )
    ;
    ShellLink\SetPath(PATH$)
    
    ; Arguments for the Target
    ;
    ShellLink\SetArguments(Argument$)
    
    ; Working Directory
    ;
    ShellLink\SetWorkingDirectory(WorkingDirectory$)
    
    ; Description ( also used as Tooltip for the Link )
    ;
    ShellLink\SetDescription(DESCRIPTION$)
    
    ; Show command:
    ;               SW_SHOWNORMAL    = Default
    ;               SW_SHOWMAXIMIZED = aehmm... Maximized
    ;               SW_SHOWMINIMIZED = play Unreal Tournament
    ShellLink\SetShowCmd(ShowCommand)
    
    ; Hotkey:
    ; 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
    ;
    ShellLink\SetHotkey(HotKey)
    
    ; Set Icon for the Link:
    ; There can be more than 1 icons in an icon resource file,
    ; so you have to specify the index.
    ;
    ShellLink\SetIconLocation(IconFile$, IconIndexInFile)
    
    
    ;-ShellLink_SAVE:
    ; Query IShellLink For the IPersistFile interface For saving the
    ; shortcut in persistent storage.
    If ShellLink\QueryInterface(?IID_IPersistFile, @PersistFile.IPersistFile) = 0
      ; Ensure that the string is Unicode.
      ;Save the link by calling IPersistFile::Save.
      ;hres = ppf->Save(wsz, TRUE);
      hres = PersistFile\SAVE(LINK$, #True)
      result = 1
      PersistFile\Release()
    EndIf
    ShellLink\Release()
  EndIf
  CoUninitialize_()
  ProcedureReturn result
  
  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_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
    IID_IPersistFile:
    ; 0000010b-0000-0000-C000-000000000046
    Data.l $0000010b
    Data.w $0000,$0000
    Data.b $C0,$00,$00,$00,$00,$00,$00,$46
  EndDataSection
  
EndProcedure



; CreateLink
;             - TARGET$ for the Link ("c:\PureBasic\purebasic.exe")
;             - LINK$ - name of the Link ("c:\pb.lnk")
;             - Argument$ for the target  ("%1")
;             - Description$ = Description and Tooltip ("Start PureBasic")
;             - Working Directory ("c:\PureBasic\")
;             - Show command: #SW_SHOWNORMAL or #SW_SHOWMAXIMIZED or #SW_SHOWMINIMIZED
;             - HotKey - no need to use this  FPRIVATE "TYPE=PICT;ALT=icon_smile.gif"
;             - IconFile + Index ( "c:\PureBasic\purebasic.exe" , 1 )


;If CreateLink("D:\BASIC\PureBasic\purebasic.exe","c:\PB.lnk","","Pure FUN","D:\BASIC\PureBasic\",#SW_SHOWMAXIMIZED,0,"%SystemRoot%\system32\SHELL32.dll",12)
;  Debug "Success !"
;EndIf

WinDir$ = Space(100): GetSystemDirectory_(WinDir$,100)
If CreateLink(WinDir$+"\calc.exe","c:\amd\CALC.lnk","","Calculator","",0,0,"%SystemRoot%\system32\SHELL32.dll",23)
  Debug "Success !"
Else
  Debug "Failed"
EndIf
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post by thefool »

But if you want, there is an userlib calles SETUP made by Andreas Miethe.
You should be able to get it at purearea.net
That should make it easier, and it have a lot of other functions too.
Post Reply