[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

Reuse command prompt process

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
jalymo
Asked:
jalymo
  • 5
  • 3
1 Solution
 
AzraSoundCommented:
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
 
AzraSoundCommented:
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
 
jalymoAuthor Commented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AzraSoundCommented:
it sits there after it finishes the entire loop or after just the first run??
0
 
jalymoAuthor Commented:
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
 
AzraSoundCommented:
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
 
jalymoAuthor Commented:
I am going to take the info you have given me and work on it.

Thanks!
0
 
AzraSoundCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now