?
Solved

Reuse command prompt process

Posted on 2000-04-21
8
Medium Priority
?
294 Views
Last Modified: 2010-05-18
A shelled Command Prompt cmd is executing many times within a loop.  Each iteration appears to create a new process and I am running out of memory.  When I go to Task Mgr, I see hundreds of cmd.exe processes.  How can I execute cmd to reuse the same Command Prompt?  Is there a switch or parameter?
(I currently use switch /k to hide window.) Sample code:

for z 0 to 500
 a = "c:\winnt\system32\cmd.exe /k" & _
     " net user /domain " & uid(z) & _
     " >c:\temp\user.txt"
 ' do some processing
loop
0
Comment
Question by:jalymo
[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
  • 5
  • 3
8 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2738164
i think maybe you need to wait until it finishes processing the previous instance before proceeding.   it will keep creating new process because it isnt done with the last one and the loop will run much faster than your process can be completed thus causing overflow
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2738180
see if this helps you out:

Private Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, _
     ByVal bInheritHandle As Long, _
     ByVal dwProcessId As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" _
    (ByVal hProcess As Long, lpExitCode As Long) As Long

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

Const PROCESS_QUERY_INFORMATION = &H400
Const STATUS_PENDING = &H103&


Function YourFunc()
for z 0 to 500
 a = "c:\winnt\system32\cmd.exe /k" & _
     " net user /domain " & uid(z) & _
     " >c:\temp\user.txt"
 RunShell a
loop
End Function


Private Sub RunShell (cmdline As String)

    Dim hProcess As Long
    Dim ProcessId As Long
    Dim exitCode As Long

    ProcessId = Shell(cmdline, 1)
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)

    Do

        Call GetExitCodeProcess(hProcess, exitCode)
        DoEvents
   
    Loop While exitCode = STATUS_PENDING

    Call CloseHandle(hProcess)

    MsgBox "The shelled process " & cmdline & " has ended."

End Sub







0
 

Author Comment

by:jalymo
ID: 2738375
AzraSound,

In my sample code, I forgot to include the following shell cmd that differs from yours:

shellx = Shell(cmdline, 0)
   
In yours, you have a 1 as a parameter:

ProcessId = Shell(cmdline, 1)
   

When I run yours, the DOS window opens and sits there until I enter an "exit" cmd; it then comes back and pgm continues executing.

The "0" parameter seems to hide and close the DOS window.  When I tested with your code using the "0" parameter, the pgm gets stuck looping in the "Do .. Loop While" requiring me to break out.

I need the cmd prompt process to be hidden and require no manual intervention to close.

Thanks much for your info and help
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 28

Expert Comment

by:AzraSound
ID: 2738512
it sits there after it finishes the entire loop or after just the first run??
0
 

Author Comment

by:jalymo
ID: 2738580
It sits there after the first run.

The following is your sample, modified to feed a few user-ids into the shell cmd. When I run it, my debug.print shows the first user-id "LMX" and I sit at the DOS window.  When I enter "exit" at the Dos prompt, it does the next user-id and sits ... I enter "exit" for each user-id processed.

Function YourFunc()
Dim a As String
Dim uid(4) As String
Dim z As Integer

For z = 0 To 5
 If z = 0 Then uid(z) = "LMX"
 If z = 1 Then uid(z) = "QAX"
 If z = 2 Then uid(z) = "NMX"
 If z = 3 Then uid(z) = "GKC"
 If z = 4 Then uid(z) = "SLD"
Next

For z = 0 To 4
Debug.Print uid(z)
 a = "c:\winnt\system32\cmd.exe /k" & _
     " net user /domain " & uid(z) & _
     " >c:\temp\user.txt"
 RunShell a
Next

End Function


0
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
ID: 2738592
i think maybe we have two choices.  we can close the dos window using API (findwindow and sendmessage) or try maybe to send keys automatically to processs the exit.  for using api see this article:

http://www.vbsquare.com/tips/tip235.html

if you want to try sendkeys,

SendKeys "exit"
SendKeys "{ENTER}"
0
 

Author Comment

by:jalymo
ID: 2738630
I am going to take the info you have given me and work on it.

Thanks!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2738637
you are very welcome   good luck!

if you have further questions go ahead and post in this thread and i will respond
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

770 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