I'm trying to use the FormatMessage function to obtain the error message when windows functions fail. The functions I'm particularly interested in are CopyFile and CreateProcess. The follow subroutine is my first attempt to get the error message but so far I've had no joy.
subroutine w32_CheckError
!**********************************************************
! Check for Windows W32 Error And Display Message
!**********************************************************
use dfwin, NULLPTR => NULL
use kernel32
implicit none
! Local Variables
character*255 :: string
integer :: nchar
nchar = FormatMessage(ior(FORMAT_MESSAGE_ALLOCATE_BUFFER,ior(FORMAT_MESSAGE_FROM_SYSTEM, &
FORMAT_MESSAGE_IGNORE_INSERTS)), &
NULL, &
GetLastError(), &
int(MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),DWORD), & ! Default language
! LANG_NEUTRAL, & ! Default language
string, &
255, &
NULL)
! Display the string.
if(nchar.gt.0) then
call grp_message('Windows Error: '//trim(string(1:nchar)))
endif
return
end subroutine
When I try this the contents of string is garbled. Has anyone successfully used this in a FORTRAN context that can tell me what I'm doing wrong.
Thanks in advance.
Steve




