Mixed language programming DLL , C++, VB/VBA, Fortran 
Author Message
 Mixed language programming DLL , C++, VB/VBA, Fortran

I need to create a DLL from C++ and call some of its functions from VB/VBA
and fortran.

While creating the DLL I use the /Gz option and also  '  extern "C"  '
The code below causes runtime error 453 although the function has been
exported as    int ExtractInt(CString &Line)

Declare Function ExtractInt Lib "MyDll.dll" (ByRef Line) As Integer
Function TestExtractInt() As Integer
    Dim ValInt As Integer
    Dim Text As String
    Text = "12 Jack 3"
    ValInt = ExtractInt(Text)
    TxtRet = Text
    TextExtractInt = ValInt
End Function

Questions:
a) Where can I find documentation that describes how to call DLLs written in
C++ from VB (especially if MFC and CStrings are involved)?
b) What is wrong with my code?



Fri, 16 Apr 2004 17:50:05 GMT  
 Mixed language programming DLL , C++, VB/VBA, Fortran
If you are trying to pass a string from VB to the dll, you will probably
want to declare the string as a char* (LPSTR) in the  dll and ByVal ... As
String in VB.

Don't have a clue how to call from FORTRAN.

Check out the thread from last week entitled "VB + C DLL: Probs filling a
String param".

--
Michael Shutt

Please respond to newsgroup as I will not return direct emails.


Quote:
> I need to create a DLL from C++ and call some of its functions from VB/VBA
> and Fortran.

> While creating the DLL I use the /Gz option and also  '  extern "C"  '
> The code below causes runtime error 453 although the function has been
> exported as    int ExtractInt(CString &Line)

> Declare Function ExtractInt Lib "MyDll.dll" (ByRef Line) As Integer
> Function TestExtractInt() As Integer
>     Dim ValInt As Integer
>     Dim Text As String
>     Text = "12 Jack 3"
>     ValInt = ExtractInt(Text)
>     TxtRet = Text
>     TextExtractInt = ValInt
> End Function

> Questions:
> a) Where can I find documentation that describes how to call DLLs written
in
> C++ from VB (especially if MFC and CStrings are involved)?
> b) What is wrong with my code?



Fri, 16 Apr 2004 20:28:14 GMT  
 Mixed language programming DLL , C++, VB/VBA, Fortran

Quote:

> I need to create a DLL from C++ and call some of its functions from VB/VBA
> and Fortran.

Which Fortran compiler?  I have some familiarity with using Fortran
with other languages, including C/C++ and VB, but I'm not sure how many
other readers of the VC groups do.  You may want to include
comp.lang.fortran in the newsgroups list for any questions along those
lines.

Quote:
> While creating the DLL I use the /Gz option and also  '  extern "C"  '
> The code below causes runtime error 453 although the function has been
> exported as    int ExtractInt(CString &Line)

Using CString in this context is a very bad idea.  The native VB string
type is BSTR, and otherwise you should use some variety of char *.
While it might be theoretically possible to construct something that
matches the internal layout of the MFC CString in VB, IMO it's much
more trouble than it's worth.  Better to take a char * and, if
necessary, convert it.

Beyond that, VB requires the __stdcall calling convention, which it
doesn't appear you used.

So, for example,

    int __stdcall ExtractInt(char * Line);

and beware of memory management and ownership issues.

Quote:
> Declare Function ExtractInt Lib "MyDll.dll" (ByRef Line) As Integer

C/C++ int != VB Integer

    Private Declare Function ExtractInt Lib "MyDll.dll"

Quote:
> Questions:
> a) Where can I find documentation that describes how to call DLLs written in
> C++ from VB (especially if MFC and CStrings are involved)?

There should be some documentation in MSDN.  I'm not sure, offhand,
where to find it.


Fri, 16 Apr 2004 23:25:29 GMT  
 Mixed language programming DLL , C++, VB/VBA, Fortran

Quote:

> I need to create a DLL from C++ and call some of its functions from VB/VBA
> and Fortran.

> While creating the DLL I use the /Gz option and also  '  extern "C"  '
> The code below causes runtime error 453 although the function has been
> exported as    int ExtractInt(CString &Line)

> Declare Function ExtractInt Lib "MyDll.dll" (ByRef Line) As Integer
> Function TestExtractInt() As Integer
>     Dim ValInt As Integer
>     Dim Text As String
>     Text = "12 Jack 3"
>     ValInt = ExtractInt(Text)
>     TxtRet = Text
>     TextExtractInt = ValInt
> End Function

> Questions:
> a) Where can I find documentation that describes how to call DLLs written in
> C++ from VB (especially if MFC and CStrings are involved)?
> b) What is wrong with my code?

1) VC++ function must use __stdcall for VB to call it.

2) VC int == VB Long, you must use the same size variables

3) When passing String from VB to VC Dll it is passed ByVal not ByRef.

Declare Function ExtractInt Lib "MyDll.dll" (ByVal Line as String) As
Long

Also check out MSDN Article ID: Q106553
Although the following article applies to VBA, I believe it will
also help for regular VB...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modc...



Sat, 17 Apr 2004 01:41:45 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Mixed language programming DLL , C++, VB/VBA, Fortran

2. Mixed Language - VBA calling C DLL passing a structure

3. Problems with Mixed Language Compilation (C++/FORTRAN) on PC

4. Problems with Mixed Language Compilation (C++/FORTRAN) on PC

5. mixed-language programming (Calling Fortran routine from VC++)

6. C++/C Mixed language programming issues

7. Fortran/C Mixed Language Compile

8. Mixed language FORTRAN and C

9. call a VB COM dll thru a C++ COM dll from a C program

10. call a VB COM dll thru a C++ COM dll from a C program

11. VC/Fortran mixed programming

12. Mixing C/C++ FORTRAN follow-up

 

 
Powered by phpBB® Forum Software