Highlighted
Absent Member.
Absent Member.
1422 views

Changing priority of a running process from a AcuCobol program

[Migrated content. Thread originally posted on 17 January 2012]

Hi 🙂
Have anyone tried to change the priority of a running process from within AcuCobol program ? I've read that it can be done from VB with a call to SetPriorityClass, but I would like to do the same call from within AcuCobol.
Any help would be appreciated.
0 Likes
4 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Changing priority of a running process from a AcuCobol program

This should be a fairly simple call to the Win32 API SetPriorityClass function. You need a handle to the process you want to set the priority of. If you want to set the priority of the current COBOL program, you can call GetProcess.

There is good documentation for calling Win32 API functions in the extend documentation set. It is probably best if I don't try to repeat that here!
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Changing priority of a running process from a AcuCobol program

Thanks for your input, but I can't seem to make it work.
My code looks like this :

IDENTIFICATION DIVISION.
PROGRAM-ID. SETPRIORITY.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.

01 WRK-FIELDS.
02 WRK-NUM-FIELDS.
03 SYS-PROCESS-ID PIC 9(7).
03 SYS-PRIORITY PIC 9(9) COMP-5.
03 WIN-PID PIC 9(9) COMP-5.
03 WIN-PID-HANDLE HANDLE.
03 WIN-RESULT PIC 9(9) COMP-5.

PROCEDURE DIVISION.
MAIN SECTION.
MAIN-ENTRY.

CALL "C$GETPID" GIVING SYS-PROCESS-ID.

SET ENVIRONMENT "DLL-CONVENTION" TO "1".

CALL "AdvAPI32.DLL".
CALL "Kernel32.DLL".

MOVE ZERO TO WIN-PID-HANDLE.
MOVE ZERO TO WIN-RESULT.

CALL "GetCurrentProcess" USING WIN-PID-HANDLE
GIVING WIN-RESULT.

MOVE SYS-PROCESS-ID TO WIN-PID-HANDLE.
MOVE 31 TO SYS-PRIORITY.
MOVE ZERO TO WIN-RESULT.

CALL "SetPriorityClass" USING WIN-PID-HANDLE
SYS-PRIORITY
GIVING WIN-RESULT.

CANCEL "AdvAPI32.DLL".
CANCEL "Kernel32.DLL".

SET ENVIRONMENT "DLL-CONVENTION" TO "0".

MAIN-EXIT.

STOP RUN.
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Changing priority of a running process from a AcuCobol program

A couple of things.

First, SetPriorityClass takes a handle to the process, not the process-id. So calling C$GETPID is not necessary.

Next, GetCurrentProcess returns the process handle. It takes no arguments.

Next, SetPriorityClass doesn't set a particular priority, it sets a priority class. As such, the valid values for the second parameter are from a very limited set. And 31 is not one of those valid values.

Also, SetPriorityClass should be passed ints, so the arguments should be passed by value.

I modified the program to look like this, and when I run it in debug, and watch in Task Manager, I can see the priority change to High (which is what x#80 corresponds to).

       IDENTIFICATION DIVISION.
       PROGRAM-ID. SETPRIORITY.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       DATA DIVISION.
       FILE SECTION.
       WORKING-STORAGE SECTION.

       01 WRK-FIELDS.
       02 WRK-NUM-FIELDS.
       03 SYS-PRIORITY PIC 9(9) COMP-5.
       03 WIN-PID-HANDLE HANDLE.
       03 WIN-RESULT PIC S9(9) COMP-5.

       PROCEDURE DIVISION.
       MAIN SECTION.
       MAIN-ENTRY.

           SET ENVIRONMENT "DLL-CONVENTION" TO "1".

           CALL "AdvAPI32.DLL".
           CALL "Kernel32.DLL".

           MOVE ZERO TO WIN-PID-HANDLE.
           MOVE ZERO TO WIN-RESULT.

           CALL "GetCurrentProcess" GIVING WIN-PID-HANDLE.

           MOVE x#80 TO SYS-PRIORITY.
           MOVE ZERO TO WIN-RESULT.

           CALL "SetPriorityClass" USING by value
                 WIN-PID-HANDLE SYS-PRIORITY
           GIVING WIN-RESULT.

           CANCEL "AdvAPI32.DLL".
           CANCEL "Kernel32.DLL".

           SET ENVIRONMENT "DLL-CONVENTION" TO "0".

       MAIN-EXIT.

           STOP RUN.


I hope this helps.
0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Changing priority of a running process from a AcuCobol program

Thanks a lot - that did help 🙂

Regards
Steen
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.