Results 1 to 18 of 18

Thread: Simplest way of creating a shortcut?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229

    Simplest way of creating a shortcut?

    What is the simplest way to create a shortcut file "*.lnk"?
    I want really simple code, no one page worthless code! I'll even use DLLs or Controls. Thank you for your help.

    Please do not direct me to other threads cuz I've already done a search of this forum.

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    VB Code:
    1. 'Module Code
    2. Public Enum CSIDL_FOLDERS
    3.     CSIDL_DESKTOP = &H0 '// The Desktop - virtual folder
    4.     CSIDL_PROGRAMS = 2 '// Program Files
    5.     CSIDL_CONTROLS = 3 '// Control Panel - virtual folder
    6.     CSIDL_PRINTERS = 4 '// Printers - virtual folder
    7.     CSIDL_DOCUMENTS = 5 '// My Documents
    8.     CSIDL_FAVORITES = 6 '// Favourites
    9.     CSIDL_STARTUP = 7 '// Startup Folder
    10.     CSIDL_RECENT = 8 '// Recent Documents
    11.     CSIDL_SENDTO = 9 '// Send To Folder
    12.     CSIDL_BITBUCKET = 10 '// Recycle Bin - virtual folder
    13.     CSIDL_STARTMENU = 11 '// Start Menu
    14.     CSIDL_DESKTOPFOLDER = 16 '// Desktop folder
    15.     CSIDL_DRIVES = 17 '// My Computer - virtual folder
    16.     CSIDL_NETWORK = 18 '// Network Neighbourhood - virtual folder
    17.     CSIDL_NETHOOD = 19 '// NetHood Folder
    18.     CSIDL_FONTS = 20 '// Fonts folder
    19.     CSIDL_SHELLNEW = 21 '// ShellNew folder
    20. End Enum
    21. Private Const FO_MOVE = &H1
    22. Private Const FO_RENAME = &H4
    23. Private Const FOF_SILENT = &H4
    24. Private Const FOF_NOCONFIRMATION = &H10
    25. Private Const FOF_RENAMEONCOLLISION = &H8
    26. Private Const MAX_PATH As Integer = 260
    27. Private Const SHARD_PATH = &H2&
    28. Private Const SHCNF_IDLIST = &H0
    29. Private Const SHCNE_ALLEVENTS = &H7FFFFFFF
    30.  
    31. Private Type SHFILEOPSTRUCT
    32.     hwnd   As Long
    33.     wFunc As Long
    34.     pFrom As String
    35.     pTo     As String
    36.     fFlags  As Integer
    37.     fAborted       As Boolean
    38.     hNameMaps As Long
    39.     sProgress      As String
    40. End Type
    41.  
    42. Private Type ****EMID
    43.     cb As Long
    44.     abID As Byte
    45. End Type
    46.  
    47. Private Type ITEMIDLIST
    48.     mkid As ****EMID
    49. End Type
    50.  
    51. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    52. Private Declare Function SHGetSpecialFolderLocation Lib "Shell32.dll" _
    53.         (ByVal hwndOwner As Long, ByVal nFolder As Long, pidl As ITEMIDLIST) As Long
    54. Private Declare Function SHGetSpecialFolderLocationD Lib "Shell32.dll" Alias _
    55.         "SHGetSpecialFolderLocation" (ByVal hwndOwner As Long, ByVal nFolder As Long, _
    56.         ByRef ppidl As Long) As Long
    57. Private Declare Function SHAddToRecentDocs Lib "Shell32.dll" (ByVal dwflags As Long, _
    58.         ByVal dwdata As String) As Long
    59. Private Declare Function SHFileOperation Lib "Shell32.dll" Alias "SHFileOperationA" _
    60.         (lpFileOp As SHFILEOPSTRUCT) As Long
    61. Private Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Long, _
    62.         ByVal uFlags As Long, ByVal dwItem1 As Long, ByVal dwItem2 As Long) As Long
    63. Private Declare Function SHGetPathFromIDList Lib "Shell32.dll" Alias "SHGetPathFromIDListA" _
    64.         (ByVal pidl As Long, ByVal pszPath As String) As Long
    65.  
    66.  
    67. Private Function fGetSpecialFolder(CSIDL As Long) As String
    68. Dim sPath As String
    69. Dim IDL As ITEMIDLIST
    70. '
    71. ' Retrieve info about system folders such as the "Recent Documents" folder.
    72. ' Info is stored in the IDL structure.
    73. '
    74. fGetSpecialFolder = ""
    75. If SHGetSpecialFolderLocation(Form1.hwnd, CSIDL, IDL) = 0 Then
    76.     '
    77.     ' Get the path from the ID list, and return the folder.
    78.     '
    79.     sPath = Space$(MAX_PATH)
    80.     If SHGetPathFromIDList(ByVal IDL.mkid.cb, ByVal sPath) Then
    81.         fGetSpecialFolder = Left$(sPath, InStr(sPath, vbNullChar) - 1) & "\"
    82.     End If
    83. End If
    84. End Function
    85.  
    86. 'Thanks to Mike J for pointing out some errors
    87. 'and improving this routine
    88.  
    89. 'Module Code
    90. Public Function CreateShortcut(ByRef txtFilePath As String, _
    91.         ByRef txtName As String, ByRef vTarget As CSIDL_FOLDERS)
    92.     Dim I As Integer
    93.     Dim lResult As Long
    94.     Dim lpil As Long
    95.     Dim sFilePath As String
    96.     Dim sFileName As String
    97.     Dim sRecentPath As String
    98.     Dim sDesktopPath As String
    99.     Dim sFilePathOld As String
    100.     Dim sFilePathNew As String
    101.     Dim sShortCutName As String
    102.     Dim SMsg As String
    103.     Dim SHFileOp As SHFILEOPSTRUCT
    104.  
    105.     ' Add a shortcut to any path virtual folder.
    106.     '
    107.     ' Get the .exe path and display name associated with the
    108.     ' button that was right clicked (determined by ptbrRightButton).
    109.     '
    110.     On Error GoTo cmdCreateError
    111.     Screen.MousePointer = vbHourglass
    112.     sFilePath = Trim$(txtFilePath)
    113.     sShortCutName = Trim$(txtName) & ".lnk"
    114.     '
    115.     ' Get the paths of the folders to add the shortcuts to.
    116.     ' The folders are the Recent Files List and the Desktop.
    117.     '
    118.     sRecentPath = fGetSpecialFolder(CSIDL_RECENT)
    119.     '
    120.     ' NOTE: to create the shortcut in another folder, set sDesktopPath
    121.     ' to that folder.
    122.     sDesktopPath = fGetSpecialFolder(VTarget)
    123.     SMsg = "Error retrieving folder location."
    124.     If sRecentPath <> "" And sDesktopPath <> "" Then
    125.         '
    126.         ' Create a shortcut in the Recent Files list.
    127.         '
    128.         sMsg = "Error adding shortcut to the Recent File list."
    129.         lResult = SHAddToRecentDocs(SHARD_PATH, sFilePath)
    130.         Call Sleep(1500)
    131.         If lResult Then
    132.    
    133.             ' Extract the .exe name from the path.
    134.             I = 1
    135.             sFileName = sFilePath
    136.             Do While I
    137.                 I = InStr(1, sFileName, "\")
    138.                 If I Then sFileName = Mid$(sFileName, I + 1)
    139.             Loop
    140.            
    141.            
    142.             ' Move the shortcut from the Recent folder to the Desktop.
    143.             ' Since the shortcut now resides in the Recent folder,
    144.             ' modify the file name to include the Recent folder
    145.             ' path. Also, append ".lnk" to the original filename.
    146.             '
    147.             sFilePath = sRecentPath & "\" & sFileName & ".lnk" & _                vbNullChar & vbNullChar
    148.            
    149.             With SHFileOp
    150.                 .wFunc = FO_MOVE
    151.                 .pFrom = sFilePath
    152.                 .pTo = sDesktopPath
    153.                 .fFlags = FOF_SILENT
    154.             End With
    155.            
    156.             SMsg = "Error creating desktop shortcut."
    157.             lResult = SHFileOperation(SHFileOp)
    158.             Sleep (1500)
    159.             If lResult = 0 Then
    160.            
    161.                 '
    162.                 ' Rename the link.
    163.                
    164.                 sFilePathOld = sDesktopPath & "\" & sFileName & ".lnk" & _
    165.                      vbNullChar & vbNullChar
    166.                 sFilePathNew = sDesktopPath & "\" & sShortCutName & _
    167.                      vbNullChar & vbNullChar
    168.                 With SHFileOp
    169.                   .wFunc = FO_RENAME
    170.                   .pFrom = sFilePathOld
    171.                   .pTo = sFilePathNew
    172.                   .fFlags = FOF_SILENT Or FOF_RENAMEONCOLLISION
    173.                 End With
    174.                 SMsg = "Error renaming desktop shortcut."
    175.                 lResult = SHFileOperation(SHFileOp) '123 = can't rename.
    176.                 SMsg = ""
    177.                 '
    178.                 ' Refresh the desktop to display the shortcut.
    179.                 '
    180.                 Call SHGetSpecialFolderLocationD(0, CSIDL_DESKTOP, lpil)
    181.                
    182.                 Call SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_IDLIST, lpil, 0)
    183.                
    184.             End If
    185.         End If
    186.     End If
    187.     Screen.MousePointer = vbDefault
    188.     Exit Function
    189.    
    190. cmdCreateError:
    191.     MsgBox "Error creating desktop shortcut. " & SMsg, vbExclamation, "Create Shortcut"
    192. End Function

    Your installer should provide this functionality, right?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3
    Member
    Join Date
    Nov 2002
    Location
    Barberton Ohio
    Posts
    42
    Yep, that's simple. Short and sweet!
    What is the KeyCode for the 'ANY' Key? - Bill Gates

  4. #4
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849

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

  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    KISS - so now I'm stupid?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  6. #6
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    hahaha, it's censored out SHITEMID
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  7. #7
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849

    Re: Well

    Originally posted by James Stanich
    KISS - so now I'm stupid?
    I've been saying this for ages to the kids at home and folks 'round me.

    KISS does not stand for "Keep It Simple Stupid". Its stands for "Keep It Short , Sweety"

    So U are not Stupid, just Sweet

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

  8. #8
    Fanatic Member seec77's Avatar
    Join Date
    Jan 2003
    Posts
    596
    james, how can i use your code?
    Best Regards,
    seec77

    If you helped me, cosinder yourself thanked.

    Get each and every Garfield strip here!
    Here you can get all Calvin & Hobes strips!
    Damn UComics! It was probably unprofitable for them to allow us to just download Garfield and Calving & Hobes strips... so they made folder indexing unallowed on their server!!!

    I am 33% addicted to Counterstrike. What about you?
    I am 23% addicted to Star Wars. What about you?
    I am 0% addicted to Tupac. What about you?

  9. #9
    Addicted Member
    Join Date
    Dec 2002
    Location
    Malaysia
    Posts
    224
    James, mind showing how to utilize your code ??

    thanks

    leng wai

  10. #10
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Here is an example :

    VB Code:
    1. CreateShortcut "C:\test.txt", "your name for the file", CSIDL_DOCUMENTS

    Just replace the "C:\test.txt" with the path of the file, "your name for the file" speaks for itself and select one of the options for CSIDL_DOCUMENTS


    Has someone helped you? Then you can Rate their helpful post.

  11. #11
    Addicted Member
    Join Date
    Dec 2002
    Location
    Malaysia
    Posts
    224
    hi manavo,

    Thanks the code works fine.

    but whenever the object is deleted the source file will also be removed. If its a shortcut file, that is not suppose to happen.

  12. #12
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    "the object is deleted"

    Do you mean the shortcut that is created?


    Has someone helped you? Then you can Rate their helpful post.

  13. #13
    Addicted Member
    Join Date
    Dec 2002
    Location
    Malaysia
    Posts
    224
    yes ... right after the so called shortcut is created i delete it. Later found my source file missing too.

    I found out one more thing. I checked the file description of the shortcut created, I don't think it is defined as shortcut type. It looks like a normal file .

    Please help.

    thanks
    leng wai

  14. #14
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    That didn't happen to me! I created the shortcut, deleted it and I still have the original file. It also did look like a shortcut file.


    Has someone helped you? Then you can Rate their helpful post.

  15. #15
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Here is another method. Maybe it is better


    Has someone helped you? Then you can Rate their helpful post.

  16. #16
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Or maybe this :

    VB Code:
    1. 'Source: MSDN column 'Ask Dr. GUI'
    2. 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
    3. Private Sub Form_Load()
    4.     Dim strGroupName As String, strLinkName As String
    5.     Dim strLinkPath As String, strLinkArguments As String
    6.     Dim fPrivate As Boolean, sParent As String
    7.     Dim fSuccess As Boolean
    8.     strLinkName = "Shortcut to Calculator"
    9.     strLinkPath = "c:\Windows\calc.exe"
    10.     strLinkArguments = ""
    11.     fPrivate = True                              ' Add shortcut to desktop.
    12.     strGroupName = "..\..\Desktop"
    13.     sParent = "$(Programs)"
    14.     fSuccess = fCreateShellLink(strGroupName & vbNullChar, strLinkName, strLinkPath, strLinkArguments & vbNullChar, fPrivate, sParent)
    15.     'the path should never be enclosed in double quotes
    16.     If fSuccess Then
    17.         MsgBox "Created desktop shortcut"
    18.     Else
    19.         MsgBox "Unable to create desktop shortcut"
    20.     End If
    21.     ' Add shortcut to Programs menu.
    22.     strGroupName = "$(Programs)"
    23.     sParent = "$(Programs)"
    24.     fSuccess = fCreateShellLink(strGroupName & vbNullChar, strLinkName, strLinkPath, strLinkArguments & vbNullChar, fPrivate, sParent)
    25.     'the path should never be enclosed in double quotes
    26.     If fSuccess Then
    27.         MsgBox "Created shortcut on Programs menu"
    28.     Else
    29.         MsgBox "Unable to create shortcut on Programs menu"
    30.     End If
    31.     ' Add shortcut to Startup folder of Programs menu.
    32.     strGroupName = "Startup"
    33.     sParent = "$(Programs)"
    34.     fSuccess = fCreateShellLink(strGroupName & vbNullChar, strLinkName, strLinkPath, strLinkArguments & vbNullChar, fPrivate, sParent)
    35.     'the path should never be enclosed in double quotes
    36.     If fSuccess Then
    37.         MsgBox "Created shortcut in Startup folder"
    38.     Else
    39.         MsgBox "Unable to create shortcut in Startup folder"
    40.     End If
    41. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  17. #17
    Addicted Member
    Join Date
    Dec 2002
    Location
    Malaysia
    Posts
    224
    ermm ... what could be the problem then ???

    Anyway ... I'd tried your new method, it great. It's now working correctly. Thank you.

    But i find controlling the destination path a little bit too tidious. The first method is rather cool. Anyway .. I'll keep trying .

    Thanks a lot manavo.

    Cheers,
    leng wai

  18. #18
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    I'm glad the other ways worked for you. Unfortunatelly, I can't understand what causes the problem. Sorry...


    Has someone helped you? Then you can Rate their helpful post.

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