Pin to Start Menu in Vista
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Pin to Start Menu in Vista

  1. #1
    Join Date
    Jun 2004
    Location
    NH
    Posts
    674

    Pin to Start Menu in Vista

    With xp it's easy to pin items to the start menu, by simply adding a com reference to "Windows script host object model"
    Pin
    Code:
    CreateObject("Shell.Application").Namespace(Environment.SystemDirectory).ParseName(fileName_Ext).InvokeVerb("P&in to Start Menu")
    Unpin
    Code:
    CreateObject("Shell.Application").Namespace(Environment.SystemDirectory).ParseName(fileName_Ext).InvokeVerb("Unp&in from Start Menu")
    EDIT: This example looks in the system32 folder for the file name specified as fileName_Ext.

    However, this doesn't seem to work on Vista.
    Does anyone know if this is possible with Vista?
    Last edited by TT(n); August 3rd, 2008 at 08:35 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,960

    Re: Pin to Start Menu

    oops. wrong forum? that's vb.net, but should work with some modification.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,860

    Re: Pin to Start Menu

    [ split thread ]
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  4. #4
    Join Date
    Jun 2004
    Location
    NH
    Posts
    674

    Re: Pin to Start Menu in Vista

    I turned up the strictness of the project, and found "late binding" errors.
    I don't understand why though.
    These are all objects, and they are dimensioned as objects.

    How do you resolve the late binding issue with this example?

  5. #5
    Join Date
    Aug 2005
    Location
    Imperial College London, England
    Posts
    490

    Re: Pin to Start Menu in Vista

    Because a system.object has no properties or methods, so if you try and calls any, then you'll get a late binding error. So, although CreateObject returns an Object, it may not have a namespace property. That's what causes the late binding error.

    I can't see a solution immediately, apart from referencing shell32.dll from windows. But that would lead to cross system compatibility issues anyway!
    Help from me is always guaranteed!*
    VB.NET code is made up on the spot with VS2008 Professional with .NET 3.5. Everything else is just made up on the spot.
    Please Remember to rate posts, use code tags, send me money and all the other things listed in the "Before you post" posts.

    *Guarantee may not be honoured.

  6. #6
    Join Date
    Jun 2004
    Location
    NH
    Posts
    674

    Re: Pin to Start Menu in Vista

    That's what I've been thinking lately.
    I've tried it several different ways, and still nothing.
    I can't be sure that this is why it does'nt work on Vista, but for now it's the only reason I could come up with.

    Thanks alot for the confirmation.

  7. #7
    Join Date
    Jun 2004
    Location
    NH
    Posts
    674

    Re: Pin to Start Menu in Vista

    After some more searching, and some reworking I found a vb script works on Vista.

    So the a full solution to both xp and Vista would be something like this for .NET:

    Code:
    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim fName As String = Process.GetCurrentProcess.ProcessName
            Dim pinned As Boolean
            pinned = PinToStartMenu(fName, True)
            If pinned = True Then
                MessageBox.Show("Pinned to start menu")
            Else
                MessageBox.Show("Not pinned to start menu")
            End If
        End Sub
    
        Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            Dim fName As String = Process.GetCurrentProcess.ProcessName
            PinToStartMenu(fName, False)
        End Sub
    
        Private Function PinToStartMenu(ByVal fName As String, Optional ByVal pMenu As Boolean = True) As Boolean
            On Error Resume Next
            CreateShortCut(Environment.CurrentDirectory & "\" & fName & ".exe", Environment.CurrentDirectory, fName)
            If pMenu = True Then PinToStartMenu = DoVerb(fName, "P&in to Start Menu") 'P&in
            If pMenu = False Then PinToStartMenu = DoVerb(fName, "Unp&in from Start Menu") 'Unp&in
        End Function
    
        Private Function CreateShortCut(ByVal filePathOrigin As String, ByVal folderPathDestination As String, ByRef linkName As String) As Boolean
            On Error Resume Next
            Dim WshShell As Object
            Dim oShellLink As Object
            WshShell = CreateObject("WScript.Shell")
            oShellLink = WshShell.CreateShortCut(folderPathDestination & "\" & linkName & ".lnk")
            oShellLink.TargetPath = filePathOrigin
            oShellLink.WindowStyle = 1
            oShellLink.Hotkey = ""
            oShellLink.Description = ""
            oShellLink.WorkingDirectory = Environment.CurrentDirectory
            oShellLink.Save()
        End Function
    
        Private Function DoVerb(ByVal fName As String, ByVal sVerb As String) As Boolean
            Dim objShell As Object = CreateObject("Shell.Application")
            Dim objFolder As Object = objShell.Namespace(CurDir)
            If objFolder Is Nothing Then GoTo Release
            Dim objFolderItem As Object = objFolder.ParseName(fName & ".lnk")
            If objFolderItem Is Nothing Then GoTo Release
            Dim objVerb As Object
            Dim objVerbs As Object
            Dim osv As String
            osv = Environment.OSVersion.VersionString.Substring(21, 3)
            If osv = "5.0" OrElse osv = "5.1" Then
                If sVerb = "P&in to Start Menu" Then objFolderItem.InvokeVerb("P&in to Start Menu")
                If sVerb = "Unp&in from Start Menu" Then objFolderItem.InvokeVerb("Unp&in from Start Menu")
                DoVerb = True 'Did it, or at least tried to do it
            ElseIf osv.Substring(0, 1) = "6" OrElse osv.Substring(0, 1) = "7" Then
                objVerbs = objFolderItem.Verbs
                For Each objVerb In objVerbs
                    If objVerb.Name = sVerb Then objVerb.doit() : DoVerb = True
                Next
            End If
    Release:
            objShell = Nothing : objFolder = Nothing : objFolderItem = Nothing : objVerb = Nothing : objVerbs = Nothing
        End Function
    
    End Class

    You should not always pin executables directly to the start menu, because not all of them will load correctly for some unknown reason.
    The solution is to make a shortcut to the executable, and then pin that file(.lnk) to the start menu.
    You may want to hide the shortcuts somewhere though, in the case the user deletes the shortcut.
    Last edited by TT(n); December 9th, 2008 at 04:25 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center