DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: shell and wait

  1. #1
    Guy Guest

    shell and wait


    Hi,

    From my VB6 exe, I want to call another program and wait for it to return
    before continuing. I am having (at least) two problems with this, they may
    be related.

    1) When I use the shell command to call the other program, it leaves a DOS
    command window open, but I need it to close on its own. Here's my command
    syntax:

    RetVal = Shell("mypgm.bat arg1 arg2")

    2) I have seen in some resources that I can use the code (down below) to
    solve the "shell and wait" problem, but I cannot get it to compile. I put
    the two function declarations in the general declarations section following
    the variables and contants and get this compile error message:

    "Constants, fixed length strings, arrays, user-defined types and declare
    statements not allowed as public members of object modules."

    Any help would be appreciated...Guy

    And here's the code...
    Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess
    As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject
    As Long) As Long

    Private Sub ShellAndWait(ByVal strProg as String, ByVal lStyle As VbAppWinStyle)
    Dim ProcessId As Long
    Dim ProcessHandle As Long
    Const Access As Long = &H100000
    ProcessId = Shell(strProg, lStyle)
    Do
    ProcessHandle = OpenProcess(Access, False, ProcessId)
    If ProcessHandle <> 0 Then
    CloseHandle ProcessHandle
    End If
    DoEvents
    Loop Until ProcessHandle = 0
    End Sub


  2. #2
    Guy Guest

    Re: shell and wait


    I managed to find the answers for this one. In case you have the same problem
    (gee, they seem so simple once they're solved), the solutions are embedded
    below...Guy

    "Guy" <ghokanson@diamonddata.com> wrote:
    >
    >Hi,
    >
    >From my VB6 exe, I want to call another program and wait for it to return
    >before continuing. I am having (at least) two problems with this, they may
    >be related.
    >
    >1) When I use the shell command to call the other program, it leaves a DOS
    >command window open, but I need it to close on its own. Here's my command
    >syntax:
    >
    > RetVal = Shell("mypgm.bat arg1 arg2")


    Solution: (courtesy of http://support.microsoft.com/support.../Q138/8/07.asp

    Start the program using the MS-DOS command interpreter, Command.com, with
    the /C switch. This switch closes the window after the program quits. For
    example, to run a batch file, use the following command:
    Shell "command.com /c fred.bat"


    >
    >2) I have seen in some resources that I can use the code (down below) to
    >solve the "shell and wait" problem, but I cannot get it to compile. I put
    >the two function declarations in the general declarations section following
    >the variables and contants and get this compile error message:
    >
    >"Constants, fixed length strings, arrays, user-defined types and declare
    >statements not allowed as public members of object modules."
    >


    Solution: courtesy of
    http://msdn.microsoft.com/library/de...lprocedure.htm

    DLL procedures declared in standard modules are public by default and can
    be called from anywhere in your application. DLL procedures declared in any
    other type of module are private to that module, and you must identify them
    as such by preceding the declaration with the Private keyword.

    (I merely had to precede the declarations with "Private")


    >Any help would be appreciated...Guy
    >
    >And here's the code...
    >Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess
    >As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    >Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject
    >As Long) As Long
    >
    >Private Sub ShellAndWait(ByVal strProg as String, ByVal lStyle As VbAppWinStyle)
    > Dim ProcessId As Long
    > Dim ProcessHandle As Long
    > Const Access As Long = &H100000
    > ProcessId = Shell(strProg, lStyle)
    > Do
    > ProcessHandle = OpenProcess(Access, False, ProcessId)
    > If ProcessHandle <> 0 Then
    > CloseHandle ProcessHandle
    > End If
    > DoEvents
    > Loop Until ProcessHandle = 0
    >End Sub
    >



  3. #3
    Guy Guest

    Re: shell and wait


    I managed to find the answers for this one. In case you have the same problem
    (gee, they seem so simple once they're solved), the solutions are embedded
    below...Guy

    "Guy" <ghokanson@diamonddata.com> wrote:
    >
    >Hi,
    >
    >From my VB6 exe, I want to call another program and wait for it to return
    >before continuing. I am having (at least) two problems with this, they may
    >be related.
    >
    >1) When I use the shell command to call the other program, it leaves a DOS
    >command window open, but I need it to close on its own. Here's my command
    >syntax:
    >
    > RetVal = Shell("mypgm.bat arg1 arg2")


    Solution: (courtesy of http://support.microsoft.com/support.../Q138/8/07.asp

    Start the program using the MS-DOS command interpreter, Command.com, with
    the /C switch. This switch closes the window after the program quits. For
    example, to run a batch file, use the following command:
    Shell "command.com /c fred.bat"


    >
    >2) I have seen in some resources that I can use the code (down below) to
    >solve the "shell and wait" problem, but I cannot get it to compile. I put
    >the two function declarations in the general declarations section following
    >the variables and contants and get this compile error message:
    >
    >"Constants, fixed length strings, arrays, user-defined types and declare
    >statements not allowed as public members of object modules."
    >


    Solution: courtesy of
    http://msdn.microsoft.com/library/de...lprocedure.htm

    DLL procedures declared in standard modules are public by default and can
    be called from anywhere in your application. DLL procedures declared in any
    other type of module are private to that module, and you must identify them
    as such by preceding the declaration with the Private keyword.

    (I merely had to precede the declarations with "Private")


    >Any help would be appreciated...Guy
    >
    >And here's the code...
    >Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess
    >As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    >Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject
    >As Long) As Long
    >
    >Private Sub ShellAndWait(ByVal strProg as String, ByVal lStyle As VbAppWinStyle)
    > Dim ProcessId As Long
    > Dim ProcessHandle As Long
    > Const Access As Long = &H100000
    > ProcessId = Shell(strProg, lStyle)
    > Do
    > ProcessHandle = OpenProcess(Access, False, ProcessId)
    > If ProcessHandle <> 0 Then
    > CloseHandle ProcessHandle
    > End If
    > DoEvents
    > Loop Until ProcessHandle = 0
    >End Sub
    >



  4. #4
    michiel de bruijn Guest

    Re: shell and wait

    Hi Guy,

    > "Constants, fixed length strings, arrays, user-defined types and declare
    > statements not allowed as public members of object modules."


    Which is exactly your problem. Adding the Private keyword in front of each
    Declare will solve it.

    '//mdb



  5. #5
    michiel de bruijn Guest

    Re: shell and wait

    Hi Guy,

    > "Constants, fixed length strings, arrays, user-defined types and declare
    > statements not allowed as public members of object modules."


    Which is exactly your problem. Adding the Private keyword in front of each
    Declare will solve it.

    '//mdb



  6. #6
    Sergey Merzlikin Guest

    Re: shell and wait

    Hello!
    >
    > Solution: (courtesy of

    http://support.microsoft.com/support.../Q138/8/07.asp
    >
    > Start the program using the MS-DOS command interpreter, Command.com, with
    > the /C switch. This switch closes the window after the program quits. For
    > example, to run a batch file, use the following command:
    > Shell "command.com /c fred.bat"
    >


    This solution depends on contents of command.pif file in Windows or
    Windows\pif directory. Some users can have this file with very unusual
    settings (for example "Run full-screen") . To be sure your bat-file or any
    other DOS application would br launched with correct settings, always create
    PIF file and shell it instead of bat or com or exe file.

    Your ShellAndWait function is very time-consuming because it uses endless
    cycle. Actually you need to call OpenProcess once and then to use one of API
    wait functions.
    See here to learn how to use wait functions in Visual Basic:
    http://smsoft.chat.ru/en/vbwait.htm

    Sergey Merzlikin
    http://smsoft.chat.ru
    smsoft@chat.ru




  7. #7
    Sergey Merzlikin Guest

    Re: shell and wait

    Hello!
    >
    > Solution: (courtesy of

    http://support.microsoft.com/support.../Q138/8/07.asp
    >
    > Start the program using the MS-DOS command interpreter, Command.com, with
    > the /C switch. This switch closes the window after the program quits. For
    > example, to run a batch file, use the following command:
    > Shell "command.com /c fred.bat"
    >


    This solution depends on contents of command.pif file in Windows or
    Windows\pif directory. Some users can have this file with very unusual
    settings (for example "Run full-screen") . To be sure your bat-file or any
    other DOS application would br launched with correct settings, always create
    PIF file and shell it instead of bat or com or exe file.

    Your ShellAndWait function is very time-consuming because it uses endless
    cycle. Actually you need to call OpenProcess once and then to use one of API
    wait functions.
    See here to learn how to use wait functions in Visual Basic:
    http://smsoft.chat.ru/en/vbwait.htm

    Sergey Merzlikin
    http://smsoft.chat.ru
    smsoft@chat.ru




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center