Passing real variables between VB6 and CVF6.6A

Passing real variables between VB6 and CVF6.6A

The following call is made from a VB6 application to a Fortran subroutine linked into a DLL.

Call InitProj_dll(Proj, iError)

iError declared as Integer*4 in the Fortran subroutine and as Long in VB6.

Proj declared in the Fortran as:

Structure /ProjType/
Character*(6) Isotope ! 178Os
Integer*4 Z ! proton number
Character*2 Nuc ! chemical symbol
Integer*4 Mass ! Atomic mass AMU
Real*4 RestMass ! Rest mass, kg
Real*4 TotalMass ! Total mass, kg
Real*4 EnerMeV ! kinetic energy in MeV
Real*4 EnerSI ! kinetic energy in SI units
Real*4 Velocity ! velocity m/s
End Structure
Record / ProjType / Proj

and in the VB6 module as:

Type ProjType
Isotope As String * 6 ' 178Os
Z As Long ' proton number
Nuc As String * 2 ' chemical symbol
Mass As Long ' Atomic mass AMU
RestMass As Single ' Rest mass, kg
TotalMass As Single ' Total mass, kg
EnerMeV As Single ' kinetic energy in MeV
EnerSI As Single ' kinetic energy in SI units
Velocity As Single ' velocity m/s
End Type
Public Proj As ProjType
Declare Sub InitProj_dll Lib "Delta_dll.dll" _
(Proj As ProjType, _
iError As Long)

The program works OK as shown above.

Changing the definition of the real variablles to Real*8 in the Fortran module and Double in the VB6 module will cause problems. When the values of the Proj record examined with the Debugger in the Fortran module the Real*8 values are wrong. According to the on-line documentation on "Handling Data Types in Mixed-Language Programming" the Real*8/Double declaration should work. Is there an error in my code?

4 帖子 / 0 全新

Further investigation revealed, that the problem persist only if in the Structure definition a Real*8 variable preceded by a Real*4 variable. For example, consider a case of passing a derived type variable containing 3 elements, A(Real*8), B(Real*4) and C(Real*8). The values of A, B and C are set to 123.567 in the VB6 application before calling the Fortran routine. Examining the values with the debugger results: A=123.567000000000, B=123.5670 and C=5.335714555313168E-315.

P.S. Sorry about the wrapped format of the code in my previous posting, but it was looking correctly formatted when it was posted. I do remember now, that there are some special commands to prevent it to happen. Are there in the Forum`s Help instructions?

Community Admin的头像

I suspect that what is happening is that VB is lining up the data exactly
as declared with no padding, but that CVF is padding so that the data
has natural alignment. From the on-line docs:

By default, fields are aligned on natural boundaries; misaligned fields are padded as necessary. To avoid padding of records, you should lay out structures so that all fields are naturally aligned.

To pack fields on arbitrary byte boundaries, you must specify a compiler option. You can also specify alignment for fields by using the OPTIONS or PACK general directive.

Sounds like this is fixable, either by reordering the items in your structure, or
by finding the correct combination of options so that VB and CVF line things
up the same way.

Try adding the lines:

!dec$ pack:2
structure /projtype/
end structure
!dec$ pack:

As far as your other question about formatting in this kinda odd forum :-) just encapsulate any code you post inside PRE tags, as in