- Home
- User Articles
- How To Write ZEMAX Extensions in FORTRAN
- Home
- Programming ZEMAX
- Extensions
- How To Write ZEMAX Extensions in FORTRAN
How To Write ZEMAX Extensions in FORTRAN
- By Anthony Richards
- Published 22 June 2006
- User Articles , Extensions
-
Rating:




FORTRAN Coding Requirements 1
Controlling the calling interface from FORTRAN to C
This is done using a FORTRAN MODULE, called DDEFUNCS, that contains INTERFACE blocks for all the C-routines in ZCLIENT.OBJ that are to be called from within the FORTRAN program. This module must be included in each routine or function that needs to call one or more of the three C-functions mentioned above, as it gives the compiler the vital information that it needs to generate the correct code for function calls to either receive or send data between subprograms and to generate the correct symbols. This is done by including the FORTRAN statement ‘USE DDEFUNCS’. The module is reproduced below (with added comments):

The Visual Fortran compiler recognises ‘compiler directives’ in the form
!DEC$ ATTRIBUTES C, REFERENCE, ALIAS:'_PostRequestMessage' :: PostRequestMessageThe attributes “C, REFERENCE” are required when ‘__cdecl’ is used on the C++ side.
You will notice the inclusion of extra modules, DFWINTY and DDETYPE. DFWINTY is supplied with the Compaq Visual Fortran compiler (its equivalent supplied with the Intel Fortran would be IFWINTY) and it defines parameters and types for most of the Windows® application programming interface (API) functions and routines (such as MessageBox etc.). However, it is only required in DDEFUNCS because it contains the definition of the parameter ‘SINT’ as having value ‘4’, i.e. the integer arguments are to be declared as 4-bytes long using INTEGER(4). It is quite possible to omit the reference to DFWINTY and replace all occurrences of ‘SINT’ with the value ‘4’ if you so wish. However, if you wish to generate 64-bit code, you may have to change SINT to 8. This is automatically taken care of using the above code, where DFWINTY (or IFWINTY) contains conditionally compiled code dependent on which platform the code is to be generated for.
The inclusion of the ALIAS attribute “ALIAS:'_PostRequestMessage' :: PostRequestMessage “ ensures that the case-sensitive leading-underscored name _PostRequestMessage will be substituted for the otherwise automatically-generated all uppercase name POSTREQUESTMESSAGE used in the FORTRAN code. Thus the linker will seek for the appropriately named C-function when the executable is finally built in the FORTRAN environment.
The module DDETYPE defines a data type that exactly matches the structure of the DDERAYDATA structure defined in the C-code. The FORTRAN module code is reproduced below. The SEQUENCE statement ensures that the items are stored in memory in exactly the sequence shown, so that it will exactly match the structure defined in the C-code:
