Software Archive
Read-only legacy content
17061 Discussions

dll stdcall convention for vba

pecan204
Beginner
472 Views
Hello, I cannot get the string to pass to VA and suspect the stdcall to be incorrect? Can someone help?Option Explicit

In VBA:
Public Declare Sub CALL_string Lib "C:Program FilesMicrosoft Visual StudioMy Projectscall_stringcall_string.dll" Alias _
"_FCALL_STRING@4" (ByRef b As String)

'string fixed length
Function Mod5()
Static b As String * 4
Call FCALL_string(b)
Mod5 = b
End Function

In Fortran:
subroutine FCALL_STRING(b)
!DEC$ ATTRIBUTES DLLEXPORT::FCALL_STRING
Character*4, intent(out) :: b
b = 'boeh'
end subroutine FCALL_STRING

Thank you.
0 Kudos
8 Replies
Intel_C_Intel
Employee
472 Views
The CVF online documentation is a very good resource. The Programmer's Guide has a chapter on Programming with Mixed Languages. It is required reading if you're doing mixed language programming. That chapter has an 'example passing strings where 'the length of the string will be constant and known by both the Basic and Fortran code, so you do not need to pass the length to Fortran, but you need to tell Fortran not to expect its length'. That example maps exactly to what you are trying to do.

However, I would not recommend hard coding in string lengths like that. But hey, there's another CVF sample on their website that is more robust and shows how to pass the hidden length arguement that fortran expects for assumed length character dummy arguments.

hth,
John
0 Kudos
pecan204
Beginner
472 Views
Hello,

I'm aware of these examples and have tried them however , they fail to identify the proper stdcall parameter.

I assummed it is "_FCALL_STRING@4" for this example. But it doesn't work.

What should this parameter be for both of these examples?

Anyone? Thanks.
0 Kudos
Steven_L_Intel1
Employee
472 Views
First, you have to change the VB declaration of the argument as ByVal instead of ByRef. Then, add the following to your Fortran routine:

!DEC$ ATTRIBUTES REFERENCE :: FCALL_STRING, B

That should do what you want. You need ByVal in VB which means "just pass an ASCII version of the string by reference". Otherwise you get a "BSTRING" which Fortran doesn't know how to interpret directly.

Steve
0 Kudos
pecan204
Beginner
472 Views
I used the above reference and changed to byval but I get a runtime error 453 cant find dll entry point. What do you think?

I am still uncertain if the allias "_FCALLl_STRING@4" is correct?
0 Kudos
Steven_L_Intel1
Employee
472 Views
Well, the compiler is generating _FCALL_STRING@8 and it's expecting a second argument which is the length by value. But the REFFERENCE keywords on the routine and argument SHOULD have avoided that - I don't understand why not. I'll find out...

Steve
0 Kudos
Steven_L_Intel1
Employee
472 Views
Ok - the answer is that to get the @4 you need to specify Project..Settings..Fortran..External Procedures..String length argument passing..After all args. THEN the changes I suggested above will drop the hidden length argument.

Steve
0 Kudos
pecan204
Beginner
472 Views
I used the above reference and changed to byval but I get a runtime error 453 cant find dll entry point. What do you think?

I am still uncertain if the allias "_FCALLl_STRING@4" is correct?
0 Kudos
pecan204
Beginner
472 Views
Steve, Made the changes and everything works now. Thanks!
0 Kudos
Reply