Results 1 to 14 of 14

Thread: HowTo run a shortcut?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    155

    HowTo run a shortcut?

    I'm trying to run a Shortcut file(ext: "lnk"). I know you can't do this using Shell, but I heard somewhere you can do it with ShellExecute, I tried this:

    VB Code:
    1. Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    2.  
    3. ShellExecute Me.hwnd, "open", App.Path & "\TMP.lnk", vbNullString, vbNullString, SW_SHOWNORMAL

    But it didn't work, any suggestions?

  2. #2
    Junior Member
    Join Date
    Jun 2006
    Posts
    21

    Re: HowTo run a shortcut?

    VB Code:
    1. ShellExecute 0&, vbNullString, App.Path & "\TMP.lnk", vbNullString, vbNullString, vbNormalFocus

    The declarations are:
    VB Code:
    1. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

  3. #3
    Junior Member
    Join Date
    Jun 2006
    Posts
    21

    Re: HowTo run a shortcut?

    I'm too lazy to test it so tell me if it works.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: HowTo run a shortcut?

    No it wont work but you could use Shell32 and get a reference to the lnk file and do a .InvokeVerb "open" on it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    155

    Re: HowTo run a shortcut?

    Quote Originally Posted by RobDog888
    No it wont work but you could use Shell32 and get a reference to the lnk file and do a .InvokeVerb "open" on it.

    Can you explain this more please and/or show an example, thanks.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: HowTo run a shortcut?

    Sure, from my code bank thread ocde just uncomment the line for .InvokeVerb.

    http://vbforums.com/showthread.php?t=322799

    You can trim the code down as you dont need to be changing the arguments and all, just attach to the lnk and invoke.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    155

    Re: HowTo run a shortcut?

    I'm getting an "Object Error" at the part I commented out, I think i'm missing something::

    VB Code:
    1. Function CreateShortcut(ByVal Target As String, Desc As String, WindowStyle As Long)
    2.  
    3.     Set C = CreateObject("WScript.Shell")
    4.     Set link = C.CreateShortcut(App.Path & "\TMP.lnk")
    5.         link.Description = Desc$
    6.         link.Targetpath = Right$(ET_PATH$, Len(ET_PATH$) - 4) 'target is the IP and Port
    7.         link.Arguments = "+connect " & Target$
    8.         link.WindowStyle = WindowStyle&
    9.         link.WorkingDirectory = "C:\PROGRA~1\WOLFEN~1"
    10.         link.Save
    11.         '--> lnk.InvokeVerb "open"
    12.        
    13. End Function

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: HowTo run a shortcut?

    Change it from "lnk" to "link"

    Here is how I tested it...


    VB Code:
    1. Option Explicit
    2. 'References:
    3. 'Microsoft Shell Controls And Automation '(C:\Windows\System32\Shell32.dll)
    4. Private Const ssfPROGRAMS = 2            'Program Files
    5.  
    6. Private Sub Command1_Click()
    7.  
    8.     Dim oShell  As Shell32.Shell
    9.     Dim oFolder As Shell32.Folder
    10.    
    11.     Set oShell = New Shell32.Shell
    12.     Set oFolder = oShell.NameSpace(ssfPROGRAMS)
    13.         If (Not oFolder Is Nothing) Then
    14.             Dim oFolderItem As Shell32.FolderItem
    15.             Set oFolderItem = oFolder.ParseName("Internet Explorer.lnk")
    16.             If (Not oFolderItem Is Nothing) Then
    17.                 Dim oShellLink As ShellLinkObject
    18.                 Set oShellLink = oFolderItem.GetLink
    19.                 If (Not oShellLink Is Nothing) Then
    20.                     oFolderItem.InvokeVerb "open" 'Opens IE
    21.                 End If
    22.                 Set oShellLink = Nothing
    23.             End If
    24.             Set oFolderItem = Nothing
    25.         End If
    26.     Set oFolder = Nothing
    27.     Set oShell = Nothing
    28.    
    29. End Sub
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: HowTo run a shortcut?

    I don't have any problems launching a LNK file using ShellExecute. However for some reason you must pass vbNullString as the verb (the lpOperation argument), as showed by KieferHagin above. You can also use the Shell function if you use this little trick:
    VB Code:
    1. Shell Environ("COMSPEC") & " /c " & sPathToLnkFile

  10. #10
    Junior Member
    Join Date
    Jun 2006
    Posts
    21

    Re: HowTo run a shortcut?

    So my way DID work? Wow, surprising.

  11. #11
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    263

    Re: HowTo run a shortcut?

    Quote Originally Posted by KieferHagin
    VB Code:
    1. Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    2.  
    3. ShellExecute 0&, vbNullString, App.Path & "\TMP.lnk", vbNullString, vbNullString, vbNormalFocus
    I am writing a VB6 program that downloads my software package from a webpage, locate the Uninstall shortcut in my program's Start menu, execute this Uninstall shortcut, wait for it to complete, then launch the new Setup.exe I downloaded to re-install my package.

    This code works fine to run my uninstall shortcut, but it does not wait for completion before it attemps to re-install the new package. I have been pulling my hair on this problem for an entire day and I just can't find a way to run a shortcut and way for its completion!

    All the solutions I have found on internet use Wscript, shell, etc, methods that do not seem to allow running a shortcut file. It there a way to keep the ShellExecute method but to have some way to check if the uninstall method is over (remember this shortcut actually runs another program called msiexec.exe).

    Thanks

  12. #12
    Junior Member
    Join Date
    Jun 2006
    Posts
    21

    Re: HowTo run a shortcut?

    I can't think of anything off the top of my head, but if you ran a few tests, you could figure out the average time it takes to finish and then use a timer control to make the program wait until it thinks its done...just an idea.

  13. #13
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: HowTo run a shortcut?

    In this case I wouldn't actually launch the shortcut at all. Instead I would run the uninstaller the shortcut launches directly. You should launch that using CreateProcess and then use WaitForSingleObject to monitor it until it's done. Have a forum search for ShellAndWait and you'll find a bunch of example code on how to do that. The trick is to first know exactly how to run the uninstaller, you can probably use simular code as shown by RobDog above to read the content of the shortcut to see exactly how it launches the uninstaller.

  14. #14
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    263

    Re: HowTo run a shortcut?

    Thanks, it worked by extracting the EXE from the shortcut !!!

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