Results 1 to 18 of 18

Thread: How to Shell a program with switches?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    38

    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?

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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 0vbNullString"""C:\Windows\system32\xcopy.exe """"<--put your parameters here-->"vbNullStringvbNormalFocus

    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.

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    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"

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to Shell a program with switches?

    Quote Originally Posted by DataMiser View Post
    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.

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How to Shell a program with switches?

    Quote Originally Posted by jmsrickland View Post
    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.

  10. #10
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,653

    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.

  11. #11
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to Shell a program with switches?

    Quote Originally Posted by fafalone View Post
    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.

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How to Shell a program with switches?

    Quote Originally Posted by DataMiser View Post
    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.

  13. #13
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,653

    Re: How to Shell a program with switches?

    Quote Originally Posted by jmsrickland View Post
    Kind of like in post #2, right
    No, not at all. Mind your own business

  14. #14

    Thread Starter
    Member
    Join Date
    Mar 2011
    Posts
    38

    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!

  15. #15
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: How to Shell a program with switches?

    Quote Originally Posted by dilettante View Post
    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.

    Quote Originally Posted by Bonnie West View Post
    ... on Windows Vista & 7, the Shell function apparently calls the CreateProcessW API function ...
    Quote Originally Posted by dilettante View Post
    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:

    Quote Originally Posted by Bonnie West View Post
    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.

    Quote Originally Posted by ksquared View Post
    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)

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.

  17. #17
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How to Shell a program with switches?

    Quote Originally Posted by fafalone View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,653

    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
  •  



Click Here to Expand Forum to Full Width