Calling Fortran 77 dll's from Visual Basic under Excel 
Author Message
 Calling Fortran 77 dll's from Visual Basic under Excel

Hi all

I am using Absoft fortran 77 to create DLL's and I then call them from
VBA under Excel97. I have been doing this for quite some time now.
I have no trouble in sending variables of doubles and integers OR arrays of
integers or doubles through to the DLL's.
STRINGS, however, seems to be a problem. I have never send a string through to
a DLL and this now stumbles me.

I tried various ways. I declare the string in VBA as for instance
Byval StringVar as String

I then dimension the string to be a fixed length like
Dim StringVar as String*8
or
Static StringVar as String*8

On the Fortran side I then have

       STDCALL subroutine test(....., StringVar,...)
       character*8 stringvar

This does not work. What is wrong??
I also tried to have

       STDCALL subroutine test(....., StringVar,...)
       character*(*) stringvar
in my fortran code. It still does not work.

The whole program is working fine because if I do not send a string through,
i.e., if I make stringvar to be a double and I just work with stringvar as a
real*8 then the program works 100%.
But, the program would be a bit more efficient if I could send a string
through.

How?.
If anyone can help, please let me know.

Thanks

Antonie Kotze



Wed, 18 Jun 1902 08:00:00 GMT  
 Calling Fortran 77 dll's from Visual Basic under Excel
I don't know much about Fortran 77 but I seem to remember from my Fortran 90
days strings need to be passed null terminated
this can be acheived in VB by somthing like the following:

   Dim StringVar as String

''''''''''''''''''''''''''''''''''''''''''''
  'Before the API call

   StringVar = StringVar & Chr(0)

