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

x
?
Solved

Reuse command prompt process

Posted on 2000-04-21
8
Medium Priority
?
296 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
Industry Leaders: 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!

 
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

Industry Leaders: 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!

Question has a verified solution.

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

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

636 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