Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Terminating a process with WinAPI functions

Status
Not open for further replies.

mbru

Programmer
Mar 12, 2001
8
0
0
US
We are using VFP for both front and back end of our database. We've got about 20 remote locations that each have a server and 6-12+ workstations. Our central office server maintains master tables which are updated from data collected at the remote locations. We pass SQL commands in text files rather than run VFP from the remote locations against the master tables.

A program running at the central office starts a separate process for each of the outlying locations that polls the remote locations and moves the files when found. The program then executes the SQL commands found in the text files. The polling process is a COM server started with the CREATEOBJECT() command. The program is designed so that if it sees that the network connection to a remote location has been lost, it releases the polling object, then restarts it.

The problem is that, when the network connection is down, the object cannot always be released, so we have a "Cannot quit Visual FoxPro" error message. I haven't found an error number for this error, so I have been unable to trap for the error before starting another instance of the polling process. This has lead to numerous instances of a process running for a single remote location, to the point where we've had system resource problems. The aim is to have a "self-healing" process that doesn't need baby-sitting and we are close, but not there yet.

I basically have two questions:

(1) Can the "Cannot quit Visual Foxpro" error be trapped? If so, how?

(2) How can I programatically terminate the stalled polling process? We get and store each processid by using the Win32 api GetCurrentProcessId function when the process is started with CreateObject(), but the TerminateProcess function requires that I have the process handle. I don't see how to get the process handle unless I start my polling process with the CreateProcess function. Is this correct, or is there some way to get the process handle if you know the processid? Also, the TerminateProcess function requires an exitcode. Can anyone tell me what value goes in the exitcode?

Thanks for any assistance.
 
FWIW, here's what I am doing to terminate a process.

This code runs in the COM server object:

* Identify the process id for the process
DECLARE INTEGER GetCurrentProcessId IN kernel32
lnPID = GetCurrentProcessId()

The process ID (lnPID) is stored in a table for use by the main program. The following code runs in the stoppoll method of the main program:

* Get the process handle
DECLARE INTEGER OpenProcess IN kernel32 INTEGER, INTEGER, INTEGER
lnPH = OpenProcess(2035711, 0, lnPID)

* Get the exit code
lnExitCode = 0
DECLARE LONG GetExitCodeProcess IN kernel32 LONG, LONG @
=GetExitCodeProcess(lnPH, @lnExitCode)

* Terminate the process
DECLARE TerminateProcess IN kernel32 INTEGER, INTEGER
=TerminateProcess(lnPH, lnExitCode)

* Close the process handle
DECLARE INTEGER CloseHandle IN kernel32 INTEGER
=CloseHandle(lnPH)

* Clear the DLLS opened in the DECLARE statements
CLEAR DLLS

It seems to be working, but if anybody sees problems in this code, I'd appreciate a heads up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top