Results 1 to 5 of 5

Thread: Desktop Shortcut

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2001
    Location
    Philippines
    Posts
    136

    Question Desktop Shortcut

    Hello there,

    I want to create a shortcut icon on the desktop of my application. how can i do this automatically using vb.


    thanks in advance.
    elbimbo

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. 'http://www.vbweb.co.uk/show.asp?id=245
    2.  
    3. 'Creating a shortcut In VB Is far more complicated than it should be.
    4. 'The easiest way To create a shortcut Is To Add it To the recent documents list,
    5. 'And Then move it To where you want it To go.
    6.  
    7. 'First, the code below finds out where the Recent Documents And the Desktop folder
    8. 'Is, using the fGetSpecialFolder Function In the module code. This Is so we can copy
    9. 'the file from the Recent Documents To the Desktop. If you want To Put it somewhere Else,
    10. 'either Set sDesktopPath To an actual path, 'Or use fGetSpecialFolder using the CSIDL_*
    11. 'constants.
    12.  
    13. 'Then, the code calls the SHAddToRecentDocs API passing the Text In txtFilePath, And
    14. 'pauses For 1 1/2 seconds To ensure that it has been created. If successful, it Then
    15. 'works out the Name of the file the API has created, And Then moves it To the desktop.
    16. 'Finally, it renames the file To the caption 'specified In txtName.... And that's it!
    17.  
    18. 'Module Code
    19. Public Const CSIDL_DESKTOP = &H0 '// The Desktop - virtual folder
    20. Public Const CSIDL_PROGRAMS = 2 '// Program Files
    21. Public Const CSIDL_CONTROLS = 3 '// Control Panel - virtual folder
    22. Public Const CSIDL_PRINTERS = 4 '// Printers - virtual folder
    23. Public Const CSIDL_DOCUMENTS = 5 '// My Documents
    24. Public Const CSIDL_FAVORITES = 6 '// Favourites
    25. Public Const CSIDL_STARTUP = 7 '// Startup Folder
    26. Public Const CSIDL_RECENT = 8 '// Recent Documents
    27. Public Const CSIDL_SENDTO = 9 '// Send To Folder
    28. Public Const CSIDL_BITBUCKET = 10 '// Recycle Bin - virtual folder
    29. Public Const CSIDL_STARTMENU = 11 '// Start Menu
    30. Public Const CSIDL_DESKTOPFOLDER = 16 '// Desktop folder
    31. Public Const CSIDL_DRIVES = 17 '// My Computer - virtual folder
    32. Public Const CSIDL_NETWORK = 18 '// Network Neighbourhood - virtual folder
    33. Public Const CSIDL_NETHOOD = 19 '// NetHood Folder
    34. Public Const CSIDL_FONTS = 20 '// Fonts folder
    35. Public Const CSIDL_SHELLNEW = 21 '// ShellNew folder
    36. Public Const FO_MOVE = &H1
    37. Public Const FO_RENAME = &H4
    38. Public Const FOF_SILENT = &H4
    39. Public Const FOF_NOCONFIRMATION = &H10
    40. Public Const FOF_RENAMEONCOLLISION = &H8
    41. Public Const MAX_PATH As Integer = 260
    42. Public Const SHARD_PATH = &H2&
    43. Public Const SHCNF_IDLIST = &H0
    44. Public Const SHCNE_ALLEVENTS = &H7FFFFFFF
    45.  
    46. Public Type SHFILEOPSTRUCT
    47.     hwnd   As Long
    48.     wFunc As Long
    49.     pFrom As String
    50.     pTo     As String
    51.     fFlags  As Integer
    52.     fAborted       As Boolean
    53.     hNameMaps As Long
    54.     sProgress      As String
    55. End Type
    56.  
    57. Public Type ****EMID
    58.     cb As Long
    59.     abID As Byte
    60. End Type
    61.  
    62. Public Type ITEMIDLIST
    63.     mkid As ****EMID
    64. End Type
    65.  
    66. Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    67. Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" _
    68.         (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
    69. Declare Function SHGetSpecialFolderLocationD Lib "Shell32.dll" Alias _
    70.         "SHGetSpecialFolderLocation" (ByVal hwndOwner As Long, ByVal nFolder As Long, _
    71.         ByRef ppidl As Long) As Long
    72. Declare Function SHAddToRecentDocs Lib "Shell32.dll" (ByVal dwflags As Long, _
    73.         ByVal dwdata As String) As Long
    74. Declare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" _
    75.         (lpFileOp As SHFILEOPSTRUCT) As Long
    76. Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Long, _
    77.         ByVal uFlags As Long, ByVal dwItem1 As Long, ByVal dwItem2 As Long) As Long
    78. Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA" _
    79.         (ByVal pidl As Long, ByVal pszPath As String) As Long
    80.  
    81. Public Function fGetSpecialFolder(CSIDL As Long) As String
    82. Dim sPath As String
    83. Dim IDL As ITEMIDLIST
    84. '
    85. ' Retrieve info about system folders such as the "Recent Documents" folder.
    86. ' Info is stored in the IDL structure.
    87. '
    88. fGetSpecialFolder = ""
    89. If SHGetSpecialFolderLocation(Form1.hwnd, CSIDL, IDL) = 0 Then
    90.     '
    91.     ' Get the path from the ID list, and return the folder.
    92.     '
    93.     sPath = Space$(MAX_PATH)
    94.     If SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath) Then
    95.         fGetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\"
    96.     End If
    97. End If
    98. End Function
    99.  
    100.  
    101. 'Form Code
    102. Private Sub cmdCreate_Click()
    103. Dim i As Integer
    104. Dim lResult As Long
    105. Dim lpil As Long
    106. Dim sFilePath As String
    107. Dim sFileName As String
    108. Dim sRecentPath As String
    109. Dim sDesktopPath As String
    110. Dim sFilePathOld As String
    111. Dim sFilePathNew As String
    112. Dim sShortCutName As String
    113. Dim sMsg As String
    114. Dim SHFileOp As SHFILEOPSTRUCT
    115. '
    116. ' Add a shortcut to the Windows desktop.
    117. '
    118. ' Get the .exe path and display name associated with the
    119. ' button that was right clicked (determined by ptbrRightButton).
    120. '
    121. If Trim$(txtFilePath) = "" Then Exit Sub
    122. If Trim$(txtName) = "" Then Exit Sub
    123.  
    124. On Error GoTo cmdCreateError
    125. Screen.MousePointer = vbHourglass
    126. sFilePath = Trim$(txtFilePath)
    127. sShortCutName = Trim$(txtName) & ".lnk"
    128. '
    129. ' Get the paths of the folders to add the shortcuts to.
    130. ' The folders are the Recent Files List and the Desktop.
    131. '
    132. sRecentPath = fGetSpecialFolder(CSIDL_RECENT)
    133. '
    134. ' NOTE: to create the shortcut in another folder, set sDesktopPath
    135. ' to that folder.
    136. '
    137. sDesktopPath = fGetSpecialFolder(CSIDL_DESKTOPDIRECTORY)
    138. sMsg = "Error retrieving folder location."
    139. If sRecentPath <> "" And sDesktopPath <> "" Then
    140.     '
    141.     ' Create a shortcut in the Recent Files list.
    142.     '
    143.     sMsg = "Error adding shortcut to the Recent File list."
    144.     lResult = SHAddToRecentDocs(SHARD_PATH, sFilePath)
    145.     Sleep (1500)
    146.     If lResult Then
    147.         '
    148.         ' Extract the .exe name from the path.
    149.         '
    150.         i = 1
    151.         sFileName = sFilePath
    152.         Do While i
    153.             i = InStr(1, sFileName, "\")
    154.             If i Then sFileName = Mid$(sFileName, i + 1)
    155.         Loop
    156.         '
    157.         ' Move the shortcut from the Recent folder to the Desktop.
    158.         ' Since the shortcut now resides in the Recent folder,
    159.         ' modify the file name to include the Recent folder
    160.         ' path. Also, append ".lnk" to the original filename.
    161.         '
    162.         sFilePath = sRecentPath & sFileName & ".lnk" & vbNullChar & vbNullChar
    163.         With SHFileOp
    164.             .wFunc = FO_MOVE
    165.             .pFrom = sFilePath
    166.             .pTo = sDesktopPath
    167.             .fFlags = FOF_SILENT
    168.         End With
    169.  
    170.         sMsg = "Error creating desktop shortcut."
    171.         lResult = SHFileOperation(SHFileOp)
    172.         Sleep (1500)
    173.         If lResult = 0 Then
    174.             '
    175.             ' Rename the link.
    176.             '
    177.             sFilePathOld = sDesktopPath & sFileName & ".lnk" & vbNullChar & vbNullChar
    178.             sFilePathNew = sDesktopPath & sShortCutName & vbNullChar & vbNullChar
    179.             With SHFileOp
    180.                 .wFunc = FO_RENAME
    181.                 .pFrom = sFilePathOld
    182.                 .pTo = sFilePathNew
    183.                 .fFlags = FOF_SILENT Or FOF_RENAMEONCOLLISION
    184.             End With
    185.             sMsg = "Error renaming desktop shortcut."
    186.             lResult = SHFileOperation(SHFileOp) '123 = can't rename.
    187.             sMsg = ""
    188.             '
    189.             ' Refresh the desktop to display the shortcut.
    190.             '
    191.            Call SHGetSpecialFolderLocationD(Me.hwnd, CSIDL_DESKTOP, lpil)
    192.            Call SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_IDLIST, lpil, 0)
    193.         End If
    194.     End If
    195. End If
    196.  
    197. Screen.MousePointer = vbDefault
    198.     Exit Sub
    199.  
    200. cmdCreateError:
    201.     MsgBox "Error creating desktop shortcut. " & sMsg, vbExclamation, "Create Shortcut"
    202. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2001
    Location
    Philippines
    Posts
    136

    RESOLVED!

    Hello there,

    I have post this question coz i like to create a shortcut of my application during the installation. And my solution is using the Visual Studio Installer 1.1! If your interested to know how I can share it with you. Just e-mail [email protected]
    elbimbo

  4. #4
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Originally posted by peet
    VB Code:
    1. 'http://www.vbweb.co.uk/show.asp?id=245
    2.  
    3. 'Creating a shortcut In VB Is far more complicated than it should be.
    4. 'The easiest way To create a shortcut Is To Add it To the recent documents list,
    5. 'And Then move it To where you want it To go.
    6.  
    7. 'First, the code below finds out where the Recent Documents And the Desktop folder
    8. 'Is, using the fGetSpecialFolder Function In the module code. This Is so we can copy
    9. 'the file from the Recent Documents To the Desktop. If you want To Put it somewhere Else,
    10. 'either Set sDesktopPath To an actual path, 'Or use fGetSpecialFolder using the CSIDL_*
    11. 'constants.
    12.  
    13. 'Then, the code calls the SHAddToRecentDocs API passing the Text In txtFilePath, And
    14. 'pauses For 1 1/2 seconds To ensure that it has been created. If successful, it Then
    15. 'works out the Name of the file the API has created, And Then moves it To the desktop.
    16. 'Finally, it renames the file To the caption 'specified In txtName.... And that's it!
    17.  
    18. 'Module Code
    19. Public Const CSIDL_DESKTOP = &H0 '// The Desktop - virtual folder
    20. Public Const CSIDL_PROGRAMS = 2 '// Program Files
    21. Public Const CSIDL_CONTROLS = 3 '// Control Panel - virtual folder
    22. Public Const CSIDL_PRINTERS = 4 '// Printers - virtual folder
    23. Public Const CSIDL_DOCUMENTS = 5 '// My Documents
    24. Public Const CSIDL_FAVORITES = 6 '// Favourites
    25. Public Const CSIDL_STARTUP = 7 '// Startup Folder
    26. Public Const CSIDL_RECENT = 8 '// Recent Documents
    27. Public Const CSIDL_SENDTO = 9 '// Send To Folder
    28. Public Const CSIDL_BITBUCKET = 10 '// Recycle Bin - virtual folder
    29. Public Const CSIDL_STARTMENU = 11 '// Start Menu
    30. Public Const CSIDL_DESKTOPFOLDER = 16 '// Desktop folder
    31. Public Const CSIDL_DRIVES = 17 '// My Computer - virtual folder
    32. Public Const CSIDL_NETWORK = 18 '// Network Neighbourhood - virtual folder
    33. Public Const CSIDL_NETHOOD = 19 '// NetHood Folder
    34. Public Const CSIDL_FONTS = 20 '// Fonts folder
    35. Public Const CSIDL_SHELLNEW = 21 '// ShellNew folder
    36. Public Const FO_MOVE = &H1
    37. Public Const FO_RENAME = &H4
    38. Public Const FOF_SILENT = &H4
    39. Public Const FOF_NOCONFIRMATION = &H10
    40. Public Const FOF_RENAMEONCOLLISION = &H8
    41. Public Const MAX_PATH As Integer = 260
    42. Public Const SHARD_PATH = &H2&
    43. Public Const SHCNF_IDLIST = &H0
    44. Public Const SHCNE_ALLEVENTS = &H7FFFFFFF
    45.  
    46. Public Type SHFILEOPSTRUCT
    47.     hwnd   As Long
    48.     wFunc As Long
    49.     pFrom As String
    50.     pTo     As String
    51.     fFlags  As Integer
    52.     fAborted       As Boolean
    53.     hNameMaps As Long
    54.     sProgress      As String
    55. End Type
    56.  
    57. Public Type ****EMID
    58.     cb As Long
    59.     abID As Byte
    60. End Type
    61.  
    62. Public Type ITEMIDLIST
    63.     mkid As ****EMID
    64. End Type
    65.  
    66. Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    67. Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" _
    68.         (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
    69. Declare Function SHGetSpecialFolderLocationD Lib "Shell32.dll" Alias _
    70.         "SHGetSpecialFolderLocation" (ByVal hwndOwner As Long, ByVal nFolder As Long, _
    71.         ByRef ppidl As Long) As Long
    72. Declare Function SHAddToRecentDocs Lib "Shell32.dll" (ByVal dwflags As Long, _
    73.         ByVal dwdata As String) As Long
    74. Declare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" _
    75.         (lpFileOp As SHFILEOPSTRUCT) As Long
    76. Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Long, _
    77.         ByVal uFlags As Long, ByVal dwItem1 As Long, ByVal dwItem2 As Long) As Long
    78. Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA" _
    79.         (ByVal pidl As Long, ByVal pszPath As String) As Long
    80.  
    81. Public Function fGetSpecialFolder(CSIDL As Long) As String
    82. Dim sPath As String
    83. Dim IDL As ITEMIDLIST
    84. '
    85. ' Retrieve info about system folders such as the "Recent Documents" folder.
    86. ' Info is stored in the IDL structure.
    87. '
    88. fGetSpecialFolder = ""
    89. If SHGetSpecialFolderLocation(Form1.hwnd, CSIDL, IDL) = 0 Then
    90.     '
    91.     ' Get the path from the ID list, and return the folder.
    92.     '
    93.     sPath = Space$(MAX_PATH)
    94.     If SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath) Then
    95.         fGetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\"
    96.     End If
    97. End If
    98. End Function
    99.  
    100.  
    101. 'Form Code
    102. Private Sub cmdCreate_Click()
    103. Dim i As Integer
    104. Dim lResult As Long
    105. Dim lpil As Long
    106. Dim sFilePath As String
    107. Dim sFileName As String
    108. Dim sRecentPath As String
    109. Dim sDesktopPath As String
    110. Dim sFilePathOld As String
    111. Dim sFilePathNew As String
    112. Dim sShortCutName As String
    113. Dim sMsg As String
    114. Dim SHFileOp As SHFILEOPSTRUCT
    115. '
    116. ' Add a shortcut to the Windows desktop.
    117. '
    118. ' Get the .exe path and display name associated with the
    119. ' button that was right clicked (determined by ptbrRightButton).
    120. '
    121. If Trim$(txtFilePath) = "" Then Exit Sub
    122. If Trim$(txtName) = "" Then Exit Sub
    123.  
    124. On Error GoTo cmdCreateError
    125. Screen.MousePointer = vbHourglass
    126. sFilePath = Trim$(txtFilePath)
    127. sShortCutName = Trim$(txtName) & ".lnk"
    128. '
    129. ' Get the paths of the folders to add the shortcuts to.
    130. ' The folders are the Recent Files List and the Desktop.
    131. '
    132. sRecentPath = fGetSpecialFolder(CSIDL_RECENT)
    133. '
    134. ' NOTE: to create the shortcut in another folder, set sDesktopPath
    135. ' to that folder.
    136. '
    137. sDesktopPath = fGetSpecialFolder(CSIDL_DESKTOPDIRECTORY)
    138. sMsg = "Error retrieving folder location."
    139. If sRecentPath <> "" And sDesktopPath <> "" Then
    140.     '
    141.     ' Create a shortcut in the Recent Files list.
    142.     '
    143.     sMsg = "Error adding shortcut to the Recent File list."
    144.     lResult = SHAddToRecentDocs(SHARD_PATH, sFilePath)
    145.     Sleep (1500)
    146.     If lResult Then
    147.         '
    148.         ' Extract the .exe name from the path.
    149.         '
    150.         i = 1
    151.         sFileName = sFilePath
    152.         Do While i
    153.             i = InStr(1, sFileName, "\")
    154.             If i Then sFileName = Mid$(sFileName, i + 1)
    155.         Loop
    156.         '
    157.         ' Move the shortcut from the Recent folder to the Desktop.
    158.         ' Since the shortcut now resides in the Recent folder,
    159.         ' modify the file name to include the Recent folder
    160.         ' path. Also, append ".lnk" to the original filename.
    161.         '
    162.         sFilePath = sRecentPath & sFileName & ".lnk" & vbNullChar & vbNullChar
    163.         With SHFileOp
    164.             .wFunc = FO_MOVE
    165.             .pFrom = sFilePath
    166.             .pTo = sDesktopPath
    167.             .fFlags = FOF_SILENT
    168.         End With
    169.  
    170.         sMsg = "Error creating desktop shortcut."
    171.         lResult = SHFileOperation(SHFileOp)
    172.         Sleep (1500)
    173.         If lResult = 0 Then
    174.             '
    175.             ' Rename the link.
    176.             '
    177.             sFilePathOld = sDesktopPath & sFileName & ".lnk" & vbNullChar & vbNullChar
    178.             sFilePathNew = sDesktopPath & sShortCutName & vbNullChar & vbNullChar
    179.             With SHFileOp
    180.                 .wFunc = FO_RENAME
    181.                 .pFrom = sFilePathOld
    182.                 .pTo = sFilePathNew
    183.                 .fFlags = FOF_SILENT Or FOF_RENAMEONCOLLISION
    184.             End With
    185.             sMsg = "Error renaming desktop shortcut."
    186.             lResult = SHFileOperation(SHFileOp) '123 = can't rename.
    187.             sMsg = ""
    188.             '
    189.             ' Refresh the desktop to display the shortcut.
    190.             '
    191.            Call SHGetSpecialFolderLocationD(Me.hwnd, CSIDL_DESKTOP, lpil)
    192.            Call SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_IDLIST, lpil, 0)
    193.         End If
    194.     End If
    195. End If
    196.  
    197. Screen.MousePointer = vbDefault
    198.     Exit Sub
    199.  
    200. cmdCreateError:
    201.     MsgBox "Error creating desktop shortcut. " & sMsg, vbExclamation, "Create Shortcut"
    202. End Sub
    Using "WScript", its that much more shorter and easier

    http://www.experts-exchange.com/Prog..._20395968.html

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2001
    Location
    Philippines
    Posts
    136

    No Code needed

    Hi,

    those codes were too long. Using Visual Studio Installer you only need one line with less than 30 characters.
    elbimbo

Posting Permissions

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



Click Here to Expand Forum to Full Width