Solved

Shell, ShellExecute, CreateProcess..blah blah blah

Posted on 2000-04-19
24
539 Views
Last Modified: 2010-05-18
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
Comment
Question by:groone
  • 9
  • 5
  • 5
  • +4
24 Comments
 
LVL 5

Expert Comment

by:KDivad
ID: 2733108
Simplest way:

x = Shell("Start " & LNKFileName)
0
 
LVL 3

Author Comment

by:groone
ID: 2733163
Sorry KDivad, shell doesnt work that way.  Thanks for the input though
0
 
LVL 3

Author Comment

by:groone
ID: 2733175
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
 
LVL 9

Expert Comment

by:Ruchi
ID: 2733291
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2733360
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
 
LVL 5

Expert Comment

by:KDivad
ID: 2733636
wsh2, Thanks for the defense.
0
 

Expert Comment

by:RuslanM
ID: 2733687
Just use :
Shell Explorer.exe & " " & DirectoryName

It should work.
0
 
LVL 3

Author Comment

by:groone
ID: 2734385
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
 

Expert Comment

by:RuslanM
ID: 2734429
Try this :
Shell "Explorer.exe " " & LinkFileName
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2734976
RuslanM:
I think you mean the Directory/FileName contained in the shortcut, rather than the shorcut itself.. <smile>.

Shell "Explorer.exe """ & FileName & """"
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2737043
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
 
LVL 3

Author Comment

by:groone
ID: 2737390
Yes it opens the directory for browsing, BUT it creates a dos incorrect parameter error.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 5

Expert Comment

by:KDivad
ID: 2738572
That's odd, because it doesn't on my machine.
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2738668
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
 
LVL 3

Author Comment

by:groone
ID: 2738707
hmm...interesting wsh2

I will try that as soon as I get home
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2738716
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
 
LVL 4

Expert Comment

by:dreamvb
ID: 2738868
Dim zFilename As String
 zFilename = "C:\MyFolder.lnk"
  Shell "start.exe " & zFilename, vbHide
 
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2739411
<<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
 
LVL 3

Author Comment

by:groone
ID: 2739481
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
 
LVL 14

Accepted Solution

by:
wsh2 earned 90 total points
ID: 2739978
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
 
LVL 3

Author Comment

by:groone
ID: 2740050
rofl..you sure did mention that.  how could I have over looked hat.
0
 
LVL 3

Author Comment

by:groone
ID: 2740069
Adjusted points from 50 to 90
0
 
LVL 3

Author Comment

by:groone
ID: 2740070
Wsh2, perfect thanks!
0
 
LVL 9

Expert Comment

by:Dalin
ID: 2769387
viewed
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now