Default shortcut name pattern

Windows specific forum
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Default shortcut name pattern

Post by Dude »

When you right-click a file in Windows and select "Create shortcut", it defaults to a specific name pattern depending on the OS and language used. Here's an image of "iexplore.exe" with two shortcuts created; one from English Windows, and one from Vietnamese Windows:

Image

My question is: my app creates shortcuts for the user (from this code: viewtopic.php?p=512146#p512146) but I want to use the default naming pattern as the language of the OS used. After all, there's no point in me naming it "Shortcut" on a Vietnamese OS, where the user will be expecting to see "Lối tắt".

And to throw a further problem in: on Win XP, the shortcut pattern is "Shortcut to <file>" instead, which makes it even harder to use the correct name pattern. So, does anyone have any ideas on how to resolve these issues? Is there maybe a way for Windows to return what the default shortcut name will be, before I try to create it? Thank you.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4637
Joined: Sun Apr 12, 2009 6:27 am

Re: Default shortcut name pattern

Post by RASHAD »

You will need Admin rights
And do not use Explorer Sendto ,use the next snippet

Code: Select all

Global obj.s, lnk.s

Procedure.s GetSpecialFolder(id)
  Protected path.s, *ItemId.ITEMIDLIST

  *itemId = #Null
  If SHGetSpecialFolderLocation_(0, id, @*ItemId) = #NOERROR
    path = Space(#MAX_PATH)
    If SHGetPathFromIDList_(*itemId, @path)
      If Right(path, 1) <> "\"
        path + "\"
      EndIf
      ProcedureReturn path
    EndIf
  EndIf
  ProcedureReturn ""
EndProcedure

Procedure CreateShellLink(obj.s, lnk.s, arg.s, desc.s, dir.s, icon.s, index)
  Protected hRes.l, mem.s, ppf.IPersistFile
  CompilerIf #PB_Compiler_Unicode
    Protected psl.IShellLinkW
  CompilerElse
    Protected psl.IShellLinkA
  CompilerEndIf

  CoInitialize_(0)
  hRes = CoCreateInstance_(?CLSID_ShellLink, 0, 1, ?IID_IShellLink, @psl)

  If hRes = 0
    psl\SetPath(Obj)
    psl\SetArguments(arg)
    psl\SetDescription(desc)
    psl\SetWorkingDirectory(dir)
    psl\SetIconLocation(icon, index)
    hRes = psl\QueryInterface(?IID_IPersistFile, @ppf)

    If hRes = 0
        hRes = ppf\Save(lnk, #True)
      ppf\Release()
    EndIf
    psl\Release()
  EndIf
  
  CoUninitialize_()

  DataSection
    CLSID_ShellLink:
    Data.l $00021401
    Data.w $0000,$0000
    Data.b $C0,$00,$00,$00,$00,$00,$00,$46
    IID_IShellLink:
    CompilerIf #PB_Compiler_Unicode
      Data.l $000214F9
    CompilerElse
      Data.l $000214EE
    CompilerEndIf
    Data.w $0000,$0000
    Data.b $C0,$00,$00,$00,$00,$00,$00,$46
    IID_IPersistFile:
    Data.l $0000010b
    Data.w $0000,$0000
    Data.b $C0,$00,$00,$00,$00,$00,$00,$46
  EndDataSection
  ProcedureReturn hRes
EndProcedure

#CSIDL_WINDOWS = $24
#CSIDL_DESKTOPDIRECTORY = $10

obj = "c:\MMR Sound Player\Sound Player.exe"
llnk.s = GetFilePart(obj,#PB_FileSystem_NoExtension)
lnk = GetSpecialFolder(#CSIDL_DESKTOPDIRECTORY) + llnk+".lnk"

If createShellLink(obj, lnk, "", "", "C:\", obj, 0) = 0
  MessageRequester("OK", "Link created on the desktop", #PB_MessageRequester_Ok)
EndIf
End
Egypt my love
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Default shortcut name pattern

Post by spikey »

This will extract the relevant resource strings from the DLL, it doesn't need admin rights. The first is the actual localisation, the second is a hint as to how the OS will apply it. That should give you the information you need. There is no guarantee that the string ids will remain consistent across major versions though, so you will need to check for ID changes in other versions (these are the IDs for Windows 7).

Code: Select all

Define.I hLibrary
Define.S sBuffer

hLibrary = OpenLibrary(0, "SHELL32.DLL")
sBuffer = Space(255)

LoadString_(hLibrary, 4153, @sBuffer, 255)
Debug sBuffer
LoadString_(hLibrary, 4154, @sBuffer, 255)
Debug sBuffer

CloseLibrary(0)
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Default shortcut name pattern

Post by Dude »

Hi Rashad, I tried your code but it doesn't show the default shortcut name; it just seems to create a shortcut without the default name?

Spikey: your code is what I was looking for (especially the 4154 ID code). But I noticed that you need to hard-code the library number to make it work? If I use OpenLibrary with #PB_Any, then it fails. Looks like a PureBasic bug? I've used #PB_Any with OpenLibrary before so I don't know why this particular snippet is failing.

Code: Select all

Define.I hLibrary
Define.S sBuffer

hLibrary = OpenLibrary(#PB_Any, "SHELL32.DLL")
sBuffer = Space(255)

Debug LoadString_(hLibrary, 4154, @sBuffer, 255) ; Shows "0"
Debug sBuffer ; Shows ""

CloseLibrary(hLibrary)
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Default shortcut name pattern

Post by Little John »

Dude wrote:Spikey: your code is what I was looking for (especially the 4154 ID code). But I noticed that you need to hard-code the library number to make it work? If I use OpenLibrary with #PB_Any, then it fails. Looks like a PureBasic bug?
No, it's a bug in your code.

If a self-chosen library number is used like in Spikey's code, then OpenLibrary() returns a handle, and that's what LoadString_() expects as input. But if you use #PB_Any, then OpenLibrary() returns a number rather than a handle, and you have to use LibraryID() in your code:

Code: Select all

Define.I hLibrary
Define.S sBuffer

hLibrary = OpenLibrary(#PB_Any, "SHELL32.DLL")
sBuffer = Space(255)

Debug LoadString_(LibraryID(hLibrary), 4154, @sBuffer, 255)
Debug sBuffer

CloseLibrary(hLibrary)
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Default shortcut name pattern

Post by Dude »

Hi Little John,

Ah, so because I was using an API call, I needed the LibraryID(). Makes sense. :)

[Edited to remove my mistake with a further question] :oops:
Last edited by Dude on Sun Sep 01, 2019 8:18 am, edited 1 time in total.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Default shortcut name pattern

Post by Little John »

Dude wrote:But, when I use this example from the Help manual, ...
Where exactly in the manual is that example?
I need to look at the original code.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Default shortcut name pattern

Post by Dude »

Never mind, I made a mistake. Post edited. Thanks for your guidance. :)
Post Reply