Results 1 to 9 of 9

Thread: Creating Shortcut (.lnk)'s

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    68

    Red face

    Hi all,

    Does anybody know how to create shortcuts in VB?

    I need an API function that will allow me to create shortcut (.lnk) files on the desktop. I must be able to set the start in directory as well as the Command Line...

    Any ideas??


    Also... Any body know any good API - from a Vb perspective, and Object books??
    THE DOUGSTER!!!!!!


    *-MCSD-*

  2. #2
    Guest
    Use the fCreateShellLink api function.

    Code:
    In Visual Basic 5.0, change Stkit432.dll in the following
    'statement to Vb5stkit.dll.  Stkit432.dll is for Visual Basic 4.0
    
    Private Declare Function fCreateShellLink Lib "STKIT432.DLL" (ByVal
    lpstrFolderName As String, ByVal lpstrLinkName As String, ByVal lpstrLinkPath As
    String, ByVal lpstrLinkArgs As String) As Long
    
    Private Sub Command1_Click()
        Dim lReturn As Long
        'Add to Desktop
        lReturn = fCreateShellLink("..\..\Desktop", _
        "Shortcut to MyProg", "c:\myprogram\myprog.exe", "")
    End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    68
    Thanks for the reply... However I am running VB6 on win 98 and when i try to run the app it says it cant find the stkit dll file???

    Also with this API can you set the working directory?
    THE DOUGSTER!!!!!!


    *-MCSD-*

  4. #4
    Guest
    Ok, I found it.

    Code:
    Private Declare Function fCreateShellLink _
    Lib "VB6STKIT.DLL" (ByVal _
    lpstrFolderName As String, ByVal lpstrLinkName As String, _
    ByVal lpstrLinkPath As _
    String, ByVal lpstrLinkArgs As String) As Long
    What do you mean by "working directory"?

  5. #5
    Addicted Member Cbomb's Avatar
    Join Date
    Jul 1999
    Posts
    153

    Lightbulb Oh.

    I think he means the directory in which the link is created. And if this is so...take a look at the first arg when calling fCreateShellLink
    Cbomb
    Techie

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    68

    Wink

    Thanks for the help.

    What I mean by working deirectory is the Start In Dir.

    So basicly the dir where the exe is actually started. Due to the nature of the application that I am installing it requires that the EXE's be fired up in a certain DIR because they need to read an INI file.

    You know when you go into the properties of a shortcut you get the START IN, or working DIR, That is what i need to specify.


    THE DOUGSTER!!!!!!


    *-MCSD-*

  7. #7
    Hyperactive Member
    Join Date
    Apr 1999
    Location
    Prague, Czech Republic
    Posts
    350

    Question

    And how can I retrieve the original file name (icon, title etc.) of the LNK or PIF file?

    I made some silly code reading the binary file, but this is very slow (if you have more files) so I believe there is an API, isn't?

  8. #8
    Guest

    Thumbs up

    API is your answer.

    Explanation:

    (A) We are using the calculator as an example
    (B) Make a shortcut of the calculator on your desktop
    (C) Make sure the shortcut calculator is named "Calc"
    (1) Find the Window (even if it means the shortcut)
    (2) Get the exe by the given handle
    (3) Tell you the handle
    (4) The calculator will become the focused window when you open it
    (5) If the calculator doesn't have focus, give it focus (this will give it focus no matter what)
    (6) Get the title of the active window
    (7) Extract the Icon as well


    Code:
    'Needed:  Form, Command Button, Picturebox, Module
    
    'Module code:
    
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    
    Private Declare Function GetWindowTextLength Lib "user32" Alias _
    "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
    (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    
    Private Declare Function ExtractAssociatedIcon Lib "shell32.dll" Alias "ExtractAssociatedIconA" (ByVal hInst As Long, ByVal lpIconPath As String, lpiIcon As Long) As Long
    
    Private Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Long
    
    Public Const TH32CS_SNAPPROCESS As Long = 2&
    Public Const MAX_PATH As Long = 260
    Public Type PROCESSENTRY32
    
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwflags As Long
    szexeFile As String * MAX_PATH
    End Type
    
    Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    
    Public Declare Function CreateToolhelpSnapshot Lib "Kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlgas As Long, ByVal lProcessID As Long) As Long
    
    Public Declare Function ProcessFirst Lib "Kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    
    Public Declare Function ProcessNext Lib "Kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    
    Public Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
    
    Public Declare Function SetForegroundWindow _
    Lib "user32" (ByVal hwnd As Long) As Long
    
    
    Public Function GetExeFromHandle(hwnd As Long) As String
        Dim threadID As Long, processID As Long, hSnapshot As Long
        Dim uProcess As PROCESSENTRY32, rProcessFound As Long
        Dim i As Integer, szExename As String
        ' Get ID for window thread
        threadID = GetWindowThreadProcessId(hwnd, processID)
        ' Check if valid
        If threadID = 0 Or processID = 0 Then Exit Function
        ' Create snapshot of current processes
        hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)   ' Check if snapshot is valid
        If hSnapshot = -1 Then Exit Function
        'Initialize uProcess with correct size
        uProcess.dwSize = Len(uProcess)
        'Start looping through processes
        rProcessFound = ProcessFirst(hSnapshot, uProcess)
        Do While rProcessFound
            If uProcess.th32ProcessID = processID Then
                'Found it, now get name of exefile
                i = InStr(1, uProcess.szexeFile, Chr(0))
                If i > 0 Then szExename = Left$(uProcess.szexeFile, i - 1)
                Exit Do
            Else
                'Wrong ID, so continue looping
                rProcessFound = ProcessNext(hSnapshot, uProcess)
            End If
        Loop
        Call CloseHandle(hSnapshot)
        GetExeFromHandle = szExename
    End Function
    
    
    Public Function GetActiveWindowTitle(ByVal ReturnParent As Boolean) As String
       Dim i As Long
       Dim j As Long
    
       i = GetForegroundWindow
    
    
       If ReturnParent Then
          Do While i <> 0
             j = i
             i = GetParent(i)
          Loop
    
          i = j
       End If
    
       GetActiveWindowTitle = GetWindowTitle(i)
    End Function
    Public Function GetWindowTitle(ByVal hwnd As Long) As String
       Dim l As Long
       Dim s As String
    
       l = GetWindowTextLength(hwnd)
       s = Space(l + 1)
    
       GetWindowText hwnd, s, l + 1
    
       GetWindowTitle = Left$(s, l)
    End Function
    
    
    'Form declarations:
    
    Dim stopit As Boolean
    Dim handl As String
    Dim retFile As String
    Dim retActiveWin As String
    
    Private Sub Command1_Click()
        handl = FindWindow(vbNullString, "Calculator") 'Find the Calculator
        If handl <> 0 Then
            'Calculator found
            Call SetForegroundWindow(handl) 'give calculator focus
            retFile = GetExeFromHandle("" & handl & "") 'get/add exe to string
            Debug.Print GetExeFromHandleretFile 'print exe
            retActiveWin = GetActiveWindowTitle(True) 'get/add active window to string
            Debug.Print retActiveWin 'print active window
            Picture1.Cls 'clear picture box
            lIcon = ExtractAssociatedIcon(App.hInstance, retFile, -1) 'extract icon
            Call DrawIconEx(Picture1.hdc, 0, 0, lIcon, 32, 32, 0, 0, 3) 'draw it in picture box
            DoEvents
            stopit = True 'stopit = picture is in picturebox
            If stopit = False Then 'if not picture is in picturebox then
            Call SetForegroundWindow(Me.hwnd) 'give form focus
            Command1_Click 'click command1 until the picture stays
        Else
            Exit Sub 'Window not found
        End If
        End If
    End Sub

    Hope that is what you are trying to do.
    I did make an example, which I had to do to make sure what I was doing worked, if you would rather download the example, I will send it to you.

    Enjoy!

  9. #9
    Guest
    Originally posted by Dhugal
    Thanks for the reply... However I am running VB6 on win 98 and when i try to run the app it says it cant find the stkit dll file???

    Also with this API can you set the working directory?
    Originally posted by Jhd.Honza
    And how can I retrieve the original file name (icon, title etc.) of the LNK or PIF file?

    I made some silly code reading the binary file, but this is very slow (if you have more files) so I believe there is an API, isn't?

    I guess that answers both your questions.
    Working directory = Location of original exe where the shorcut is actually linking to.
    And Jhd.Honza is already spelled out for me.

    I hope that helps both of you out.

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