Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

ShellExecute and Wait for response

Posted on 1998-07-10
12
Medium Priority
?
564 Views
Last Modified: 2012-05-04
The steps that I am taking are the following:

1. Execute something using the Shell command under vbHide, which returns a program's task id.

2. Wish to monitor the task id and not continue until that task id has completed.

I want to us the appropriate API calls in a while loop.

Keep in mind I know the command line to MAP a drive.

I'm sure this is easily done with a shell command, but what the main purpose of this is to MAP and Network drive (we know what the computer name to network is) blind to the user, then UNMAP it when we are done with the task which we have started.  

If there is a better or easier way without shell command I'm up for options.

Sorry I can offer more points I have been too generous in the past and only have 101 total points left.  Don't know why but it also said I have 151 Point available, isn't that backwards?

Thanx,
Chizl
0
Comment
Question by:Chizl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 9

Expert Comment

by:Dalin
ID: 1465457
Chizl,
See if this works for you.
Regards
Dalin


'Declaration
Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type              

Private Declare Function WaitForSingleObject Lib _
"kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds _
As Long) As Long

Declare Function CreateProcessA Lib "kernel32" _
(ByVal lpApplicationName As Long, ByVal lpCommandLine As _
String, ByVal lpProcessAttributes As Long, ByVal _
lpThreadAttributes As Long, ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, lpStartupInfo As _
STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) _
As Long              

Declare Function CloseHandle Lib "kernel32" (ByVal hObject _
As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
             

'Prodedure

'This is the code that can be called to execute an external program.  
'Simply copy this code into the code of your project.  To run an external
'program, call ExecCmd(program) from anywhere in your code.

Public Sub ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO

' Initialize the STARTUPINFO structure:
start.cb = Len(start)

' Start the shelled application:
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
ret& = CloseHandle(proc.hProcess)
End Sub              
0
 
LVL 4

Author Comment

by:Chizl
ID: 1465458
It works perfect except one thing.  It is visible to the user.  We are running a dos command line from Shell or ShellExecute, but this would work great if there is a way of keeping the dos window from popping up.
0
 
LVL 4

Author Comment

by:Chizl
ID: 1465459
Also I see that running like this If I keep the DOS window then I can't kill it within my app.  I don't want this because if I wanted to use the window then I could use FindWindow to get when the dos app is dead and then close it.   If I can get this invisible, vbHide, then I could most defantly use it.  Thanx for the good and close example of what I need.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Author Comment

by:Chizl
ID: 1465460
I have been playing with the:

start.wShowWindow = 0 up to start.wShowWindow = 10 and thought this might have something to do with hidding the DOS Window.  All I see so far is everyone of them do the exact same thing, NOTHING!  Any clues?
0
 
LVL 7

Expert Comment

by:kamall
ID: 1465461
After starting the process, get the window handle and hide it using the SW_HIDE.

Public Const SW_HIDE = 0
Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Hope that helps.
0
 
LVL 4

Author Comment

by:Chizl
ID: 1465462
Sorry!

The problem there is that the, "CreateProcessA" doesn't pass back a handle.  I could use the FindWindow to get the handle, but that is where I don't want to go.  I don't want any windows, even for a few seconds.  Even if the window was there shortly before I Hide it, it would still be in memory at a finished status making the process never to end.

Thanx for the help though!
0
 
LVL 4

Expert Comment

by:yowkee
ID: 1465463
Chizl,

   Is this the function you want (I put code in a form module) :

-----
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const PROCESS_QUERY_INFORMATION = &H400
Const INFINITE = &HFFFF

Public Sub ShellAndWait(ByVal sTask As String)

    Dim lTaskID As Long
    Dim hTask As Long
    Dim lRtn As Long
   
    lTaskID = Shell(sTask, vbHide)
    If lTaskID <> 0 Then
        ' Use task id to obtain the process handle
        hTask = OpenProcess(PROCESS_QUERY_INFORMATION, False, lTaskID)
        ' Wait till the task finish
        lRtn = WaitForSingleObject(hTask, INFINITE)
        ' Close the process handle
        CloseHandle hTask
    End If
   
End Sub
-----

Hope it helps.

0
 
LVL 5

Expert Comment

by:yronnen
ID: 1465464
Why not changing the appropriate values in the registry, this way you will not have to create another process, and it will be completly transparent
0
 
LVL 4

Author Comment

by:Chizl
ID: 1465465
Well the hidding is what I want which I could already do that.  You are hidding the window, but the process is still open in memory and isn't closing when its finished.  So this makes the project lock up and I now have to alt-ctrl-del to get it out of memory.   A little of everything here is what I want, but making it all work correctly isn't easy.

>>Why not changing the appropriate values in the
>>registry, this way you will not have to create
>>another process, and it will be completly
>>transparent.

????? What do you mean?

0
 
LVL 7

Accepted Solution

by:
tward earned 400 total points
ID: 1465466
This was tested and does work:

Private Const INFINITE = -1&
Private Const NORMAL_PRIORITY_CLASS = &H20&

Public Const SW_HIDE = 0
Public Const STARTF_USESHOWWINDOW = &H1

Private Type STARTUPINFO

       cb As Long
       lpReserved As String
       lpDesktop As String
       lpTitle As String
       dwX As Long
       dwY As Long
       dwXSize As Long
       dwYSize As Long
       dwXCountChars As Long
       dwYCountChars As Long
       dwFillAttribute As Long
       dwFlags As Long
       wShowWindow As Integer
       cbReserved2 As Integer
       lpReserved2 As Long
       hStdInput As Long
       hStdOutput As Long
       hStdError As Long

End Type

Private Type PROCESS_INFORMATION
       
       hProcess As Long
       hThread As Long
       dwProcessID As Long
       dwThreadID As Long

End Type

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Sub msiShellAndWait(ByVal CommandLine As String)

  Dim ReturnValue As Long
  Dim Start As STARTUPINFO
  Dim Process As PROCESS_INFORMATION

  ' Initialize the STARTUPINFO structure:
  Start.cb = Len(Start)

  Start.dwFlags = STARTF_USESHOWWINDOW

  Start.wShowWindow = SW_HIDE

  ' Start the shelled application:
  ReturnValue = CreateProcessA(0&, CommandLine, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, Start, Process)

  ' Wait for the shelled application to finish:
  ReturnValue = WaitForSingleObject(Process.hProcess, INFINITE)
  ReturnValue = CloseHandle(Process.hProcess)
 
End Sub
0
 
LVL 7

Expert Comment

by:tward
ID: 1465467
Note these two lines, these are what hides the window:

  Start.dwFlags = STARTF_USESHOWWINDOW

  Start.wShowWindow = SW_HIDE
0
 
LVL 4

Author Comment

by:Chizl
ID: 1465468
Perfect!  Thanx much!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

618 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