Page 2 of 2

Re: Create Desktop Shortcuts (Windows)

Posted: Fri Sep 28, 2012 8:55 pm
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")

Re: Create Desktop Shortcuts (Windows)

Posted: Sat Sep 29, 2012 7:52 am
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?

Re: Create Desktop Shortcuts (Windows)

Posted: Sat Sep 29, 2012 7:58 am
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...

Re: Create Desktop Shortcuts (Windows)

Posted: Sat Sep 29, 2012 8:31 am
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.

Re: Create Desktop Shortcuts (Windows)

Posted: Sun Oct 14, 2012 11:22 am
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.

Re: Create Desktop Shortcuts (Windows)

Posted: Wed May 24, 2017 6:56 am
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.

Re: Create Desktop Shortcuts (Windows)

Posted: Fri Apr 17, 2020 9:57 am
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

Re: Create Desktop Shortcuts (Windows)

Posted: Sat May 04, 2024 9:55 pm
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.

Re: Create Desktop Shortcuts (Windows)

Posted: Sun May 05, 2024 12:36 am
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