''''''''''''''''''''''''''''''''''''''''''''''
Call   test(....., StringVar,...)

I haven't tested this but I hope it works for you.

Matt

Quote:

>Hi all

>I am using Absoft Fortran 77 to create DLL's and I then call them from
>VBA under Excel97. I have been doing this for quite some time now.
>I have no trouble in sending variables of doubles and integers OR arrays of
>integers or doubles through to the DLL's.
>STRINGS, however, seems to be a problem. I have never send a string through
to
>a DLL and this now stumbles me.

>I tried various ways. I declare the string in VBA as for instance
>Byval StringVar as String

>I then dimension the string to be a fixed length like
>Dim StringVar as String*8
>or
>Static StringVar as String*8

>On the Fortran side I then have

>       STDCALL subroutine test(....., StringVar,...)
>       character*8 stringvar

>This does not work. What is wrong??
>I also tried to have

>       STDCALL subroutine test(....., StringVar,...)
>       character*(*) stringvar
>in my fortran code. It still does not work.

>The whole program is working fine because if I do not send a string
through,
>i.e., if I make stringvar to be a double and I just work with stringvar as
a
>real*8 then the program works 100%.
>But, the program would be a bit more efficient if I could send a string
>through.

>How?.
>If anyone can help, please let me know.

>Thanks

>Antonie Kotze




Wed, 18 Jun 1902 08:00:00 GMT  
 Calling Fortran 77 dll's from Visual Basic under Excel
I had this problem about a year and a half ago with F90 and eventually had
to give up on using strings. Then someone mentioned using structures might
be the solution but I never got to try it.



Quote:
> Hi all

> I am using Absoft Fortran 77 to create DLL's and I then call them from
> VBA under Excel97. I have been doing this for quite some time now.
> I have no trouble in sending variables of doubles and integers OR arrays
of
> integers or doubles through to the DLL's.
> STRINGS, however, seems to be a problem. I have never send a string
through to
> a DLL and this now stumbles me.

> I tried various ways. I declare the string in VBA as for instance
> Byval StringVar as String

> I then dimension the string to be a fixed length like
> Dim StringVar as String*8
> or
> Static StringVar as String*8

> On the Fortran side I then have

>        STDCALL subroutine test(....., StringVar,...)
>        character*8 stringvar

> This does not work. What is wrong??
> I also tried to have

>        STDCALL subroutine test(....., StringVar,...)
>        character*(*) stringvar
> in my fortran code. It still does not work.

> The whole program is working fine because if I do not send a string
through,
> i.e., if I make stringvar to be a double and I just work with stringvar
as a
> real*8 then the program works 100%.
> But, the program would be a bit more efficient if I could send a string
> through.

> How?.
> If anyone can help, please let me know.

> Thanks

> Antonie Kotze




Wed, 18 Jun 1902 08:00:00 GMT  
 Calling Fortran 77 dll's from Visual Basic under Excel
When passing strings from VB to Fortran, you need to keep in mind that VB
stores strings as BStrings. This format is basically not compatible with the
one used by Fortran. BStrings are wide strings (i.e. each character takes 2
bytes), they are null terminated (actually double-null terminated), and have
the length of the string right in front. A pointer to a bstring points to
the first character of the string. As a rule, BStrings should only be
modified through the supplied OLE-API, so you are not supposed to change a
Bstring from inside Fortran (unless you do it calling the appropriate
OLE-API). Now, the trick to pass BStrings from VB to Fortran is to do it by
value. In the VB end, you declare the Fortran sub as:


MyText As String)

on the Fortran end, you declare the subroutine as

stdcall subroutine MyStringSub(StringPtr)

character(len=255) MyString
pointer (StringPtr, MyString)
:
:

You must make sure that MyString is long enough for the strings you are
passing. Also, MyString will be null terminated, so you have to scan the
string to find its length. This trick works because VB will convert the
passed Bstring into a Multibyte string when calling API functions by Value.
So what you get in your Fortran side is basically a pointer to a normal
C-style string.

For those of you that have been struggling with this problem, you should
visit our web pages in about two more weeks. We will publish a library of
subroutines (actually, a portion of our f90ADO library) that will allow
passing bstrings back and forth between Fortran and VB.

Marco A. Garcia
Canaima Software
3981 Utah St.
San Diego, CA 92104
U.S.A.

http://www.canaimasoft.com
Developers of f90SQL and f90ADO,
the Complete Database Connectivity Solution for Fortran

Quote:

>Hi all

>I am using Absoft Fortran 77 to create DLL's and I then call them from
>VBA under Excel97. I have been doing this for quite some time now.
>I have no trouble in sending variables of doubles and integers OR arrays of
>integers or doubles through to the DLL's.
>STRINGS, however, seems to be a problem. I have never send a string through
to
>a DLL and this now stumbles me.

>I tried various ways. I declare the string in VBA as for instance
>Byval StringVar as String

>I then dimension the string to be a fixed length like
>Dim StringVar as String*8
>or
>Static StringVar as String*8

>On the Fortran side I then have

>       STDCALL subroutine test(....., StringVar,...)
>       character*8 stringvar

>This does not work. What is wrong??
>I also tried to have

>       STDCALL subroutine test(....., StringVar,...)
>       character*(*) stringvar
>in my fortran code. It still does not work.

>The whole program is working fine because if I do not send a string
through,
>i.e., if I make stringvar to be a double and I just work with stringvar as
a
>real*8 then the program works 100%.
>But, the program would be a bit more efficient if I could send a string
>through.

>How?.
>If anyone can help, please let me know.

>Thanks

>Antonie Kotze




Wed, 18 Jun 1902 08:00:00 GMT  
 Calling Fortran 77 dll's from Visual Basic under Excel

Quote:
> I am using Absoft Fortran 77 to create DLL's and I then call them from
> VBA under Excel97. I have been doing this for quite some time now.
> I have no trouble in sending variables of doubles and integers OR arrays
of
> integers or doubles through to the DLL's.
> STRINGS, however, seems to be a problem. I have never send a string
through to
> a DLL and this now stumbles me.

Basically, character strings are non-standard and are implemented totally
differently on every system and every language under the sky. Well, that
may be a slight exaggeration, but it will do. Add to that the attempts by
some misguided vendors (MS?) to introduce Unicode, where the character
string isn't even in ASCII any more, and it becomes a farce.

What you can do fairly reliably however is to *explicitely* convert the
string to a byte vector, character by character and in VB, and then pass
that in the usual manner to the DLL. This does work. It bypasses the whole
character string farce.
This is useful when you call a CommonDialogBox in VB and then pass the
returned filename to the DLL for instance. But check in the program that
there is something in the string: if the user "cancels" out of the box
nothing is returned.

I think the latest VB does have some extra features for handling character
strings to overcome some of these problems (or was it DVF v6?), but why
bother? The compute time to do the conversion to bytes is essentially zero.

Cheers, Roger
--
Roger Caffin (Dr)
Director
Berrilee Consulting Services P/L
5 Charltons Ck Rd
Berrilee  NSW  2159
Australia
All the usual disclaimers apply....



Wed, 18 Jun 1902 08:00:00 GMT  
 Calling Fortran 77 dll's from Visual Basic under Excel

Quote:
>I don't know much about Fortran 77 but I seem to remember from my Fortran 90
>days strings need to be passed null terminated
>this can be acheived in VB by somthing like the following:

>   Dim StringVar as String

>''''''''''''''''''''''''''''''''''''''''''''
>  'Before the API call

>   StringVar = StringVar & Chr(0)

>''''''''''''''''''''''''''''''''''''''''''''''
>Call   test(....., StringVar,...)

