Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

EnumProcesses

bearoflittlebrain_ol
1,372 Views
I am trying to get the psapi.dll WinAPI EnumProcesses working, but as usual am having trouble with the interface. Would someone please tell me the bug(s) in the following program?
Regards
Alan
Code:
program TestEnumProcesses
!-------------------------------------------------------------------------------
use dfwinty
use kernel32
use user32
implicit none
integer*4 :: pProcessIds = 0          ! Pointer to output array.
integer*4 :: cb                       ! Size of the output array, bytes.
integer*4 :: nProcesses               ! No. of processes.
integer*4 :: ProcessIds(1024)         ! Output array.
integer*4 :: pBytesReturned  = 0      ! No. of bytes returned.
integer*4, save :: hdll      = 0
integer*4, save :: pEnumProc = 0
character*5     :: ErrStr
!
pointer(pProcessIds, ProcessIds)
pointer(pEnumProc,   EnumProcesses)
interface
  logical*4 function EnumProcesses(pProcessIds, cb, pBytesReturned)
  implicit none
!DEC$ ATTRIBUTES STDCALL, ALIAS:'_EnumProcesses' :: EnumProcesses
  integer*4, intent(out) :: pProcessIds        ! Pointer to output array.
  integer*4, intent(in)  :: cb                 ! Size of the ouput array, bytes.
  integer*4, intent(out) :: pBytesReturned     ! No. of bytes returned.
  end function EnumProcesses
end interface
!  
! Load the dll if not loaded.
!
if (pEnumProc == 0) then
  hdll = LoadLibrary('psapi.dll'C)
  if (hdll == 0) then
    hdll = MessageBox(NULL, 'Psapi.dll not loaded'C, 'Dll loading error'C,     &
                      MB_OK .OR. MB_ICONSTOP)
    stop
  end if
  pEnumProc = GetProcAddress(hdll, 'EnumProcesses'C)
  if (pEnumProc == 0) then
    hdll = MessageBox(NULL, 'EnumProcesses not found in Psapi.dll'C,           &
                     'Finding error'C, MB_OK .OR. MB_ICONSTOP)
    stop
  end if
end if
cb = sizeof(ProcessIds)
if (EnumProcesses(pProcessIds, cb, pBytesReturned)) then
  nProcesses = pBytesReturned/sizeof(DWORD)
else
  nProcesses = GetLastError()                  ! Get last error number.
  ! Gives System Error No. 998 - Invalid access to memory location.
  write(ErrStr, '(I0)') nProcesses
  hdll = MessageBox(NULL, 'System Error No. '//ErrStr//''C, 'System Error'C,   &
                    MB_OK .OR. MB_ICONSTOP)
end if
end program TestEnumProcesses


0 Kudos
6 Replies
Steven_L_Intel1
Employee
1,372 Views
I see several errors:

1. Remove the _ on the ALIAS and add DECORATE.
2. The return type should be INTEGER*4, not LOGICAL*4, and you should test the result as an integer, not as a logical. Fortran LOGICAL is not C BOOL.
3. Make the first argument an array DIMENSION(*) and pass the array directly, not a pointer.
4. Add !DEC$ ATTRIBUTES REFERENCE for the BytesReturned argument
0 Kudos
Jugoslav_Dujic
Valued Contributor II
1,372 Views
Hi Bear, haven't heard from you for a long time...

Any particular reason you don't use DFWIN or KERNEL32 and get the correct interface automatically?
0 Kudos
Steven_L_Intel1
Employee
1,372 Views
It's not there. PSAPI isn't in the list of provided modules.
0 Kudos
bearoflittlebrain_ol
1,372 Views

Steve,

Thank you very much - it worked perfectly. I will try to get correct interfaces for some of the other useful functions in psapi.dll, but knowing my deteriorating grey matter, I will be back to ask you about some of them.

Jugoslav,

Thanks for the welcome back - I had a close encounter with the Grim Reaper, but am now in fine fettle with a spanking new pacemaker.

Regards

Alan

0 Kudos
kompute
Beginner
1,372 Views

Dear Steve,

I wastrying to compilethe codeaccording toyour reply. There was this point 3 which I don't really grasp. Could you illustrate it on the code attached. Why would we need to make the first agument an array and how do we predetermine the array size?Thanks.

Warmest regards!

0 Kudos
Steven_L_Intel1
Employee
1,372 Views
You want this:

integer*4, dimension(*) :: ProcessIds ! Output array.

and then just pass the array, How do you determine the size of the array? Dunno - the MSDN examples always pick a large fixed size, such as 1024.

I'm not sure why you're dynamically activating psapi.dll. It isn't necessary.

See here for a full PSAPI module.
0 Kudos
Reply