Highlighted
Absent Member.
Absent Member.
3208 views

[archive] Stopping System Calls From WithIn

[Migrated content. Thread originally posted on 13 December 2006]

I need to know if there is a way to stop a CALL "C$SYSTEM" command from within cobol. I'm trying to create a communications server that will call batch scripts which in turn invoke a communications program. I need to be able to stop the communications program from with in the server application, so if a user starts a long communication process, they will be able to cancel / stop it from the screen application.

The way I have the Server application set up is it has a thread that is monitoring a message que file for commands to process. When it receives a command to process from a User, it performs that process (batch file) invoking the communications process. When the comm process is complete, it then changes the status of the message record so that the user application knows that it was complete and they can continue processing. While the communications is taking place the User has a screen with a timer indicating how long it has been taking, plus a cancel button that would allow them to stop the communication process.

I haven't tried anything yet, but was thinking of using a thread to start the comm process, then I could cancel the thread, but I don't know if the "C$SYSTEM" call would work that way. BTW, this is on a Windows System.

Thanks,
0 Likes
15 Replies
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

First of all, COBOL itself has no such mechanism, this is OS stuff.
C$SYSTEM will not make any difference if ran in a thread. I presume you have your reasons for why you want to run a system call, and that doing this is inevitable then.
In Windows, the "nice" way of stopping a process, is to send a message to it (WM_ENDSESSION) to make it shut down on its own. But I presume this system call of yours is running without desktop interaction? If so, there is probably no message handling either.
The only solution you have then, is to start the process using the API function CreateProcess, this will give you the process handle among a lot of other information.
With the process handle you can call the API function TerminateProcess. Note however; The function TerminateProcess is doing is exactly that, it will kill your process without questions, no matter if it is in the middle of a database update or whatever. So, besides involving CreateProcess which in itself is a bit complex, also this rough termination is something I would not recommend.
How about using the existance of a file as a metaphor? If your spawned process looked for a file someuniqenumber.terminate every 10 second or something?
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

Gisle,
Thanks for responding. To give you more information on what is going on here, the application that is being spawned with the C$SYSTEM call is a custom written program that provideds 3780 communications using NDP protocal to communicate to an NCR register. This communication session is call by invoking at batch script which copies some control files, then invokes the .exe program to perform the communications. The .exe program does have screen interaction in that the user can press the escape key to terminate the session, but since the server would be running on a different PC that would probably be in a different room I need another method for terminating the process. So I think the "nice" way that you mention would probably work. I guess what I would need now is how would you determine the session id of the program? I'm assuming that I would have to get a session Id list from the system and parse it for the .exe program, then use the (WM_ENDSESSION) to terminate it. Do you have any examples of how to do this?

Thanks,
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

Not a session id, but a process id. As I suggested, if you use the CreateProcess API function, you get this id. Otherwise you will have to parse the processes on the machine, checking the windowclass for each to find a match. If you are certain there will be only one such process at a time, that should be a reliable technique then.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

Ok, so far I've gotten the CreateProcess function to start a new process and I get both the Process handle and the Process ID from the Process_Information structure. The problem I'm having now is that I can't seem to get the GetExitCodeProcess function to work, so that I can check on the status of the running application. I also tried the TerminateProcess function and it would not terminate the process either. In both functions, it would return a false (0) condition, indicating that it failed. That is according to the SDK documentation. Do I need to try to set the security and access rights in order for these to work?
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

I think I figured it out, when I was calling the GetExitCodeProcess, I forgot to pass the handle as a value instead of a reference.

I changed it to:
call "GetExitCodeProcess" using
by value hProcess,
by reference lpExitCode
returning Call-R-N.

The way I was able to figure it out was by using the "GetLastError" function, it told me that it was an Invalid Handle.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

Good work!
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

Well, now I'm stuck again. I've got it starting the communication process without any problems, and I can call "GetExitCodeProcess" to determine that the application is still running, but when I try to call "TerminateProcess", the server PC completely locks up to the point that nothing will work. This seems to be if the program that was called is the .exe communication process. If it is a Batch file that was called, then it will terminate with out any problems, but it only terminates the Batch file and not the exe program that was called from the batch.

Any Ideas???
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

May I see your definitions of hProcess and lpExitCode? (declaration in working storage).
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

01 Process-Information.
05 hProcess
USAGE IS HANDLE.
05 hThread
USAGE IS HANDLE.
05 dwProcessId PIC 9(5)
USAGE IS COMP-N.
05 dwThreadId PIC 9(5)
USAGE IS COMP-N.
01 lpExitCode PIC 9(5)
USAGE IS COMP-N.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

Here is probably your problem. Change all of these to PIC X(4) COMP-N.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: [archive] Stopping System Calls From WithIn

Here is probably your problem. Change all of these to PIC X(4) COMP-N.


Are you saying that hProcess should also be PIC X(4) COMP-N? Or just dwProcessid, dwThreadid and lpExitCode?

Thanks,
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.