Ok, I'll try to be more clear.
The M$ low level standard for parameters passing says that the value returned by a function is placed in the EAX register, or in the couple EAX:EDX depending on the size. If the size is greater than the couple EAX:EDX could host the compiler automatically returns a pointer to the value in the register EAX.
This is what VB expects as return value!
Now a small structure like "float complex" occupy 32+32bits, so it fits in the couple EAX:EDX. In the case of a "double complex" the size is 64+64bits and don't fits, so a pointer is returned.
This means that when you use "double complex" VB knows that a pointer to the structure is returned and everything works well. But when you return a "float complex" VB uses contents of EAX:EDX registers. But because PellesC now diverges from M$ standard the value is not correct because EAX contains the pointer to the "float complex" structure and EDX contains rubbish.
Adding a filler that expands the size of the structure automatically instructs VB that it should expect a pointer to a structure in EAX and not a value in EAX:EDX.
See this article
http://www.angelcode.com/dev/callconv/callconv.html.
Ofniw - Timovjl: I'm not expert on VB, if you know how to specify that the returned value is a pointer to structure, and not the structure itself, modify the VB code to specify this and test. If, as I suspect, is not possible to modify the expected return from VB call, you cannot use the "float complex". But if you declare a dummy structure with filler on VB side and use the standard "float complex" on C side it should work anyway with no problems while you don't try to access the filler part that doesn't effectively exist on C side.
Timovjl please could you check with debugger on VB side if register EAX, just after returning from dll call, contains effectively the address of "float complex" structure, and also check that the VB code will use EAX:EDX contents as structure value.
Please try this Timovjl modified code:
Attribute VB_Name = "Module1"
Type Complex
r As Single 'Real
i As Single 'Imaginary
filler As Double
End Type
Type Complex_16
r As Double 'Real
i As Double 'Imaginy
End Type
Declare Function fc_add Lib "Complex_DLL.DLL" (ByRef a As Complex, ByRef c As Complex) As Complex
Declare Function dc_add Lib "Complex_DLL.DLL" (ByRef a As Complex_16, ByRef b As Complex_16) As Complex_16
Sub Main()
Dim c2 As Complex_16, c3 As Complex_16, c4 As Complex_16
Dim c5 As Complex, c6 As Complex, c7 As Complex
' Complex double precision Addition
c4.r = 7
c4.i = 30
c2.r = 8
c2.i = 31
c3 = dc_add(c4, c2)
' Complex single precision, Addition
c7.r = 7
c7.i = 30
c5.r = 8
c5.i = 31
c6 = fc_add(c7, c5)
End Sub
To Pelle: seems that we have troubles in mixed programming diverging from M$ with structures handling, the same problem will be present also with C code produced on different compilers.