>I haven't tested this but I hope it works for you.

>Matt

From the above, it appears that you never had any "Fortran 90 days".

--
Ronald Sverdlove               Computing Systems Research

Tel. 609-734-2517              CN 5300
FAX  609-734-2662              Princeton, NJ 08543-5300



Wed, 18 Jun 1902 08:00:00 GMT  
 Calling Fortran 77 dll's from Visual Basic under Excel
Oops! I just realized that the heading said f77 and not f90. So the Fortran
portion should be declared as follows:

stdcall subroutine MyStringSub(MyString)

character*255 MyString

Marco Garcia
Canaima Software

Quote:

>When passing strings from VB to Fortran, you need to keep in mind that VB
>stores strings as BStrings. This format is basically not compatible with
the
>one used by Fortran. BStrings are wide strings (i.e. each character takes 2
>bytes), they are null terminated (actually double-null terminated), and
have
>the length of the string right in front. A pointer to a bstring points to
>the first character of the string. As a rule, BStrings should only be
>modified through the supplied OLE-API, so you are not supposed to change a
>Bstring from inside Fortran (unless you do it calling the appropriate
>OLE-API). Now, the trick to pass BStrings from VB to Fortran is to do it by
>value. In the VB end, you declare the Fortran sub as:


>MyText As String)

>on the Fortran end, you declare the subroutine as

>stdcall subroutine MyStringSub(StringPtr)

>character(len=255) MyString
>pointer (StringPtr, MyString)
>:
>:

>You must make sure that MyString is long enough for the strings you are
>passing. Also, MyString will be null terminated, so you have to scan the
>string to find its length. This trick works because VB will convert the
>passed Bstring into a Multibyte string when calling API functions by Value.
>So what you get in your Fortran side is basically a pointer to a normal
>C-style string.

>For those of you that have been struggling with this problem, you should
>visit our web pages in about two more weeks. We will publish a library of
>subroutines (actually, a portion of our f90ADO library) that will allow
>passing bstrings back and forth between Fortran and VB.

>Marco A. Garcia
>Canaima Software
>3981 Utah St.
>San Diego, CA 92104
>U.S.A.

>http://www.canaimasoft.com
>Developers of f90SQL and f90ADO,
>the Complete Database Connectivity Solution for Fortran


>>Hi all

>>I am using Absoft Fortran 77 to create DLL's and I then call them from
>>VBA under Excel97. I have been doing this for quite some time now.
>>I have no trouble in sending variables of doubles and integers OR arrays
of
>>integers or doubles through to the DLL's.
>>STRINGS, however, seems to be a problem. I have never send a string
through
>to
>>a DLL and this now stumbles me.

>>I tried various ways. I declare the string in VBA as for instance
>>Byval StringVar as String

>>I then dimension the string to be a fixed length like
>>Dim StringVar as String*8
>>or
>>Static StringVar as String*8

>>On the Fortran side I then have

>>       STDCALL subroutine test(....., StringVar,...)
>>       character*8 stringvar

>>This does not work. What is wrong??
>>I also tried to have

>>       STDCALL subroutine test(....., StringVar,...)
>>       character*(*) stringvar
>>in my fortran code. It still does not work.

>>The whole program is working fine because if I do not send a string
>through,
>>i.e., if I make stringvar to be a double and I just work with stringvar as
>a
>>real*8 then the program works 100%.
>>But, the program would be a bit more efficient if I could send a string
>>through.

>>How?.
>>If anyone can help, please let me know.

>>Thanks

>>Antonie Kotze




Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Creating - Fortran 77 dll for use in Visual Basic 6.0 (not using Compaq Visual Fortran)

2. Calling MS Excel Visual Basic functions from Fortran DLL

3. Fortran 77, DLLs, Visual Basic ?

4. HELP : Visual Basic calling FORTRAN DLL

5. HELP : visual basic calling fortran DLL

6. Calling Fortran DLL in VIsual Basic 6.0

7. Calling fortran dll from visual basic

8. Setting up a MS Fortran .DLL/calling from Visual Basic

9. Visual Basic call a Fortran DLL

10. Calling Fortran-DLLs with Visual Basic 4.0

11. Calling a Visual Basic DLL from a Fortran executable

12. Calling a fortran DLL from Visual Basic

 

 
Powered by phpBB® Forum Software