PDA

View Full Version : Dereferencing


RobertVanA
03-24-2009, 05:51 PM
In Window programming it sometimes happens that one of the arguments
of the callback function acts as a pointer to a structure. In the winproc
we need access to this structure. In C language
this can be handled very easily, but with Fortran things become complicated.

An example in C-language:

LRESULT CALLBACK WndProc( , , , LPARAM lParam)
.
LPDRAWITEMSTRUCT pdis
.
pdis=(LPDRAWITEMSTRUCT) lParam

The last argument in the Callback function is the variable under consideration.
A structure is defined in the second line; in the third line the parameter lParam
is casted into the structure pdis.
Elements of the structure then can be referred to as:

pdis->hDC
pdis->rcItem

etc.

In FORTRAN there is, as far as FORTRAN 95 concerns and AFAIK no adequate
solution. A "trick" to handle such a problem in Fortran is as follows:
(obtained from a contribution of Dick Russell to the repository at the Lahey site)
the relevant lines in the Window procedures are:

INTEGER FUNCTION WndProc( , , , lParam)
USE winapi_definitions ! definitions
INTEGER,VALUE :: lParam
.
TYPE(DRAWITEMSTRUCT) :: dis,dis_function
.
dis = dis_function(val(lParam))
.
.
dis%hDC
dis%rcItem

A structure of the required type is defined (dis) and its value is obtained
by a call to dis_function with the value of lParam as argument.
The members of the structure dis are then referred to as given.
This function is specified as follows:


TYPE(DRAWITEMSTRUCT) function dis_function(hulp)
USE winapi_definitions
TYPE(DRAWITEMSTRUCT),INTENT(in) :: hulp ! inconsistent argument definition
dis_function = hulp !
RETURN
END

Needless to say that the actual argument is the value of a scalar and the
dummy argument is a structure! This clashes with the standard.
With Lahey Fortran the problem is handled as given here, but don't compile
these parts in one file and switch off the checking! Otherwise the compiler
complains and compilation breaks off.

Does anybody know a way to handle this problem so that it is in full
accordance with the Fortran 95 standard? And further, is there a way to
do it such a way that two-way communication is possible? (The
method that is given here cannot set members of the structure that
becomes available to the calling program.


Thanks!

tzeis
04-01-2009, 07:14 PM
I doubt if you will be able to discover some standard conforming way to dereference a pointer that can point to _anything_. The solution in hand is probably the best you will find in pure Fortran. In theory, you should be able to turn interface checking off for the casting function, but leave it on for other procedures, and still get the diagnostic benefit.

RobertVanA
04-02-2009, 08:46 PM
Thank for the reply; it seems, as you said, that this is the only workable solution with the present language definition! About checking: I only do not use the argument checking (chk, a) in the called functon; the others still work fine!