Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

API -> Creating shortcuts.

Status
Not open for further replies.

wanmaster

Programmer
Nov 20, 2000
94
0
0
NL
I want to create my own setup program, and what I need to know is: are there API-functions to create shortcuts?
I know there is in a dll called something like 'vb6kit.dll', but in order to keep the program small, I would like to use the 'normal' libraries like user32, shell32 etc.
 
I don't know if this helps, but when you manually create a shortcut, the program rundll32.exe is executed (this shows up Task Manager) on Windows2000 at least.

As to what dll's it uses and the API calls involved .....????

Simon
 
Thanks, good idea..
Using the VB-Dependency walker, I found out what dll's it uses. I think it's the kernel32.dll. So I will continue my quest for the holy api call...
 
Shell32 provides the functions to to manipulate Shell Links (shortcuts).
Microsoft provides a Microsoft Windows Installer and installation database API with windows, If you have some knowledge of C you could access those DLL to do the work for you. Information on The Microsoft Windows Installer and its function references (all in C++) can be found on MSDN

Good Luck, and please doo me a favour and post on what you come up with
Collin
 
I haven't tested this code but a quick scan didn't show any problems with it. It should create three shortcuts in the Start Menu and then rename them:
[tt]
'Module level

Public Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type

Public Const FO_MOVE = &H1
Public Const FO_RENAME = &H4

Public Const FOF_SILENT = &H4
Public Const FOF_NOCONFIRMATION = &H10
Public Const FOF_FILESONLY = &H80
Public Const FOF_SIMPLEPROGRESS = &H100
Public Const FOF_NOCONFIRMMKDIR = &H200

Public Const SHARD_PATH = &H2&


Public Declare Function SHAddToRecentDocs Lib "shell32.dll" _
(ByVal dwFlags As Long, ByVal dwData As String) As Long

Public Declare Function SHFileOperation _
Lib "shell32.dll" Alias "SHFileOperationA" _
(lpFileOp As SHFILEOPSTRUCT) As Long

Public Declare Function SHGetPathFromIDList _
Lib "shell32.dll" Alias "SHGetPathFromIDListA" _
(ByVal pidl As Long, ByVal pszPath As String) As Long

Public Declare Function SHGetSpecialFolderLocation _
Lib "shell32.dll" _
(ByVal hwndOwner As Long, _
ByVal nFolder As Long, _
pidl As Long) As Long


'Form Level

Option Explicit
Private Sub Command1_Click()
Dim r As Long
Dim i As Integer
Dim FolderPath As String
Dim StartMenuPath As String
Dim fNameOld As String
Dim fNameNew As String
Dim fNames() As String
ReDim fNames(1 To 3) As String
Const CSIDL_RECENT = &H8
Const CSIDL_STARTMENU = &HB

FolderPath = GetSpecialFolder(CSIDL_RECENT)
StartMenuPath = GetSpecialFolder(CSIDL_STARTMENU)
If FolderPath = "" Or StartMenuPath = "" Then
MsgBox "Error retrieving folder paths."
Exit Sub
End If

StartMenuPath = StartMenuPath & "Programs\MyApp\"
'Create shortcuts to these files
fNames(1) = "d:\MyApp\My.exe"
fNames(2) = "d:\MyApp\My.hlp"
fNames(3) = "d:\MyApp\MyReadme.txt"
'First, add them to the recent files list
r = SHAddToRecentDocs(SHARD_PATH, fNames(1))
r = SHAddToRecentDocs(SHARD_PATH, fNames(2))
r = SHAddToRecentDocs(SHARD_PATH, fNames(3))

fNames(1) = FolderPath & "My.exe.lnk"
fNames(2) = FolderPath & "My.hlp.lnk"
fNames(3) = FolderPath & "MyReadme.txt.lnk"
'Move the shortcuts to the start menu
ShellMoveFiles fNames(), StartMenuPath
'Optionally, you can rename the shortcuts
fNameOld = StartMenuPath & "My.exe.lnk"
fNameNew = StartMenuPath & "New Name for My Exe.lnk"
ShellRenameFile fNameOld, fNameNew
fNameOld = StartMenuPath & "My.hlp.lnk"
fNameNew = StartMenuPath & "New Name for My Help.lnk"
ShellRenameFile fNameOld, fNameNew
fNameOld = StartMenuPath & "MyReadme.txt.lnk"
fNameNew = StartMenuPath & "New Name for My Readme.lnk"
ShellRenameFile fNameOld, fNameNew
End Sub

Private Sub ShellMoveFiles(sFileArray() As String, sDestination As String)
Dim r As Long
Dim i As Integer
Dim sFiles As String
Dim SHFileOp As SHFILEOPSTRUCT
For i = LBound(sFileArray) To UBound(sFileArray)
sFiles = sFiles & sFileArray(i) & Chr$(0)
Next
sFiles = sFiles & Chr$(0)
With SHFileOp
.wFunc = FO_MOVE
.pFrom = sFiles
.pTo = sDestination
.fFlags = FOF_SILENT Or FOF_NOCONFIRMATION Or FOF_NOCONFIRMMKDIR
End With
r = SHFileOperation(SHFileOp)
End Sub


Private Function GetSpecialFolder(CSIDL As Long) As String
Dim sPath As String
Dim pidl As Long
Const ERROR_SUCCESS = 0
Const MAX_LENGTH = 260
If SHGetSpecialFolderLocation(Me.hWnd, CSIDL, pidl)= ERROR_SUCCESS Then
sPath = Space$(MAX_LENGTH)
If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then
GetSpecialFolder = Left$(sPath, _
InStr(sPath, Chr$(0)) - 1) & "\"
End If
End If
End Function

Private Sub ShellRenameFile(sOldName As String, sNewName As String)
Dim SHFileOp As SHFILEOPSTRUCT
Dim r As Long
sOldName = sOldName & Chr$(0) & Chr$(0)
sNewName = sNewName & Chr$(0) & Chr$(0)
Print sOldName
Print sNewName
With SHFileOp
.wFunc = FO_RENAME
.pFrom = sOldName
.pTo = sNewName
.fFlags = FOF_SILENT Or FOF_NOCONFIRMATION
End With
r = SHFileOperation(SHFileOp)
End Sub
[/tt]
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
Thanx all!

I've taken a look at shell32, and I only found a function called SHGetNewLinkInfo, no direct function to create shortcuts.

And I tried Alt255's code, and it works quit well.
The only problem I had is that it added the shortcuts to the recent folder, but it didn't copy them to the start menu. (tried in Win2000)
But since it already created shortcuts, it won't be much work to get them to right place.

I myself tried to find something in the registry about the start menu and its contents that could be helpful, but although I found some interesting things, it didn't really help.
And after getting sick of it, I used VB6STKIT.DLL after all.

But now... there's new hope (& enthousiasm)!

Remedy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top