Determining/Removing a Shortcut

I've got this program that runs in the system tray.  I want to have an option for the user to select whether or not they want the program to load during startup.  Through a KB article I found I figured out how to add Shortcut:

Private Declare Function fCreateShellLink Lib "vb5stkit.dll" (ByVal lpstrFolderName as String, ByVal lpstrLinkName as String, ByVal lpstrLinkPath as String, ByVal lpstrLinkArgs As String) As Long

Now I need to be able to determine whether the shortcut already exists (Don't want to make it twice), and how to remove the shortcut if the user so chooses.  Thanks for your help in advance.

:) dapperry
LVL 3
dapperryAsked:
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.

manojaminCommented:
use fRemoveShellLink function in the same dll...
0
manojaminCommented:
Private Declare Function fRemoveShellLink Lib "vb5stkit.dll" (ByVal lpstrFolderName As String, ByVal lpstrLinkName As String) As Long

0
moterkCommented:
use the Dir() method to check if it is there and the kill method to remove it.

The startup folder's path is stored in the registry at:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\

startup="startup path here"

if your link's caption is "myprog" then the file name for Dir and Kill to opperate on is:

StartupFolder\myprog.lnk
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!

manojaminCommented:
All the links are nothing but the linkname.lnk files.

If you want to check if the link exits,

(hoping that you would know the location, just do this as per this example

Private Declare Function fRemoveShellLink Lib "vb5stkit.dll" _
    (ByVal lpstrFolderName As String, _
    ByVal lpstrLinkName As String) As Long
Private Sub cmdRemove_Click()
    'this is an example of mirosoft access link
    'do not select YES on the msgbox, or it will delete the access link
    sFileName = Dir("C:\Program Files\Microsoft Office\Microsoft Access.lnk", vbNormal)
    If (sFileName <> "") Then
        iAns = MsgBox("Link Exists, Do want to remove it?", vbYesNo + vbDefaultButton2 + vbQuestion, App.Title)
        If (iAns = vbYes) Then
            Call fRemoveShellLink(linkfolder, linkname)
        End If
    End If
End Sub
0
dapperryAuthor Commented:
I don't know the path of the startup folder.  It differs upon machine, Win95 or NT, installation dir, etc.  Can you show me how to read that registry key that you mentioned, and then do a dir() from there.  Thanks to all thus far.

:) dapperry
0
WatsonRCommented:
Alternatively... This code wont create multiple copies, so no need to took and see if it's there.

Ross

(You'll need the "Windows Scripting Runtime" (from Microsoft web site) - SCRRUN.DLL)

Sub Main()
    Dim oShell    As New IWshRuntimeLibrary.IWshShell_Class
    Dim oShortcut As IWshRuntimeLibrary.IWshShortcut_Class

    With oShell
        Debug.Print .SpecialFolders.Item("Desktop") ' <-- shortcut created here
        Set oShortcut = .CreateShortcut(.SpecialFolders.Item("Desktop") & "\" & cPROJECT & ".lnk")
        With oShortcut
            .Description = cPROJECT
            .WorkingDirectory = "C:\" ' wherever your app lives
            .TargetPath = .WorkingDirectory & "Fred.exe"
            .IconLocation = .TargetPath & ",1"
            .Save
        End With
    End With
   
    Set oShortcut = Nothing
    Set oShell = Nothing

End Sub
0
moterkCommented:
To read where the startmenu is use:

Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpszValueName As String, ByVal lpdwReserved As Long, lpdwType As Long, lpData As Any, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

RegTopKey& = &H80000001 'HKey Current User
RegSection$="\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\"
ValueName$="StartUp"
RegValue$=""
RegResult& = RegOpenKeyEx(RegTopKey&, RegSection$, 0, &H1, RegHandle&)
If RegResult& = 0& Then
   RegResult& = RegQueryValueEx(RegHandle&, ValueName$, 0, 1, "", RegLenData&)
   If RegResult& = 234 Then
      RegValue$ = Space(RegLenData&)
      RegResult& = RegQueryValueEx(RegHandle&, ValueName$, 0, 1, ByVal RegValue$, RegLenData&)
   End If
   If RegResult& = 0& Then
      RegValue$ = Left$(RegValue$, RegLenData& - 1)
   End If
End if
RegResult& = RegCloseKey(RegHandle&)


Then RegValue$ contains the path of your StartUp folder
0
dapperryAuthor Commented:
moterk,
      RegValue$ comes back with a blank value.  I put the top declarations in a module, and the code at the bottom in a form event sub.  Am I doing something wrong?

WatsonR,
      I would rather be able to determine if its there, so that I can pre-check or pre-uncheck the check box in my options form.  So I'm going to give moterk an opprtunity to get his/her idea to wrok.  If it doesn't I'll try out your solution.


:) dapperry
0
moterkCommented:
At home I use:

Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003
Const HKEY_DYN_DATA = &H80000006
Const HKEY_CURRENT_CONFIG = &H80000005
Const HKEY_CLASSES_ROOT = &H80000000
Const ERROR_SUCCESS = 0&
Const ERROR_MORE_DATA = 234
Const REG_SZ = 1
Const KEY_QUERY_VALUE = &H1
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpszValueName As String, ByVal lpdwReserved As Long, lpdwType As Long, lpData As Any, lpcbData As Long) As Long
Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Function ReadRegStringValue(ByVal lTopKey as long, ByVal sSubKey As String, ByVal sValueName As String, ByVal sDefault As String) As String
    Dim lHandle     As Long
    Dim lLenData    As Long
    Dim lResult     As Long
    Dim sValue      As String
    sValue = sDefault
    lResult = RegOpenKeyEx(lTopKey, sSubKey, 0, KEY_QUERY_VALUE, lHandle)
        If lResult <> ERROR_SUCCESS Then
            'msgbox "Registry String!",,"ERROR!"
            Exit Function
        End If
        lResult = RegQueryValueEx(lHandle, sValueName, 0, REG_SZ, "", lLenData)
        If lResult = ERROR_MORE_DATA Then
            sValue = Space(lLenData)
            lResult = RegQueryValueEx(lHandle, sValueName, 0, REG_SZ, sValue, lLenData)
        End If
        If lResult = ERROR_SUCCESS Then  'Remove null character.
            sValue = Left$(sValue, lLenData - 1)
        Else
            'msgbox "Registry String!",,"ERROR!"
            Exit Function
        End If
        lResult = RegCloseKey(lHandle)
        ReadRegStringValue = sValue
