Create Desktop Shortcuts (Windows)

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Create Desktop Shortcuts (Windows)

Post by RASHAD »

OK Andi
You are all right ,I just tested it with XP
Try the next

Code: Select all

Procedure DeleteLink(ExeName$)
Path$ = GetHomeDirectory() + "Desktop\"
LinkName$ = ReplaceString(GetFilePart(ExeName$),"exe","lnk")
LinkName$ = Path$+ LinkName$
DeleteFile(LinkName$)
EndProcedure

DeleteLink("Command Prompt.exe")
Egypt my love
Andi
User
User
Posts: 21
Joined: Fri Sep 12, 2008 2:43 pm
Location: Berlin

Re: Create Desktop Shortcuts (Windows)

Post by Andi »

Thank you, Rashad. Your solution is very elegant. A pity that I was not able to find it. :wink:

btw: Is there any chance to delete a shortcut or to change a shortcut [e. g. from strg+1 to alt+F7] using PB in combination with WinApi?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

Re: Create Desktop Shortcuts (Windows)

Post by jassing »

the delete rashad pointed pointed out is very limitied.

For instance, have a shortcut "Dos Box" which points to "command.exe"

so I would say DeleteLink("dosbox.exe") for that to work...
if you want to delete shortcuts that point to a specific exe, you have to inspect all the shortcuts...
I can rename a shortcut to "Go to dos prompt" or "dos here" and it will not work as coded...
Andi
User
User
Posts: 21
Joined: Fri Sep 12, 2008 2:43 pm
Location: Berlin

Re: Create Desktop Shortcuts (Windows)

Post by Andi »

@jassing
I'm aware of what you say. When the name of the link e. g. "Dos box" is changed to whatever, then it's getting complicate. But in my case all the names of the .lnk files are known and these names will not be changed. Sure, I can change and delete shortcuts by right click the corresponding icon. But it would be nice doing this with a PB/WinApi-exe.
User avatar
TI-994A
Addict
Addict
Posts: 2698
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Create Desktop Shortcuts (Windows)

Post by TI-994A »

---> netmaestro has provided the solution to this issue here. Thank you netmaestro.

Hello all! Great examples. Instead of using the data block to read the CLSIDs, would it be possible to use the CLSIDFromString() function instead? I've been tinkering with it, but just can't seem to make it work (these are just the changes that I tried - it's not the complete code):

Code: Select all

  ...
  ...
  Define CLSID_ShellLink.clsid, IID_IShellLink.CLSID, IID_IPersistFile.CLSID
  Define.s shlCLSID, shlIID, pfIID
  
  CompilerIf #PB_Compiler_Unicode
    Define psl.IShellLinkW
    shlIID = "{000214F9-0000-0000-C000-000000000046}"
  CompilerElse
    Define psl.IShellLinkA
    shlIID = "{000214EE-0000-0000-C000-000000000046}"
  CompilerEndIf
  
  hRes =  CLSIDFromString_(@shlIID ,IID_IShellLink)
  
  shlCLSID = "{00021401-0000-0000-C000-000000000046}"
  hRes =  CLSIDFromString_(@shlCLSID, @CLSID_ShellLink)
  
  pfIID = "{0000010B-0000-0000-C000-000000000046}"  
  hRes =  CLSIDFromString_(@pfIID, @IID_IPersistFile)  
  ...
  ...
  CoInitialize_(0)
  ;replaced the following:
  ;hRes = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLink, @psl)
  hRes = CoCreateInstance_(@CLSID_ShellLink, 0, 1, @IID_IShellLink, @psl)
  
  If hRes = 0
    ...
    ...
    ;replaced the following:    
    ;hRes = psl\QueryInterface(?IID_IPersistFile, @ppf)
    hRes = psl\QueryInterface(@IID_IPersistFile, @ppf)
  ...
  ...
Essentially, I've made only two changes to the original code, and the code works if I change them back. What am I doing wrong? Can anyone help me with this please?

Thank you.

---> netmaestro has provided the solution to this issue here. Thank you netmaestro.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
a_carignan
User
User
Posts: 98
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: Create Desktop Shortcuts (Windows)

Post by a_carignan »

@RASHAD
I'd like to have a template using the shortcut property of the object you're using with. I know that the IShellLink object has a SetHotkey function and I know it works fine, but I do not know how to undo the following shortcut "Ctrl + SHIFT + D". I would also like to know the constant values of the other control keys.
Thank you in advance.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Create Desktop Shortcuts (Windows)

Post by collectordave »

New to this.

Trying to use the code to create a shortcut if I use.

Code: Select all

