Link to home
Start Free TrialLog in
Avatar of visualbasic
visualbasicFlag for Ireland

asked on

Windows Run works! Shell command does not!!! Why???

When I shell a third party program from the windows Run... it work as it should but when I shell the same program using the same command line from my VB app the output differes.
I'm using the shell command.
How do I write a routine which performs like windows Run?
What is the Currdir from Windows Run...?

Avatar of trkcorp
trkcorp

A. How does it differ?
B. Someone correct me if I am wrong but I imagine the Cur Dir from Windows Run would be more like a directory list from the path setting.
ASKER CERTIFIED SOLUTION
Avatar of gencross
gencross

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
1. What is the command line you are trying to run?  Does it have long filenames and/or parameters?  If it does then you may have to use chr(34) for " marks.

2. Windows Run uses the path statement along with registry entries to run files.  It will first check the path for the file you are trying to run, then checks a set of program "aliases" from the registry that are mapped to files.
Avatar of Richie_Simonetti
to crate a procedure that works = to Run from Start menu:

dim o as object
set o = createobject(Wscript.shell")

o.run ""
Hey Richie...

Doesn't the wscript.shell require the use of a seperate dll?  Why not use the ShellExecute API and not worry about making sure everyone has the scripting object?  Are there other advanatages/disadvantages?  I really do want to know, I'm not being sarcastic :)
i hate to use objects (like FSO)when a simple API do the job (and better by the way) but visualbasic wants to emulate run command....

Point 1 from aeklund's comment is a matter to take in mind when you use Shell/ShellExecute functions.
ShellExecute will execute using the path as well.

retval = ShellExecute(Form1.hWnd, "open", "calc.exe", "-fast", CurDir, SW_MAXIMIZE)

Passing it "calc.exe" will start the calculator.
"Notepad.exe" will open notepad.

All you have to do is pass the CurDir as the working folder (I don't even know if this is required, but that is what I do).  I guess I am confused as to how ShellExecute differs from the run command in Windows.

There is no problem with shellexecute if you pass     lpDirectory as vbnullstring but you need to know the path of the target file (if it is not in the PATH environment variable)
I beleive ShellExecute does work like the run command like gencross demonstrated...

I was curious so I added this registry key:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\zzz.exe
with default value of "c:\winnt\system32\calc.exe"

now using the shellexecute command as so:
retval = ShellExecute(hwnd, "open", "zzz", "", "", SW_MAXIMIZE)

it does open calculater like the start/run does if you just type zzz
When you shell, you are submitting a command to the DOS command processor.  If the command is not recognized, it will not work.

To overcome this, you can often tell DOS to spawn a command window in which to run the command like this:

Shell("cmd /c RunMyWindowsCommand")

In other cases, you may need to prefix it with "start":

Shell("start RunMyWindowsCommand")

Try these to see if it makes a difference.

Also, in some cases you may need to enclose a command inside double-quotes:

Const cDoubleQuote as string = """"
Shell("cmd /c " & cDoubleQuote & "Run My Windows Command.exe" & cDoubleQuote)

Const cDoubleQuote as string = """"???
why not using chr$(34)?
or, if you like
dim cDoubleQuote as string
cDoubleQuote = chr$(34)
Richie,

Double quote is a constant, but constants don't support function definitions, so I'd love to say:

const cDoubleQuote = '"' or chr$(34)

but that won't work.

And I'd rather not create a new variable for something that will never change.  However, if you want a tiny bit more readability (at the expense of a tiny bit less efficiency) then your choice works:

dim cDoubleQuote as string
cDoubleQuote = chr$(34)
 
I used to do this, but later decided to do things "right" i.e. create constants when things are constants.

Personally, I don't know why VB doesn't have a built-in constant for this (vbDblQuote?) since it has one for a bunch of other things like C/R, L/F, tab, Backspace, Null character, etc.
"Personally, I don't know why VB doesn't have a built-in constant for this (vbDblQuote?) since it has one for a bunch of other things like C/R, L/F, tab, Backspace, Null character, etc. "
I agree 100%!
By the way, vissualbasic, do you need more help? any feedback?
One thing to note...

rspahitz,

>>In other cases, you may need to prefix it with "start":
>>Shell("start RunMyWindowsCommand")

I don't think that "start" is supported in NT or 2000, or even XP.
Start works in NT.
vinnyd79,

Ok, I didn't think it did.  Have you tried it in 2000 or XP?
It appears that start is not available on W2K, so I suspect the same on WXP.
I didn't think it was available in NT either, but I guess I was wrong.  I am just curious why MS decided to not support it anymore?

visualbasic, I'm not sure about what everyone else thinks, but I would say your best bet is to use ShellExecute, keeping in mind the other advise that was given about quotes.