Solved

Reuse command prompt process

Posted on 2000-04-21
8
276 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
  • 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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2738512
it sits there after it finishes the entire loop or after just the first run??
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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 50 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now