|
-
Jul 27th, 2006, 04:58 PM
#1
Thread Starter
Addicted Member
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:
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
ShellExecute Me.hwnd, "open", App.Path & "\TMP.lnk", vbNullString, vbNullString, SW_SHOWNORMAL
But it didn't work, any suggestions?
-
Jul 27th, 2006, 05:03 PM
#2
Junior Member
Re: HowTo run a shortcut?
VB Code:
ShellExecute 0&, vbNullString, App.Path & "\TMP.lnk", vbNullString, vbNullString, vbNormalFocus
The declarations are:
VB Code:
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
-
Jul 27th, 2006, 05:05 PM
#3
Junior Member
Re: HowTo run a shortcut?
I'm too lazy to test it so tell me if it works.
-
Jul 27th, 2006, 05:10 PM
#4
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jul 27th, 2006, 05:37 PM
#5
Thread Starter
Addicted Member
Re: HowTo run a shortcut?
 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.
-
Jul 27th, 2006, 05:39 PM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jul 27th, 2006, 06:38 PM
#7
Thread Starter
Addicted Member
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:
Function CreateShortcut(ByVal Target As String, Desc As String, WindowStyle As Long)
Set C = CreateObject("WScript.Shell")
Set link = C.CreateShortcut(App.Path & "\TMP.lnk")
link.Description = Desc$
link.Targetpath = Right$(ET_PATH$, Len(ET_PATH$) - 4) 'target is the IP and Port
link.Arguments = "+connect " & Target$
link.WindowStyle = WindowStyle&
link.WorkingDirectory = "C:\PROGRA~1\WOLFEN~1"
link.Save
'--> lnk.InvokeVerb "open"
End Function
-
Jul 27th, 2006, 06:58 PM
#8
Re: HowTo run a shortcut?
Change it from "lnk" to "link" 
Here is how I tested it...
VB Code:
Option Explicit
'References:
'Microsoft Shell Controls And Automation '(C:\Windows\System32\Shell32.dll)
Private Const ssfPROGRAMS = 2 'Program Files
Private Sub Command1_Click()
Dim oShell As Shell32.Shell
Dim oFolder As Shell32.Folder
Set oShell = New Shell32.Shell
Set oFolder = oShell.NameSpace(ssfPROGRAMS)
If (Not oFolder Is Nothing) Then
Dim oFolderItem As Shell32.FolderItem
Set oFolderItem = oFolder.ParseName("Internet Explorer.lnk")
If (Not oFolderItem Is Nothing) Then
Dim oShellLink As ShellLinkObject
Set oShellLink = oFolderItem.GetLink
If (Not oShellLink Is Nothing) Then
oFolderItem.InvokeVerb "open" 'Opens IE
End If
Set oShellLink = Nothing
End If
Set oFolderItem = Nothing
End If
Set oFolder = Nothing
Set oShell = Nothing
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jul 27th, 2006, 07:14 PM
#9
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:
Shell Environ("COMSPEC") & " /c " & sPathToLnkFile
-
Jul 30th, 2006, 03:55 AM
#10
Junior Member
Re: HowTo run a shortcut?
So my way DID work? Wow, surprising.
-
Jul 31st, 2006, 02:41 PM
#11
Hyperactive Member
Re: HowTo run a shortcut?
 Originally Posted by KieferHagin
VB Code:
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
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
-
Jul 31st, 2006, 03:56 PM
#12
Junior Member
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.
-
Jul 31st, 2006, 05:45 PM
#13
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.
-
Aug 1st, 2006, 03:06 PM
#14
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|