End Function


You Would need to call

Subkey$="\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
SUPath$=ReadRegStringValue(HKEY_CURRENT_USER, SubKey$, "Startup", "oops")

If SUPath$="oops" the API did not find the key.  I can not remember the exact syntax of the subkey$
So if it is "oops" try:

Subkey$="Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\"
Subkey$="\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\"
Subkey$="Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"

I do not have VB5 at work to test it so give them all a shot.


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
dhwanilshahCommented:
'add a refrence to Windows Scripting Host
'add a refrence to Microsoft Scripting Runtime

Private Sub Form_Click()

Dim mFSO As New Scripting.FileSystemObject

Dim mShellObj As New IWshRuntimeLibrary.IWshShell_Class

Dim mCol As IWshCollection_Class

Dim mShortCut As IWshRuntimeLibrary.IWshShortcut_Class

Dim mStartMenuPath As String

Set mCol = mShellObj.SpecialFolders

'The following special folders are provided with the Windows 95 and Microsoft Windows NT 4.0 operating systems:
'
    'AllUsersDesktop
    'AllUsersStartMenu
    'AllUsersPrograms
    'AllUsersStartup
    'Desktop
    'Favorites
    'Fonts
    'MyDocuments
    'NetHood
    'PrintHood
    'Programs
    'Recent
    'SendTo
    'StartMenu
    'Startup
    'Templates
   

mStartMenuPath = mCol.Item("StartMenu")

MsgBox mStartMenuPath

If mFSO.FileExists(mStartMenuPath & "\myfile.lnk") = True Then
    'file found then maybe delete it
    If MsgBox("File Found... Do you want to remove it?", vbYesNo + vbQuestion, "File Found") = vbYes Then
        mFSO.DeleteFile mStartMenuPath & "\myfile.lnk"
    End If
Else
    'fine not found so create it
    If MsgBox("File Not Found... Do you want to create a shortcut?", vbYesNo + vbQuestion, "Shortcut Not Found") = vbYes Then
        Set mShortCut = mShellObj.CreateShortcut(mStartMenuPath & "\myfile.lnk")
        mShortCut.TargetPath = "c:\program files\myfolder\myfile.exe"
        mShortCut.Save
    End If
End If

' i am proposing this as an answer
' i can just add a comment to it now as someone has proposed an answer, if u can then please take this comment as an answer if it is suitable and post the points to me....
0
dapperryAuthor Commented:
Where can I find scrrun.dll.  Its not on my system, and a look on MS web site didn't prove too fruitful.

:) dapperry
0
WatsonRCommented:
Try...

http://msdn.microsoft.com/scripting/

then (on the left), Windows Script Host, Downloads and then (on the right) Download Microsoft Windows Script Host

or...

http://www.microsoft.com/msdownload/vbscript/scripting.asp
0
dapperryAuthor Commented:
Got Moterks answer to work.  I'm sure WatsonR's would have worked too, but this saves me one dll file.  I ended up using -

Subkey$="Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"

Thanks to all.

:) dapperry
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.