Option Explicit
Private Declare Function OSfCreateShellLink Lib "vb6stkit.dll" Alias "fCreateShellLink" _
(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 Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
(ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
Private Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, ByVal pszPath As String) As Long
Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
Private Const ERROR_SUCCESS = 0&
Private Const CSIDL_DESKTOPDIRECTORY = &H10 '{user}\Desktop
Private Sub Form_Load()
'============================
Dim strLinkName As String
Dim strFullPath As String
Dim strComLineArgs As String
Dim bPrivate As Boolean
Dim strParent As String
strLinkName = App.EXEName & ".exe"
strFullPath = App.Path & "\" & App.EXEName & ".exe"
strComLineArgs = ""
bPrivate = True
strParent = "$(Programs)"
CreateShortcut strLinkName, strFullPath, strComLineArgs, bPrivate, strParent
End Sub
Private Sub CreateShortcut(strLinkName As String, _
strFullPath As String, _
strComLineArgs As String, _
bPrivate As Boolean, _
strParent As String)
'======================================================
Dim sDeskPath As String
Dim Pos As Integer
Dim strDestination As String
On Error Resume Next
sDeskPath = GetSpecialFolder(Me.hwnd, CSIDL_DESKTOPDIRECTORY)
Pos = InStrRev(sDeskPath, "\")
strDestination = "..\.." & Mid(sDeskPath, Pos)
OSfCreateShellLink strDestination, strLinkName, strFullPath, _
strComLineArgs, bPrivate, strParent
End Sub
Public Function GetSpecialFolder(hwnd As Long, CSIDL As Long) As String
'========================================================================
Dim pidl As Long
Dim Pos As Long
Dim sPath As String
'fill the pidl with the specified folder item
If SHGetSpecialFolderLocation(hwnd, CSIDL, pidl) = ERROR_SUCCESS Then
'initialize & get the path
sPath = Space$(260)
If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then
'check for a null
Pos = InStr(sPath, Chr$(0))
If Pos Then 'strip it
GetSpecialFolder = Left$(sPath, Pos - 1)
End If
Call CoTaskMemFree(pidl)
End If
End If
End Function