Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 556
  • Last Modified:

Shell, ShellExecute, CreateProcess..blah blah blah

I have tried using

shell
shellexecute
createprocess

to run an shortcut which points to a folder.  If I have testdir.lnk that poins to c:\testdir none of these calls will open the directory.  how can i open the directory a shortcut poins to.  Cant alter the shortcut.  The assumption is that the user is stupid and just clicks on pretty pictures.
0
groone
Asked:
groone
  • 9
  • 5
  • 5
  • +4
1 Solution
 
KDivadCommented:
Simplest way:

x = Shell("Start " & LNKFileName)
0
 
grooneAuthor Commented:
Sorry KDivad, shell doesnt work that way.  Thanks for the input though
0
 
grooneAuthor Commented:
Let me rephrase that...it works that way, BUT it produces an error to open the directory folder and it does not support long names.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
RuchiCommented:
0
 
wsh2Commented:
In support of KiDavid's comment.. it will support long file names if you just add a double quote before and after the LNKFileName. Eg.

Dim strFileNameLink as String
strFileNameLink = "c:\windows\desktop\Microsoft Word.lnk"

Shell("Start " & """" & strFileNameLink & """")

Where """" = 4 Double quotes in a row.

With that.. his solution works very nicely.. <smile>.



0
 
KDivadCommented:
wsh2, Thanks for the defense.
0
 
RuslanMCommented:
Just use :
Shell Explorer.exe & " " & DirectoryName

It should work.
0
 
grooneAuthor Commented:
Shell("Start " & """" & strFileNameLink & """")

Works if the directory names are not long

If I had

strFileNameLink = "C:\my long named directory\and its long named subdirectory\shortcut to program.lnk"

Shell would not work, no matter what you did, AND with short names it still produces a dos error about incorrect parameter settings

0
 
RuslanMCommented:
Try this :
Shell "Explorer.exe " " & LinkFileName
0
 
wsh2Commented:
RuslanM:
I think you mean the Directory/FileName contained in the shortcut, rather than the shorcut itself.. <smile>.

Shell "Explorer.exe """ & FileName & """"
0
 
KDivadCommented:
Here's what I did:

1. Created a shortcut to the folder "C:\Program Files\Accessories" and named it "Access.lnk"
2. Put it in the folder "C:\Program Files"
3. Shell "Start " & Chr$(34) & "C:\Program Files\Access.lnk" & Chr$(34)

It worked fine.
0
 
grooneAuthor Commented:
Yes it opens the directory for browsing, BUT it creates a dos incorrect parameter error.
0
 
KDivadCommented:
That's odd, because it doesn't on my machine.
0
 
wsh2Commented:
KDivad.. your code runs perfectly.. <smile>. But I just saw something in groone's comment.. that could cause the error.. DOS only supports Directory pathnames up to 68 characters long.. <sigh>.. and groone loaded an epic.. <sheesh>.

Its still ok though.. we can still get around it with code.. <smile>. Using Groone's example:

Dim strLinkPath as String
strLinkPath = "C:\my long named directory\and its long named subdirectory\"

Dim strLinkFile as String
strLinkFile = "shortcut to program.lnk"

' Change Directory
ChDir strLinkPath

' Shell directly to the Link
Shell("Start " & """" & strLinkFile & """")

' Restore Directory
ChDir App.Path



0
 
grooneAuthor Commented:
hmm...interesting wsh2

I will try that as soon as I get home
0
 
wsh2Commented:
Well.. now that I have a little time.. let me show you another way to do what the questioner wants.. <smile>. In your project set a reference (VB Menu -> Project -> References) to the "Windows Scripting Host Object" (WSHOM.OCX).. change the wshShell.CreateShortcut parameter below to a legitimate Link.. and Voile.. you have a Link to Executable runtime.. with no 68 character limitation.. <smile>.

<----- Code Begin ----->

Dim wshShell As IWshShell_Class
Set wshShell = CreateObject("Wscript.Shell")

Dim wshShortCut As IWshShortcut_Class
Set wshShortCut = wshShell.CreateShortcut("c:\windows\desktop\winamp.lnk") ' <-- CHANGE

Shell (wshShortCut.TargetPath)

<----- Code End ----->
0
 
dreamvbCommented:
Dim zFilename As String
 zFilename = "C:\MyFolder.lnk"
  Shell "start.exe " & zFilename, vbHide
 
0
 
KDivadCommented:
<<DOS only supports Directory pathnames up to 68 characters long>>

Are you sure? I switched to DOS and played with this folder: "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\40\BIN" (which is 75 chars long) and had no trouble. Besides, if that was the case, should the folder have opened at all? groone says "Yes it opens the directory for browsing, BUT it creates a dos incorrect parameter error".

Groone, I have some code to extract the file/folder from a .lnk file. You could do this and use: Shell "explorer.exe " & Foldername as was previously mentioned. I haven't posted (or even mentioned) it because it took me a good while to figure it out and (in my opinion) is worth more than 50 points. The code itself is relatively simple, it just took me a while to decipher enough of the .lnk to get it. Returns either short or long path as desired.
0
 
grooneAuthor Commented:
I'm working on trying to get the command line from the lnk.  There is a api call that does it, just doing some reading to figure out how it works.  If I get it, perhaps we can share code.  I's funny, because if I use the command line of the lnk insead of the actual shortcut the folder opens just fine.

As of right now, I sill get the dos box stating an incorrect parameter setting.

This is one of the wierdest things I have ever seen.  I believe there is more to it then jus saying
shell path, vbNormalShow
(the T on my keyboard is going out)

Unforunately I don have many more points so i will have o just hope for the bes...thanks anyway.
0
 
wsh2Commented:
You don't need APIs.. as I indicated earlier.. use the "Windows Scripting Host" to get the Shortcut.. extract the executable name, and then run it.

--------------------------------------
In your project set a reference (VB Menu -> Project -> References) to the "Windows Scripting Host Object" (WSHOM.OCX)

<----- Code Begin ----->

Dim wshShell As IWshShell_Class
Set wshShell = CreateObject("Wscript.Shell")

Dim wshShortCut As IWshShortcut_Class
Set wshShortCut = wshShell.CreateShortcut("c:\windows\desktop\winamp.lnk")

Shell (wshShortCut.TargetPath)

<----- Code End ----->

It is 4 lines of code.. and then a Shell command to run it.

======================================
As to the Invalid Parameter message, I am curious as to how your COMSPEC environment variable reads.


 
0
 
grooneAuthor Commented:
rofl..you sure did mention that.  how could I have over looked hat.
0
 
grooneAuthor Commented:
Adjusted points from 50 to 90
0
 
grooneAuthor Commented:
Wsh2, perfect thanks!
0
 
DalinCommented:
viewed
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 5
  • 5
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now