Jose's Read Only Forum 2023

General Category => General Discussion => Topic started by: Patrice Terrier on October 23, 2008, 11:56:16 AM

Title: Info about desktop shortcut
Post by: Patrice Terrier on October 23, 2008, 11:56:16 AM
What is the best way (and easiest too) to retrieve informations about the desktop shortcuts?

- target path
- working directory
- arguments
- icon location

Currently i am using something like this one:
http://www.powerbasic.com/support/pbforums/showthread.php?t=25075&highlight=shortcut (http://www.powerbasic.com/support/pbforums/showthread.php?t=25075&highlight=shortcut)
Title: Re: Info about desktop shortcut
Post by: José Roca on October 23, 2008, 01:01:53 PM
 
This one is the easiest:


' SED_PBCC - Use the console compiler
#COMPILE EXE
#DIM ALL
#INCLUDE ONCE "WSHOM.INC"

FUNCTION PBMAIN () AS LONG

   ' Creates an instance of the Windows Script Host
   LOCAL pWsh AS IWshShell
   pWsh = NEWCOM "WScript.Shell"

   ' Creates a shortcut programatically (if it already exists, CreateShortcut opens it)
   LOCAL pLnk AS IWshShortcut
   pLnk = pWsh.CreateShortcut(UCODE$(<path of the shortcut>))

   STDOUT "Shortcut description: " & ACODE$(pLnk.Description)
   STDOUT "Shortcut working directory: " & ACODE$(pLnk.WorkingDirectory)
   STDOUT "Shortcut arguments: " & ACODE$(pLnk.Arguments)
   STDOUT "Shortcut hot key: " & ACODE$(pLnk.HotKey)
   STDOUT "Shortcut icon location: " & ACODE$(pLnk.IconLocation)
   STDOUT "Shortcut target path: " & ACODE$(pLnk.TargetPath)
   STDOUT "Shortcut window style: " & FORMAT$(pLnk.WindowStyle)

END FUNCTION

Title: Re: Info about desktop shortcut
Post by: Patrice Terrier on October 23, 2008, 01:43:39 PM
José,

Thanks.

But what i need, is the easiest way to read existing shortcut data, not to create a new one.

I need this to detect all the desktop shortcuts to populate my DockBar demo, like in MAC OS.

I know how to do it (the old way), but there may be easiest way of doing it with the new PB9 COM facilities.
Hence why i am asking there ;)

...
Title: Re: Info about desktop shortcut
Post by: Patrice Terrier on October 23, 2008, 01:56:31 PM
Ok, i think i shall go with the plain old SHELL32.DLL

DECLARE FUNCTION SHFileOperation LIB "SHELL32.DLL" ALIAS "SHFileOperationA" (lpFileOp AS SHFILEOPSTRUCT) AS LONG
DECLARE FUNCTION SHGetDesktopFolder LIB "SHELL32.DLL" ALIAS "SHGetDesktopFolder" (ppshf AS ANY) AS LONG
DECLARE FUNCTION SHGetFileInfo LIB "SHELL32.DLL" ALIAS "SHGetFileInfoA" (pszPath AS ASCIIZ, BYVAL dwFileAttributes AS DWORD, psfi AS SHFILEINFO, BYVAL cbFileInfo AS DWORD, BYVAL uFlags AS DWORD) AS DWORD
DECLARE FUNCTION SHGetPathFromIDList LIB "SHELL32.DLL" ALIAS "SHGetPathFromIDListA" (pidList AS ANY, lpBuffer AS ASCIIZ) AS LONG
DECLARE FUNCTION SHGetSpecialFolderLocation LIB "SHELL32.DLL" ALIAS "SHGetSpecialFolderLocation" (BYVAL hwndOwner AS DWORD, BYVAL nFolder AS LONG, pidl AS ANY) AS LONG
Title: Re: Info about desktop shortcut
Post by: José Roca on October 23, 2008, 03:01:17 PM
 
The code that I have posted doesn't create a shortcut, but opens an existing one and retrieves the values. Don't be fooled by the name of the method, CreateShortcut: see my remarks (if it already exists, CreateShortcut opens it).

This will be the code to create a shortcut:


' Creates an instance of the Windows Script Host
LOCAL pWsh AS IWshShell
pWsh = NEWCOM "WScript.Shell"

' Creates a shortcut programatically (if it already exists, CreateShortcut opens it)
LOCAL pLnk AS IWshShortcut
pLnk = pWsh.CreateShortcut(UCODE$(EXE.PATH$ & "Test.lnk"))

' Sets variuos properties and saves them to disk
pLnk.Description = UCODE$("Hello world")
pLnk.WorkingDirectory = UCODE$(EXE.PATH$)
pLnk.Arguments = UCODE$("/c")
pLnk.HotKey = UCODE$("Ctrl+Alt+e")
pLnk.IconLocation = UCODE$(EXE.PATH$ & "PROGRAM.ICO,0")
pLnk.RelativePath = UCODE$(EXE.PATH$)
pLnk.TargetPath = UCODE$(EXE.PATH$ & "EX_WHLNK_CreateShortcut.EXE")
pLnk.WindowStyle = %WshNormalFocus
pLnk.Save

Title: Re: Info about desktop shortcut
Post by: Patrice Terrier on October 23, 2008, 03:53:15 PM
Ok, José, i see it now!

Thanks for the head up  8)
Title: Re: Info about desktop shortcut
Post by: Patrice Terrier on October 24, 2008, 12:28:53 PM
Do you know how to retrieve the label shown under each desktop icon?

I can't get it with any of the IWshShortcut property

Added:
Ok, i found the answer myself.
Indeed the Label shown below the desktop icon matches exactly the name of the lnk file, that was too obvious :o
Title: Re: Info about desktop shortcut
Post by: José Roca on October 25, 2008, 01:36:46 AM
 
If instead of the path, you have a pidl, you can retrieve the display name calling the SHGetFileInfo function:


LOCAL dwRes AS DWORD
LOCAL shfi AS SHFILEINFO

dwRes = SHGetFileInfo(BYVAL pidl, 0, shfi, SIZEOF(shfi), %SHGFI_DISPLAYNAME)
PRINT shfi.szDisplayName

Title: Re: Info about desktop shortcut
Post by: Patrice Terrier on October 29, 2008, 03:07:56 PM
I found that the IShellLink interface is unable to resolve a ".lnk" file targeting a ".cpl" file.
Title: Re: Info about desktop shortcut
Post by: Patrice Terrier on November 20, 2008, 10:14:37 AM
If you want to see/learn how this topic has been sorted out, then look at the source code of the "OfTheBay" project.

...