-
Aug 28th, 2015, 04:25 PM
#1
Thread Starter
Member
How to Shell a program with switches?
I'm trying to run an exe from my VB6 program. I've tried variations on Shell with some luck. I can get a program like cmd.exe to run but I haven't yet found a way to pass global switches. What I'm trying to do is run XCOPY.EXE as xcopy/e/y/k/c sourcefile destinationfile. Even xcopy/? would be progress. When I try the string "xcopy/?", it fails because it thinks the program name is xcopy/?. If I try "xcopy /?", the program starts but doesn't see the global switch. Any thoughts or examples?
-
Aug 28th, 2015, 05:00 PM
#2
Re: How to Shell a program with switches?
Have you tried:
PHP 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
Private Sub Command1_Click() ShellExecute 0, vbNullString, """C:\Windows\system32\xcopy.exe """, "<--put your parameters here-->", vbNullString, vbNormalFocus
End Sub
Last edited by jmsrickland; Aug 29th, 2015 at 10:59 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Aug 28th, 2015, 06:15 PM
#3
Re: How to Shell a program with switches?
If that doesn't work you can do this:
Code:
Private Sub Command1_Click()
Open App.Path & "\temp.bat" For Output As #1
Print #1, "xcopy.exe /?" '<------------or whatever arguments you want
Print #1, "pause"
Close #1
Shell App.Path & "\temp.bat", vbNormalFocus
End Sub
Last edited by jmsrickland; Aug 29th, 2015 at 11:19 AM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Aug 28th, 2015, 10:31 PM
#4
Re: How to Shell a program with switches?
If I try "xcopy /?", the program starts but doesn't see the global switch. Any thoughts or examples?
I think you are just assuming it doesn't see the switch because the window closes right away. This is normal and doesn't mean it is not working.
I tried this as a test, I added the /p switch so it would wait for me to confirm the filecopy and allow me to see that it is in fact working
Code:
Shell "xcopy c:\test.txt D:\test.txt /p"
As expected it open a cmd window and executed xcopy with the source destination and switch. Xcopy then prompted me before it created the file and then the window went away when done.
using just the /? switch tells it to list the commands but there is no pause in there and the window will close as soon as the list has been shown which is quite likely far to fast to allow you to see what it did.
-
Aug 29th, 2015, 06:01 PM
#5
Re: How to Shell a program with switches?
The VB6 Shell() function seems to be a very thin wrapper around a call to the WinExec() function.
As they say there:
The executable name is treated as the first white space-delimited string in lpCmdLine. If the executable or path name has a space in it, there is a risk that a different executable could be run because of the way the function parses spaces. The following example is dangerous because the function will attempt to run "Program.exe", if it exists, instead of "MyApp.exe".
...
To avoid this problem, use CreateProcess rather than WinExec. However, if you must use WinExec for legacy reasons, make sure the application name is enclosed in quotation marks as shown in the example below.
So clearly to avoid ambiguity you'd want to do something like:
Shell """xcopy"" c:\source D:\dest /e /y /k /c"
However Xcopy also has to parse the rest of the data, the command line parameters, that you feed it. The same "space problem" exists for file names there, so it is safest to quote those as well:
Shell """xcopy"" ""c:\source"" ""D:\dest"" /e /y /k /c"
So both spaces and quotes are important in forming the command string to be executed. The quotes of course are not required as long as you know pathnames won't have any spaces, but the spaces are critical.
Thus this case should work just fine as long as Xcopy can be found:
Shell "xcopy c:\source D:\dest /e /y /k /c"
-
Aug 29th, 2015, 06:52 PM
#6
Re: How to Shell a program with switches?
/e/y/k/c
I don't know what these mean but they prevent xcopy from working. If you remove the /e then xcopy will work. It appears that only if xcopy is going to prompt you will it show it's window otherwise it will just flash and end meaning /? will not allow the help window to show even if you use the /p. The only way I got xcopy to show the help was to use a .bat file with the pause command following the xcopy
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Aug 29th, 2015, 07:42 PM
#7
Re: How to Shell a program with switches?
/e copies directories and sub directories even if empty /y suppresses any confirm prompts /k copies attributes and /c continues even if an error occurs
As for the help part you can see it easily by simply opening a command window and typing in xcopy /? When using shell it is pretty pointless to pass the /? as you will never see the window, just a blimp and it is gone.
If memory serves there was an option in the past to hold a command window open or close it when program is finished but I do not see this option now. Last time I remember seeing it was under NT 4.0 maybe it is gone now or maybe I just forgot where it was.
-
Aug 29th, 2015, 08:14 PM
#8
Re: How to Shell a program with switches?
Originally Posted by DataMiser
When using shell it is pretty pointless to pass the /? as you will never see the window, just a blimp and it is gone.
Not if you shell it like I did
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Aug 29th, 2015, 09:17 PM
#9
Re: How to Shell a program with switches?
Originally Posted by jmsrickland
Not if you shell it like I did
If you mean shelling a bat file then you are not really shelling to xcopy. You are shelling something else and having that hold the display.
As I said if you shell xcopy /? you will not see the window.
If you shell a bat then what you see is up to what you have coded into the bat file but that is not the same thing.
-
Aug 29th, 2015, 10:02 PM
#10
Re: How to Shell a program with switches?
You could just use ShellExecute instead, it makes it easier by having a whole separate argument for the switches. The window closes afterwards but anyway,
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 ShowWindowTypes) As Long
Call ShellExecute(Me.hWnd, "open", "xcopy", "/w C:\temp\orig.bmp C:\temp\copy.bmp", "C:\", SW_SHOW)
I used /w so it didn't just flash and close in my test, but it worked fine.
-
Aug 29th, 2015, 10:52 PM
#11
Re: How to Shell a program with switches?
Originally Posted by fafalone
You could just use ShellExecute instead, it makes it easier by having a whole separate argument for the switches. The window closes afterwards but anyway,
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 ShowWindowTypes) As Long
Call ShellExecute(Me.hWnd, "open", "xcopy", "/w C:\temp\orig.bmp C:\temp\copy.bmp", "C:\", SW_SHOW)
I used /w so it didn't just flash and close in my test, but it worked fine.
Kind of like in post #2, right
Last edited by jmsrickland; Aug 29th, 2015 at 11:00 PM.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Aug 29th, 2015, 10:53 PM
#12
Re: How to Shell a program with switches?
Originally Posted by DataMiser
If you mean shelling a bat file then you are not really shelling to xcopy. You are shelling something else and having that hold the display.
As I said if you shell xcopy /? you will not see the window.
If you shell a bat then what you see is up to what you have coded into the bat file but that is not the same thing.
I didn't know the rules stated you had to shell xcopy itself otherwise it doesn't count
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Aug 30th, 2015, 03:56 PM
#13
Re: How to Shell a program with switches?
Originally Posted by jmsrickland
Kind of like in post #2, right
No, not at all. Mind your own business
-
Aug 30th, 2015, 09:48 PM
#14
Thread Starter
Member
Re: How to Shell a program with switches?
Thanks everyone for your input. It is really appreciated! JM got me started and what I ended up with is:
Public Function do_xcopy(strSource As String, strDestination As String) As Long
Dim lngResult As Long
'(ByVal hwnd As Long, _ 0
' ByVal lpOperation As String, _ vbNullString
' ByVal lpFile As String, _ "C:\Windows\system32\xcopy.exe "
' ByVal lpParameters As String, _ "/e/y/k/c/i strSource strDestination"
' ByVal lpDirectory As String, _ vbNullString
' ByVal nShowCmd As Long) As Long vbNormalFocus
' ShellExecute 0, vbNullString, """C:\Windows\system32\xcopy.exe """, "/?", vbNullString, vbNormalFocus, "/e/y/k/c sourcefile destinationfile", vbNullString, vbNormalFocus
' lngResult = ShellExecute(0, vbNullString, "C:\Windows\system32\xcopy.exe ", "/?", vbNullString, vbNormalFocus)
lngResult = ShellExecute(0, vbNullString, "C:\Windows\system32\xcopy.exe ", "/e/y/k/c/i " & strSource & " " & strDestination, vbNullString, vbHide)
To complete this little beauty, I need a way to pend until the ShellExecute'ed program terminates. Is there a way?
I'm doing this in another program using CreateProcess as follows:
strCmdLine = "C:\Windows\system32\xcopy.exe /e/y/k/c/i " & strSource & " " & strDestination
intReturn = CreateProcessA(0&, strTool & strCmdLine, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
'Wait for the shelled application to finish
intReturn = WaitForSingleObject(proc.hProcess, INFINITE)
intReturn = CloseHandle(proc.hProcess)
Problem is I can't make the created process hidden. Any thoughts? I've found the WIN32API.TXT file which is helpful. Anything out there define all the stuff passed on a call?
Thanks!
-
Aug 31st, 2015, 12:13 AM
#15
Re: How to Shell a program with switches?
Originally Posted by dilettante
The VB6 Shell() function seems to be a very thin wrapper around a call to the WinExec() function.
As I've recently found out here, that no longer appears to be true as of Windows Vista & 7.
Originally Posted by Bonnie West
... on Windows Vista & 7, the Shell function apparently calls the CreateProcessW API function ...
Originally Posted by dilettante
So both spaces and quotes are important in forming the command string to be executed. The quotes of course are not required as long as you know pathnames won't have any spaces, but the spaces are critical.
From that same post:
Originally Posted by Bonnie West
On Windows Vista & 7, it appears that the intrinsic Shell function actually doesn't fail if the passed filename contains spaces yet isn't surrounded by quotes.
The documentation of the Shell function, however, warned against not using quotes:
There is some test code in that post in case anybody wants to verify the above claims on their systems.
Originally Posted by ksquared
To complete this little beauty, I need a way to pend until the ShellExecute'ed program terminates. Is there a way?
The Shell & Wait examples here might give you some ideas.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
-
Aug 31st, 2015, 01:35 AM
#16
Re: How to Shell a program with switches?
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
-
Aug 31st, 2015, 07:01 AM
#17
Re: How to Shell a program with switches?
Originally Posted by fafalone
No, not at all. Mind your own business
check yourself dude... the solution you posted, was essentially the same as what as proposed in post #2 - using ShellExecute - there's nothing wrong with that, you're late to the game (so am I), but there's no reason to get all snippy about it. Let's keep is clean and professional... it's not like this is a VB6-vs-VB.NET debate here.
-tg
-
Aug 31st, 2015, 08:26 PM
#18
Re: How to Shell a program with switches?
You check yourself. It was obviously a joke, and if the person who it was intended for didn't get it that's one thing and I'd gladly clarify and apologize, but for you to interject because you didn't get it, and had no interest in clarifying, just in assuming it was hostile, and get snippy at ME, well now I'm not joking: Mind your business.
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
|