ElektroStudios
ElektroStudios

Reputation: 20464

Alternative way to pin an exe to Windows Taskbar?

I'm looking for a way to pin an exe and/or a shortcut into the Windows 8.1 Taskbar (not in the StartMenu) without depending on the verb name.

I did a research and I only can find code-examples like this that does not works because they are verb-name language dependant, this means it checks for a verb titled "Pin To Taskbar" and then invoke it, but that verb does not exists with English name in other languages, for example in Spanish 'Pin To Taskbar' is translated as 'Anclar a la barra de tareas', I really hope that invoking a verb parsing its verb-name is not the unique way to perform this task.

Then I wonder whether the Microsoft's WindowsAPICodePack library maybe provides a way to perform this in a more efficient way, or at least in a way that really will works.

Or maybe using the Windows API SendMessage function?

Any ideas?

Upvotes: 0

Views: 3018

Answers (3)

TLama
TLama

Reputation: 76663

There is no official way to pin application to taskbar (at this time) as far as I know, so I think you will need to rely on that hacky way with verbs. But you don't need to (should not) hardcode them; they can be read from the shell32.dll library resource string table. Some time ago I wrote a script for Inno Setup which can pin app. to taskbar by using those verbs (based on this thread). One part of that code is reading necessary verbs from the shell32.dll library and that's what I've tried to translate to VB (that's my first time with VB.NET, so take the following code just as a proof of concept):

Module Module1

    Const SHELL32_STRING_ID_PIN_TO_TASKBAR As Int32 = 5386
    Const SHELL32_STRING_ID_UNPIN_FROM_TASKBAR As Int32 = 5387

    Private Declare Auto Function LoadLibrary Lib "kernel32.dll" ( _
        ByVal lpLibFileName As String _
    ) As IntPtr

    Private Declare Function FreeLibrary Lib "kernel32.dll" ( _
        ByVal hLibModule As IntPtr _
    ) As Boolean

    Private Declare Auto Function LoadString Lib "user32.dll" ( _
        ByVal hInstance As IntPtr, _
        ByVal uID As Int32, _
        ByVal lpBuffer As String, _
        ByVal nBufferMax As Int32 _
    ) As Int32

    Public Function TryGetVerbName(ByVal ID As Int32, ByRef VerbName As String) As Boolean
        Dim Handle As IntPtr
        Dim BufLen As Int32
        Dim Buffer As String = Space(255)

        Handle = LoadLibrary("shell32.dll")
        If Not Handle.Equals(IntPtr.Zero) Then
            Try
                BufLen = LoadString(Handle, ID, Buffer, Buffer.Length)
                If BufLen <> 0 Then
                    VerbName = String.Copy(Buffer)
                    Return True
                End If
            Finally
                FreeLibrary(Handle)
            End Try
        End If

        Return False
    End Function

    Sub Main()
        Dim VerbName As String = String.Empty

        If TryGetVerbName(SHELL32_STRING_ID_PIN_TO_TASKBAR, VerbName) Then
            Console.WriteLine("Verb name for pin to taskbar: " + VerbName)
        End If

        If TryGetVerbName(SHELL32_STRING_ID_UNPIN_FROM_TASKBAR, VerbName) Then
            Console.WriteLine("Verb name for unpin from taskbar: " + VerbName)
        End If

        Console.ReadLine()
    End Sub

End Module

Upvotes: 0

Jeremy Thompson
Jeremy Thompson

Reputation: 65534

Why is there no programmatic access to the Start menu pin list?

In Windows XP we intentionally did not give programmatic access to the bold list of items at the top of the Start menu (the "pin list"). The pin list is for users to put their favorite icons. It is not the place for a program to decide unilaterally, "I am so cool. I am your favorite icon. I just know it. So I'll put myself there because, well, I'm so cool."

Because we knew that the moment we let people mess with the pin list, everybody would install themselves into it and it would become meaningless (and annoying).

What's particularly galling are the programs that, as part of their install, decide that they are so cool they want to be everywhere to make sure you don't miss out on the coolest most amazing program ever written in the history of mankind, so they go into the Start menu, into the Fast items, onto the desktop, into the Quick Launch, onto your Favorites, take over as your default autoplay handler, and even hang out as an icon next to the clock on the taskbar just in case you somehow missed all those other places - and each time you run them, they go and recreate those icons and settings in case you "accidentally lost them".

I hate those programs.


Looks like it is possible with the Microsoft-Windows-Shell-Setup on Win7 and above: TaskbarLinks

You are indeed correct about Verbs being language specific, here is the documentation stating this Pin Items to the Start Menu or Windows 7 Taskbar via Script:

The verbs for each action would have to be changed in the script for use with another language.

For automated deployments, some of these items can also be configured through an answer file on Windows Vista and higher. Windows 7 provides an unattend.xml setting to configure up to three Taskbar pinned items (see TaskbarLinks in Microsoft-Windows-Shell-Setup in the Automated Installation Kit documentation). And both Windows Vista and Windows 7 provide an unattend.xml setting to configure up to five “recently opened programs” on the Start Menu (StartPanelLinks in Microsoft-Windows-Shell-Setup). However, neither provide a way in unattend.xml to pin items to the Start Menu.

Upvotes: 2

John Rah
John Rah

Reputation: 1937

In the code example you linked to you could look in the verb name for &k which is the keyboard shortcut for pinning to the taskbar. This doesn't change for different languages (I believe).

So change this in the code example:

If (verb.Name = "Pin to Tas&kbar") _

to this:

If (InStr(verb.name,"&k")>0) _

or something along those lines.

Upvotes: 0

Related Questions