Link to home
Start Free TrialLog in
Avatar of dapperry
dapperry

asked on

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
Avatar of manojamin
manojamin

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

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
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
Avatar of dapperry

ASKER

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
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
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
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
ASKER CERTIFIED SOLUTION
Avatar of moterk
moterk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
'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....
Where can I find scrrun.dll.  Its not on my system, and a look on MS web site didn't prove too fruitful.

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