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

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        
IF x>0
     MESSAGEBOX(STR(x,5,0)+' programs were stopped')
    IF ISOPENED(prog_to_stop)
        TK ='!TASKKILL /FI "WINDOWTITLE EQ ' + prog_to_stop + '"'
       MESSAGEBOX('No such programs were found active to be stopped')
Who is Participating?
AdmighaConnect With a Mentor Author Commented:
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
Chris HConnect With a Mentor Infrastructure ManagerCommented:

That will force close the window
Olaf DoschkeSoftware DeveloperCommented:
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

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.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

AdmighaAuthor Commented:
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.
Olaf DoschkeConnect With a Mentor Software DeveloperCommented:
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.
AdmighaAuthor Commented:
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!
Olaf DoschkeConnect With a Mentor Software DeveloperCommented:
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.
Chris HInfrastructure ManagerCommented:

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:
Olaf DoschkeConnect With a Mentor Software DeveloperCommented:
Well, thanks for sharing.

I think I'll stay with WMI. The win32_process class also gives you the Method GetOwner:


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

Bye, Olaf.
AdmighaAuthor Commented:
Thaks for help.
Antoine Lama.
AdmighaAuthor Commented:
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
Olaf DoschkeSoftware DeveloperCommented:
You'll never find a .bat listed as task, the file extension .bat starts cmd.exe or 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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.