- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
6 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
Any particular reason you don't use DFWIN or KERNEL32 and get the correct interface automatically?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It's not there. PSAPI isn't in the list of provided modules.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page