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

Passing function/subroutine addresses

dbruceg
Beginner
1,028 Views

I suspect this will ultimately be revealed as a stupid question, but I can't seem to figure it out. Consider the following:

external ErrorExit

integer(4) hErrorExit

hErrorExit = %loc(ErrorExit)! address of routine ErrorExit

...

call ErrorExitLocal (%val(hErrorExit))! tell ErrorExitLocal who to call

...

subroutine ErrorExitLocal (ErrorExitSub)

external ErrorExitSub

call ErrorExitSub! actually calls ErrorExit

...

The idea here is to be able to pass the address of a main-programerror routine into a DLL, such that different programs using the same DLL can handle errors in their own ways.

VF9.1 doesn't like this construct if it checks routine interfaces, suggesting that I need an interface block, but I can't figure out what that interface block should look like. Anybody have a hint?

Bruce

0 Kudos
7 Replies
Steven_L_Intel1
Employee
1,028 Views
You're making it more complicated than is necessary. This will work:

call ErrorExitLocal(ErrorExit)
0 Kudos
dbruceg
Beginner
1,028 Views
Your suggestion requires thatthe reference to ErrorExit be resolved at the time of DLL linkage. That's what I'm trying to avoid. I want to tell the DLL who to call at run time.
0 Kudos
dbruceg
Beginner
1,028 Views
This is just a test. It looks like a "Quick reply" does not change the name in "Last Post by"
0 Kudos
Steven_L_Intel1
Employee
1,028 Views
Sorry, I had understood that the first call was from the main program. Ok, you want to select at run-time the routine to call. You can do it this way: Leave the caller the way you have it except remove the %VAL. Make the called routine look like this:

subroutine ErrorExitLocal (PErrorExitSub)

external ErrorExitSub
pointer (PErrorExitSub, ErrorExitSub)

call ErrorExitSub! actually calls ErrorExit




0 Kudos
dbruceg
Beginner
1,028 Views

That's too clean!

Plus, it works. Thank you very much

Bruce

0 Kudos
Steven_L_Intel1
Employee
1,028 Views
I suggest the following change in the caller: Change:

integer(4) hErrorExit

to:

integer(int_ptr_kind()) hErrorExit

This way it will still work under 64-bits.
0 Kudos
dbruceg
Beginner
1,028 Views
Thanks. Iwish that was the only problem I'll have to face when I raise this thing to 64 bits. - Bruce
0 Kudos
Reply