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
Solved

Close a DOS program without a windows pop-up "CANNOT END PROGRAM"

Posted on 2013-01-28
12
761 Views
Last Modified: 2013-02-12
Hi,
I'm using Visual Foxpro 9 vfp9 to control an old dos apllication AccPac Plus for DOS,
Sometimes the users leave an active application opened at night,
I found a way to track this behavior and to stop the program,
But I always have a message from Windows "CANNOT END PROGRAM" that requiers a manual intervention.
Is there a way to bypass this message or reply it programatically using VFP9?
The code to colse the program is:
PARAMETERS prog_to_stop
  oWMI = GETOBJECT('winmgmts://')
    cQuery = "select * from win32_process where name='"+prog_to_stop+"'"
    oResult = oWMI.ExecQuery(cQuery)
 x = 0
   FOR EACH oProcess IN oResult
      x = x + 1        
     oProcess.Terminate(0)
     NEXT
IF x>0
     MESSAGEBOX(STR(x,5,0)+' programs were stopped')
ELSE
    IF ISOPENED(prog_to_stop)
        TK ='!TASKKILL /FI "WINDOWTITLE EQ ' + prog_to_stop + '"'
        &TK
    ELSE       
       MESSAGEBOX('No such programs were found active to be stopped')
   ENDIF       
ENDIF
0
Comment
Question by:Admigha
  • 5
  • 5
  • 2
12 Comments
 
LVL 16

Assisted Solution

by:choward16980
choward16980 earned 125 total points
ID: 38828877
Change
!TASKKILL /FI
!TASKKILL /F /FI

That will force close the window
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 38829267
If you only have the problem in the branch using TASKKILL: Find a way to better specify the process to terminate, so the WMI query finds it.

I can say from experience oProcess.Terminate(0) works well. And there is more than just the name you can query to find a process, see http://msdn.microsoft.com/en-us/library/windows/desktop/aa394372(v=vs.85).aspx

For example, if you know how you started the DOS process, you can find it by the CommandLine property of the process. Or by it's path or description.

Bye, Olaf.
0
 

Author Comment

by:Admigha
ID: 38829873
Thank you Olaf for replying,
this does not work and I tried it before (!TASKKILL /F /FI) acts like aswering cancel to the warning message, and what is strange in this command, that it removes the PID but the program still active.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 375 total points
ID: 38830136
Well, there is the DOS window and the application you run in it, isn't it so? That's two PIDs.
But why do you address me, choward16980 gave the hint about the /F switch.

I just see you only get into that branch, if the oResult from WMI has no processes. So have you debugged the code, single stepped through it to see what actually executes?

Bye, Olaf.
0
 

Author Comment

by:Admigha
ID: 38835489
Thank you Olaf,
Yes you are right I had to address to choward16980 for the /F switch.

What I'm doing is run the shortcout that itself points to a batch file xxx.bat and this btach runs the yyy.exe, when I kill the task it kills the DOS window and not the yyy.exe

can somebody tell me how to end the all tasks produced by running the shortcut?

knowing that I don't want to use the task manager, because it is an automated procedure.

Thanks everybody!
0
 

Accepted Solution

by:
Admigha earned 0 total points
ID: 38836018
Hi Everybody,
I found an Indirect solution to close Accpac by TASKKILL /F
1- to find the IM name of Accpac in taskmanager id did the following:
    a- tasklist  > c:\temp\taslist1.txt
    b- I run the sohortcut of Accpac
    c- tasklist  > c:\temp\taslist2.txt
    d- I compared the 2 lists taslist1 vs taslist2
I found that accpac has this  name NTVDM.EXE
2- i RUN THE AUTOPROGRAM to close the Accpac including this command line:
    TASKKILL /F /im ntvdm.exe >C:\temp\result.TXT
3- I included the C:\temp\result.TXT to check the result by the program
0
 
LVL 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 375 total points
ID: 38836253
You can also see a full task list if you run your WMI query without the WHER-Clause, so just query "select * from win32_process".

But as you know you want to stop the NTVDM.EXE pass that in as the prog_to_stop.

What did you pass in before? The name of a BAT/CMD file?

Bye, Olaf.

Edit: If you would shellexecute a DOC file it would also trigger Word.EXE to run, and not the DOC file. So you would stop Word and not the DOC.

A BAT or CMD file cause a CMD.EXE to run and the commands inside the bat cause NTVDM.EXE to run, obviously.

It seems you just tried to kill the wrong thing.
0
 
LVL 16

Expert Comment

by:choward16980
ID: 38836817
FWIW,

I have a program that uses the NTVDM.exe process along with crystal reports.  I use the attached script (kill.txt) with the process.exe download from the url below and it works like a charm.  The script is also terminal server friendly so it only kills the process belonging to the user who runs it:


http://retired.beyondlogic.org/solutions/processutil/process203.zip
kill.txt
0
 
LVL 29

Assisted Solution

by:Olaf Doschke
Olaf Doschke earned 375 total points
ID: 38837086
Well, thanks for sharing.

I think I'll stay with WMI. The win32_process class also gives you the Method GetOwner:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394372(v=vs.85).aspx

See http://www.foxite.com/archives/know-what-other-exes-are-runing-0000136534.htm

You can pretty much get all infos that also come from the Task Manager and do anything programmatically.

Bye, Olaf.
0
 

Author Comment

by:Admigha
ID: 38864230
Thaks for help.
Antoine Lama.
0
 

Author Closing Comment

by:Admigha
ID: 38879447
I did not find out how to close Accpac, the btach file is named appac.bat that runs a program plus.ese but the task manager shows NTVDM.EXE
0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 38880041
You'll never find a .bat listed as task, the file extension .bat starts cmd.exe or command.com and these are in the task list. Also ESE files, as far as I googled, are database files (extensible storage engine) and likely start something else when you "execute" them, like mdb or accdb causes access.exe to start, this ESE extension might be associated to NTVDM.EXE

There are only very few executable file types, COM, SYS, DLL, EXE, nothing else. You will have to know what really is the process running, but you can also reidentify it by the CommandLine property of a win32_process.

For example open the windows start menu, in the search box type [cmd.exe /k help] and execute that by hitting ENTER. A Dos (cmd.exe) window should start and run the help command. Now open the task manager, In the processes tab it only shows cmd.exe , but if you add the colum "commandline" you can see your original call of cmd.exe including the /k help.

If you execute a bat file something similar happens, the task manager will show "cmd /c "path and file name of bat file.bat" in the command line column, and that's also what you get from a win32_process object. Your starting of a bat file causes execution of cmd.exe with a /c option and the bat file as parameter.

I hope that adds a bit more understanding. You'll never find a .bat or .cmd in the task list, like you don't find a .doc or .pdf there, these are not the processes running.

Bye, Olaf.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

If you have done a reformat of your hard drive and proceeded to do a successful Windows XP installation, you may notice that a choice between two operating systems when you start up the machine. Here is how to get rid of this: Click Start Clic…
The canonical version of this article is on my web site here: http://iconoun.com/articles/collisions/ A companion presentation is available here: http://iconoun.com/articles/collisions/Unicode_Presentation.pdf
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

809 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