Using vb6 how to add a shortcut of an exe file to the desktop

I have windows 7,  using vb6,   how to add a shortcut of an executable file, to the desktop of windows 7?
saljasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SwordsowerCommented:
Okay, so this is what I've found that should be able to do what you need and more.
'System & API - How to Add a shortcut in current users Desktop, Programs menu and Startup folder
Private Declare Function fCreateShellLink Lib "vb6stkit.dll" (ByVal lpstrFolderName As String, _
    ByVal lpstrLinkName As String, ByVal lpstrLinkPath As String, ByVal lpstrLinkArguments As String, _
    ByVal fPrivate As Long, ByVal sParent As String) As Long

Private Sub Form_Load()
    Dim strGroupName As String, strLinkName As String
    Dim strLinkPath As String, strLinkArguments As String, sParent As String
    Dim fPrivate As Boolean, fSuccess As Boolean
    strLinkName = "Shortcut to Calculator"
    strLinkPath = "c:\Windows\calc.exe"
    strLinkArguments = ""
    fPrivate = True
    'Add shortcut to desktop.
    strGroupName = "..\..\Desktop"
    sParent = "$(Programs)"
    fSuccess = fCreateShellLink(strGroupName & vbNullChar, strLinkName, strLinkPath, strLinkArguments & _
        vbNullChar, fPrivate, sParent)
    'the path should never be enclosed in double quotes
    If fSuccess Then
        MsgBox "Created desktop shortcut"
    Else
        MsgBox "Unable to create desktop shortcut"
    End If
   'Add shortcut to Programs menu.
    strGroupName = "$(Programs)"
    sParent = "$(Programs)"
    fSuccess = fCreateShellLink(strGroupName & vbNullChar, strLinkName, strLinkPath, strLinkArguments & _
        vbNullChar, fPrivate, sParent)
    'the path should never be enclosed in double quotes
    If fSuccess Then
        MsgBox "Created shortcut on Programs menu"
    Else
        MsgBox "Unable to create shortcut on Programs menu"
    End If
    ' Add shortcut to Startup folder of Programs menu.
    strGroupName = "Startup"
    sParent = "$(Programs)"
    fSuccess = fCreateShellLink(strGroupName & vbNullChar, strLinkName, strLinkPath, strLinkArguments & _
        vbNullChar, fPrivate, sParent)
    'the path should never be enclosed in double quotes
    If fSuccess Then
        MsgBox "Created shortcut in Startup folder"
    Else
        MsgBox "Unable to create shortcut in Startup folder"
    End If
End Sub
 
Of course, this is just an example.  The meat and potatoes of this code is in bold.  This code will simply create a shortcut in three places, Desktop, Programs Menu, and Startup Folder.  My advice: Play with the code until you get it to do exactly what you want.  Should take no more than half an hour for even a beginner.
Any questions, email me (Xefanro@yahoo.com).
0
saljasAuthor Commented:
many thanks Swordsower
Your code did not work.    We are working on windows 7 64bit.   It adds to program menu, it adds to the startup, but it does not add to the desktop.  I think your code will work on 32 bit only.
thanks any way.
0
saljasAuthor Commented:
Mr Swordsower
I have just tried it on win7 32bit, it also did not work.  So it seems the problam is only with win 7.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

SwordsowerCommented:
Got it, it's the path the code is using for the desktop.  It currently says strGroupName = "..\..\Desktop".  The ...\...\ doesn't mean anything, it's just a placeholder.  It should read something like this "C:\Documents and Settings\User Name\Desktop".
But, instead of doing that, let's make the code more robust, shall we?  Make it say this:
strGroupName = getPathToDesktop
Now, find someplace convenient, and add this function.
Public Function getPathToDesktop() As String
    Dim WSHShell As Object
    Set WSHShell = CreateObject("WScript.Shell")
    If (Not WSHShell Is Nothing) Then
        getPathToDesktop = WSHShell.SpecialFolders("Desktop")
    End If
    Set WSHShell = Nothing
End Function

Trust me, this is hundreds of times better, because now it finds the desktop independent of the specific user's username.  If you had specified a Username in the string, your code would only work for that user.  Doing it this way lets any user run the code and it will 'find' the desktop regardless.  I know this function works in XP, but I see no reason why it wouldn't work in 7.
-------Unrelated Notes Below This Line-------
One other caveat: This has nothing to do with your 64 bit version.  Do not assume that the 64 bit system runs the code any different - VB* was designed by microsoft, it is VERY rare that a microsoft OS is going to have a problem with the bit level.  Be mindful of the fact that the path to (for example) your 'my music' folder in XP and earlier looks like this "C:\Documents and Settings\User Name\My Documents\My Music\" and in Vista and XP it looks like this "C:\Documents and Settings\User Name\Music".  Invalid path is more common than OS conflicts (at least in VB*).
*I use VB6 and VB.NET interchangeably in this instance, as they are both Microsoft, and therefore marry up with what I am saying in the paragraph.
0
saljasAuthor Commented:
By replacing
strGroupName = "..\..\Desktop"   with  strGroupName = getPathToDesktop
and adding the Public Function it can get the desktop of the current user FINE.  In fact it is helfull for future use.
But
it did NOT add to desktop in all windows   xp  2000   7  (fSuccess = False)
and by returning back to   strGroupName = "..\..\Desktop"
xp   2000    work fine  (fSuccess = True)
BUT with    win 7   still did not work  (fSuccess = False)
0
LeithauserCommented:
Use this function to create an icon on the desktop. AppName$ is the name to display under the icon,FinalPath$ is the folder name ending in \, and ExeName$ is the exe file name.

Function CreateIcon(AppName$, FinalPath$, ExeName$) As Integer

'AppName$ is name to display on icon
'FinalPath$ is path of program folder
' ExeName$ is file name
On Error Resume Next

Dim wshShell As Object
Set wshShell = CreateObject("WScript.Shell")
Dim lnkDesktop As Object
Set lnkDesktop = wshShell.CreateShortcut(wshShell.SpecialFolders("Desktop") & "\" & AppName$ & ".lnk")
With lnkDesktop
    .Description = AppName$ ' "My Application"
    '.TargetPath = FinalPath$ & "\" & ExeName$ ' "C:\Program Files\My Application\MyApp.exe"
    .TargetPath = FinalPath$ & ExeName$ ' Note: Removed \ to conform to format used in other subs
    .WorkingDirectory = FinalPath$ ' "C:\Program Files\My Application"
    .Save
End With
Set lnkDesktop = Nothing
Set wshShell = Nothing


'    Dim wshShell As Object
'    Set wshShell = CreateObject("WScript.Shell")
'    Dim lnkDesktop As Object
'    Set lnkDesktop = wshShell.CreateShortcut(wshShell.SpecialFolders("Desktop") & "\My Application.lnk")
'    With lnkDesktop
'         .Description = "My Application"
'         .TargetPath = "C:\Program Files\My Application\MyApp.exe"
'         .WorkingDirectory = "C:\Program Files\My Application"
'         .Save
'    End With
'       Set lnkDesktop = Nothing
'       Set wshShell = Nothing

If Err Then
    Err = 0
    CreateIcon = False
Else
    CreateIcon = True
End If

End Function

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
saljasAuthor Commented:
Many thanks for Swordsower for providing with a very good way of getting the desktop.
Many thanks for  Leithauser for the function that is able to add an icon to all windows platforms.
0
saljasAuthor Commented:
To  Leithauser
Yous function works fine.  Many thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.