Results 1 to 3 of 3

Thread: creating shortcuts

  1. #1

    Thread Starter
    Addicted Member TheSarlacc's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere close
    Posts
    185

    creating shortcuts

    how would i get my vb app to create a short cut to a file, like an .exe or something? thanks in advance TheSarlacc
    Get Quoted:

    "If Shane Warne had given his mobile phone to Russell Crowe, neither of them would be in trouble."
    "She has more troubles than a centipede with its legs crossed."
    "For every action, there is an equal and opposite Government program."


  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    I have this sample created by Skrol29 (whoever that is )

    seems mucky, but I have never seen other ways to do it...



    VB Code:
    1. 'Author: Skrol 29
    2. 'Category: Files And Directories
    3. 'Type: Snippets
    4. 'Difficulty: Advanced
    5.  
    6.  
    7. 'Version Compatibility:  Visual Basic 5   Visual Basic 6
    8.  
    9.  
    10. 'More information: The usual code To create shortcuts does Not allow one
    11. 'To create a shortcut For a program With parameters. This code enables you
    12. 'To create a shortcut anywhere For any Command Line With any parameters,
    13. 'the icons you want, the default folder you want And the window mode you want.
    14. 'Enjoy.
    15.  
    16. Option Explicit
    17.  
    18. '---------------------------
    19. 'Skrol 29
    20. 'http://www.rezo.net/dir/skrol29/
    21. '---------------------------
    22. 'Version 1.00, on 02/13/1999
    23. 'Version 1.01, on 04/19/1999
    24. '---------------------------
    25. Private Const CSIDL_DESKTOP = &H0
    26. Private Const CSIDL_PROGRAMS = &H2
    27. Private Const CSIDL_PERSONAL = &H5
    28. Private Const CSIDL_FAVORITES = &H6
    29. Private Const CSIDL_STARTUP = &H7
    30. Private Const CSIDL_RECENT = &H8
    31. Private Const CSIDL_STARTMENU = &HB
    32. Private Const CSIDL_COMMON_STARTMENU = &H16
    33. Private Const CSIDL_COMMON_PROGRAMS = &H17
    34. Private Const CSIDL_COMMON_STARTUP = &H18
    35. Private Const CSIDL_COMMON_FAVORITES = &H1F
    36.  
    37. Private Declare Function api_SHAddToRecentDocs Lib _
    38.    "shell32.dll" Alias "SHAddToRecentDocs" (ByVal dwFlags As _
    39.    Long, ByVal dwData As String) As Long
    40.  
    41. Private Declare Function api_SHGetSpecialFolderLocation Lib _
    42.   "shell32.dll" Alias "SHGetSpecialFolderLocation" (ByVal _
    43.   hwndOwner As Long, ByVal nFolder As Long, pidl As Long) As Long
    44.  
    45. Private Declare Function api_SHGetPathFromIDList Lib _
    46.    "shell32.dll" Alias "SHGetPathFromIDList" _
    47.    (ByVal pidl As Long, ByValsPath As String) _
    48.    As Long
    49.  
    50. Public Sub m_CreateShortcut(ScFolder As Variant, ScCaption As _
    51.    String, TargetPath As String, Optional ScParam As String, _
    52.    Optional StartFolder As String, Optional IcoNum As Integer, _
    53.    Optional IcoPath As String, Optional WindowMode As Integer)
    54.  
    55. 'If you want to use one of the windows folders for the shortcut
    56. 'location, you can pass one of the constants defined in the declarations, e.g.,
    57. ' CSIDL_PROGRAMS  = Programs
    58. ' CSIDL_STARTUP = Startup
    59. ' CSIDL_RECENT = RecentDocs
    60. ' CSIDL_DESKTOP = Desktop
    61.  
    62. 'NOTE: AS WRITTEN THIS CODE MUST BE PLACED
    63. 'WITHIN A FORM MODULE
    64.  
    65. 'Example:  Puts a shortcut to Notepad on the desktop with
    66. '          a .txt document to be opened
    67.  
    68. ' m_CreateShortcut CSIDL_DESKTOP, "MyFile", _
    69. '   "C:\windows\Notepad.exe", "C:\MyFile.txt"
    70.    
    71. Dim Shortcut0 As String 'Full path for the temporary shortcut
    72.                          'created in the RecentDocs folder.
    73. Dim n0 As Integer       'Cusror position in Shortcut0.
    74. Dim x0 As String * 1    'Variable while reading Shortcut0.
    75. Dim l0 As Long          'Lenth of the Shortcut0 file.
    76. Dim Shortcut1 As String 'Full path for the final shortcut.
    77. Dim n1 As Integer       'Cusror position in Shortcut1
    78. Dim x1 As String * 1    'Variable while reading Shortcut1.
    79. Dim l1 As Long          'Lenth of the Shortcut1 file
    80.  
    81. Dim T As Double
    82. Dim p As Long
    83. Dim i As Integer
    84. Dim x As String
    85. Dim y0 As String * 2
    86.  
    87. 'Check for the target folder
    88. If IsNumeric(ScFolder) Then
    89.     ScFolder = p_GetSpecialFolder(CInt(ScFolder))
    90. ElseIf Dir$(ScFolder, vbDirectory) = "" Then
    91.     MsgBox "Le répertoire '" & ScFolder & "' est introuvable.", _
    92.        vbCritical, "Création d'un raccrourci"
    93.     Exit Sub
    94. End If
    95.  
    96. 'Create a temporary shortcut with only the
    97. 'target in the the RecentDocs.
    98. If api_SHAddToRecentDocs(2, TargetPath) > 0 Then
    99.  
    100.     'Full path of the created shortcut
    101.     Shortcut0 = p_GetSpecialFolder(8) & "\" & _
    102.         p_File_Folder(TargetPath) & ".lnk"
    103.  
    104.     'Waiting for the end of the creation.
    105.     T = Now()
    106.     Do Until (Dir$(Shortcut0) <> "")
    107.    
    108.     If (Now() - T) > 0.00006 Then 'wait 5 seconds
    109.         If MsgBox("Attendre encore la création du raccourci ?", _
    110.             vbQuestion + vbOKCancel, "Raccourci") <> vbOK Then
    111.             Exit Sub
    112.         Else
    113.             T = Now()
    114.         End If
    115.     End If
    116.    
    117.     Loop
    118.  
    119.     'Open the temporary shortcut file in read mode.
    120.     n0 = FreeFile()
    121.     Open Shortcut0 For Binary Access Read As #n0
    122.     'Wait for the file is correctly feed.
    123.     Do Until LOF(n0) > 0
    124.     Loop
    125.     l0 = LOF(n0)
    126.  
    127.     'Open the shortcut file to create
    128.     Shortcut1 = ScFolder & "\" & ScCaption & ".lnk"
    129.     n1 = FreeFile()
    130.     Open Shortcut1 For Binary Access Write As #n1
    131.  
    132.     'Look for the last byte to get
    133.     p = (l0 - 4)
    134.     y0 = ""
    135.     Do Until (p <= 0) Or (y0 = vbNullChar & vbNullChar)
    136.         Get #n0, p, y0
    137.         p = p - 1
    138.     Loop
    139.     l1 = p + 2
    140.  
    141.     'Copy bytes
    142.     For p = 1 To l1
    143.  
    144.         Get #n0, p, x0
    145.  
    146.         Select Case p
    147.         Case 21 'path for icon, startup, parameters
    148.             i = 3
    149.             If StartFolder <> "" Then
    150.                 i = i + 16
    151.             End If
    152.             If ScParam <> "" Then
    153.                 i = i + 32
    154.             End If
    155.             If (IcoPath <> "") Or (IcoNum > 0) Then
    156.                 i = i + 64
    157.             End If
    158.             x1 = Chr$(i)
    159.         Case 57 'Icon index
    160.             x1 = Chr$(IcoNum)
    161.         Case 61 'Window mode
    162.             x1 = Chr$(WindowMode)
    163.         Case Else
    164.             x1 = x0
    165.         End Select
    166.  
    167.         Put #n1, p, x1
    168.  
    169.     Next p
    170.  
    171.     'Close and delete the temporary shorcut
    172.     Close #n0
    173.     Kill Shortcut0
    174.  
    175.     'Add the Start folder, parameters and icon file
    176.     x = ""
    177.     If StartFolder <> "" Then
    178.         x = x & Chr$(Len(StartFolder)) & vbNullChar & StartFolder
    179.     End If
    180.     If ScParam <> "" Then
    181.         x = x & Chr$(Len(ScParam)) & vbNullChar & ScParam
    182.     End If
    183.     If IcoPath = "" Then
    184.         If IcoNum > 0 Then
    185.             x = x & Chr$(Len(TargetPath)) & vbNullChar _
    186.                & TargetPath
    187.         End If
    188.     Else
    189.         x = x & Chr$(Len(IcoPath)) & vbNullChar & IcoPath
    190.     End If
    191.     x = x & String(4, vbNullChar)
    192.     Put #n1, l1 + 1, x
    193.  
    194.     Close #n1
    195.  
    196. Else
    197.  
    198.     MsgBox "Error when creating the shortcut.", _
    199.           vbCritical, "Shortcut"
    200.  
    201. End If
    202.  
    203. End Sub
    204.  
    205. Private Function p_GetSpecialFolder(CsIdl As Long) As String
    206.  
    207. 'Returns the full path of the folder corresponding to the
    208. 'Windows's id system folder.
    209.  
    210. Dim r     As Long
    211. Dim pidl  As Long
    212. Dim sPath As String
    213.  
    214. r = api_SHGetSpecialFolderLocation(Me.hWnd, CsIdl, pidl)
    215.  
    216. If r = 0 Then
    217.  
    218.     sPath = Space$(260)
    219.     r = api_SHGetPathFromIDList(ByVal pidl, ByVal sPath)
    220.     If r Then
    221.         p_GetSpecialFolder = Left$(sPath, _
    222.            InStr(sPath, Chr$(0)) - 1)
    223.     End If
    224.  
    225. End If
    226.  
    227. End Function
    228.  
    229. Private Function p_File_Folder(FullPath As String) As String
    230. 'Returns the name of the file alone.
    231.  
    232. Dim i As Integer
    233.  
    234. p_File_Folder = FullPath
    235. i = Len(FullPath)
    236. Do Until i = 0
    237.     If Mid$(FullPath, i, 1) = "\" Then
    238.         p_File_Folder = Mid$(FullPath, i + 1)
    239.         i = 0
    240.     Else
    241.         i = i - 1
    242.     End If
    243. Loop
    244.  
    245. End Function
    -= a peet post =-

  3. #3
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286
    See this link

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