obj = getSpecialFolder(#CSIDL_WINDOWS) + "World Stamp Collector.exe"
lnk = getSpecialFolder(#CSIDL_DESKTOPDIRECTORY) + "WSC.lnk"
The shortcut is created but tells me it cannot find the exe file to run it if i browse it then starts ok each time after.

Thought to give it the full path name.

Code: Select all

obj = getSpecialFolder(#CSIDL_WINDOWS) + "C:\Users\User\Documents\Temp\World Stamp Collector\World Stamp Collector.exe"
lnk = getSpecialFolder(#CSIDL_DESKTOPDIRECTORY) + "WSC.lnk"
The shortcut is then not created?

Any help appreciated


PS just changed one line to

Code: Select all

obj = "C:\Users\User\Documents\Temp\World Stamp Collector\World Stamp Collector.exe"
and the link was created with the correct icon just not the right working diretory

PPS

Ok I think I am getting it a bit now changed another line to

Code: Select all

If createShellLink(obj, lnk, "", "Start WSC", "C:\Users\User\Documents\Temp\World Stamp Collector\", obj, 0) = 0
Works fine now.


CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
a_carignan
User
User
Posts: 98
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: Create Desktop Shortcuts (Windows)

Post by a_carignan »

collectordave wrote: Fri Apr 17, 2020 9:57 am New to this.

Trying to use the code to create a shortcut if I use.

Code: Select all

obj = getSpecialFolder(#CSIDL_WINDOWS) + "World Stamp Collector.exe"
lnk = getSpecialFolder(#CSIDL_DESKTOPDIRECTORY) + "WSC.lnk"
The shortcut is created but tells me it cannot find the exe file to run it if i browse it then starts ok each time after.

Thought to give it the full path name.

Code: Select all

obj = getSpecialFolder(#CSIDL_WINDOWS) + "C:\Users\User\Documents\Temp\World Stamp Collector\World Stamp Collector.exe"
lnk = getSpecialFolder(#CSIDL_DESKTOPDIRECTORY) + "WSC.lnk"
The shortcut is then not created?

Any help appreciated


PS just changed one line to

Code: Select all

obj = "C:\Users\User\Documents\Temp\World Stamp Collector\World Stamp Collector.exe"
and the link was created with the correct icon just not the right working diretory

PPS

Ok I think I am getting it a bit now changed another line to

Code: Select all

If createShellLink(obj, lnk, "", "Start WSC", "C:\Users\User\Documents\Temp\World Stamp Collector\", obj, 0) = 0
Works fine now.


CD
Hi, getSpecialFolder does not exist. Thanks anyway.
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Create Desktop Shortcuts (Windows)

Post by idle »

old code use to create desktop short cut and add to start up

Code: Select all

CompilerIf #PB_Compiler_OS = #PB_OS_Windows 

Procedure CreateShellLink(PATH$, LINK$, Argument$, DESCRIPTION$, WorkingDirectory$, ShowCommand.l, HotKey.l, IconFile$, IconIndexInFile.l)
  Protected psl.IShellLinkA,ppf.IPersistFile,*mem,len,hres,res.s
  
  CoInitialize_(0)
  If CoCreateInstance_(?CLSID_ShellLink,0,1,?IID_IShellLink,@psl.IShellLinkA) = 0
     
   Set_ShellLink_preferences:
   psl\SetPath(PATH$)
   psl\SetArguments(Argument$)
   psl\SetWorkingDirectory(WorkingDirectory$)
   psl\SetDescription(DESCRIPTION$)
   psl\SetShowCmd(ShowCommand)
   psl\SetHotkey(HotKey)
   psl\SetIconLocation(IconFile$, IconIndexInFile)
        
   ShellLink_SAVE:
      If psl\QueryInterface(?IID_IPersistFile,@ppf.IPersistFile) = 0
        hres = ppf\Save(link$,#True)
        ppf\Release()
      EndIf
      psl\Release()
   EndIf
   CoUninitialize_()
   ProcedureReturn hres ! 1
   
   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

Procedure.s GetSpecialFolderLocation(Value.l)
  Protected Folder_ID,SpecialFolderLocation.s
 
  If SHGetSpecialFolderLocation_(0, Value, @Folder_ID) = 0
    SpecialFolderLocation = Space(#MAX_PATH*2)
    SHGetPathFromIDList_(Folder_ID, @SpecialFolderLocation)
    If SpecialFolderLocation
      If Right(SpecialFolderLocation, 1) <> "\"
        SpecialFolderLocation + "\"
      EndIf
    EndIf
    CoTaskMemFree_(Folder_ID)
  EndIf
   ProcedureReturn SpecialFolderLocation.s
EndProcedure

#CSIDL_STARTUP = $7
#CSIDL_APPDATA = $1A
#CSIDL_DESKTOP = 0  

Procedure DeleteDesktoplink(prog.s) 
  
  Protected path.s = GetSpecialFolderLocation(#CSIDL_DESKTOP) + prog + ".lnk"
  
  If FileSize(path) 
    DeleteFile(path,#PB_FileSystem_Force) 
  EndIf    
  
EndProcedure   

Procedure DeleteStartUplink(prog.s) 
  
  Protected path.s = GetSpecialFolderLocation(#CSIDL_STARTUP) + prog + ".lnk"
  
  If FileSize(path) 
    DeleteFile(path,#PB_FileSystem_Force) 
  EndIf
  
 EndProcedure  

Procedure ShellLinkAddtoDesktop(prog.s) 
  
  Protected tpath.s = GetSpecialFolderLocation(#CSIDL_DESKTOP) + prog + ".lnk"
  CreateShellLink(ProgramFilename(),tpath,"",prog,"",0,0,ProgramFilename(),0)
  Debug Tpath 
  
 EndProcedure  

Procedure ShellLinkAddtoStartMenu(prog.s)
  Protected tpath.s = GetSpecialFolderLocation(#CSIDL_STARTUP) + prog + ".lnk"
  CreateShellLink(ProgramFilename(),tpath,"",prog,"",0,0,ProgramFilename(),0)
  Debug Tpath 
EndProcedure 

CompilerElse 
  
  Procedure ShellLinkAddtoDesktop(prog.s) 
    
  EndProcedure 
  
  Procedure ShellLinkAddtoStartMenu(prog.s) 
    
  EndProcedure   
 
 
CompilerEndIf  


Post Reply