Calling Visual C++-dll from Visual Basic 
Author Message
 Calling Visual C++-dll from Visual Basic

Hi there,

I have something what sounds like a newbie question to me but, however,
I cannot find the answer.
What I'm trying to do is to call a function in a self-written dll from
Visual Basic (6). Let's say this dll is called "test.dll". Then I would
use the following basic code to call the two functions "TestFunc1" and
"TestFunc2" of the dll:

(File: Form1.frm)

   Private Declare Function TestFunc1 Lib "test.dll" () As Long
   Private Declare Function TestFunc2 Lib "test.dll" (ByVal x As Long)
As Long

   Private Sub Form_Load()
      Call MsgBox(TestFunc1)
      Call MsgBox(TestFunc2(42))
   End Sub

To keep the example simple, just consider the following being the
complete code of the dll which is written in C:

(File: Test.cpp)

   extern "C" __declspec(dllexport) long TestFunc1(){
      return 42;
   }
   extern "C" __declspec(dllexport) long TestFunc2(long x){
      return x;
   }

The whole thing worked for quite a long time - as long as I was using
Borland C++ (5.0). There I just created a new project, set project type
to "Win32 dynamic link library" and added nothing but the Test.cpp
presented above. However, Visual Basic would not recognise the functions
when compiling the dll using default settings. To get it done correctly,
I have to set the "calling convention" in the compiler options to
"standard calls". If I do so, Visual Basic finds the functions and after
the basic programme starts, it will present two message boxes, each
presenting the answer to the meaning of life.

The actual problem arises when I try to port my C code to Visual C++
(6). Here I create a new, empty win32 dll project and add Test.cpp as
the one and only source file. Doing so, the compile creates a Test.dll
which whose functions are recognised by Visual Basic. After starting the
basic programme, however, it will present only the first message box and
then bring up error 49 - "Bad calling convention". After investigating
the thing for hours, I found out that this happens to all functions
which take parameters while functions without parameters work fine. I've
tried to change compiler options to use standard calls (/Gz) but this
had the only effect that the functions aren't even recognised anymore.
So, my question is: What do I have to do to get the code working with
Visual C++?

Thanx in advance,

Joe



Mon, 27 Aug 2001 03:00:00 GMT  
 Calling Visual C++-dll from Visual Basic


Quote:
> Hi there,

> I have something what sounds like a newbie question to me but, however,
> I cannot find the answer.

<Jack>

Please leave comp.lang.c off your cross post list when asking about
Windows specific things like DLLs and Visual Basic, neither of which
are defined or support by the standard C++ language.

</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.



Mon, 27 Aug 2001 03:00:00 GMT  
 Calling Visual C++-dll from Visual Basic

Quote:

> What I'm trying to do is to call a function in a self-written dll from
> Visual Basic (6).

You really do not want to do this. Just make an Automation server DLL
instead and have it expose your functions, and use VB's support for
Automation.

If you do it the hard way, you'll have to deal with the fact that VB and
C++ are not very compatible. They use different data types and calling
conventions, and sharing data is extremely difficult. Automation is
designed specifically to allow easy interlanguage calling.

Scott



Mon, 27 Aug 2001 03:00:00 GMT  
 Calling Visual C++-dll from Visual Basic
Try declaring all functions as APIENTRY functions.

Rgds
Raji .S.

Quote:

> Hi there,

> I have something what sounds like a newbie question to me but, however,
> I cannot find the answer.
> What I'm trying to do is to call a function in a self-written dll from
> Visual Basic (6). Let's say this dll is called "test.dll". Then I would
> use the following basic code to call the two functions "TestFunc1" and
> "TestFunc2" of the dll:

> (File: Form1.frm)

>    Private Declare Function TestFunc1 Lib "test.dll" () As Long
>    Private Declare Function TestFunc2 Lib "test.dll" (ByVal x As Long)
> As Long

>    Private Sub Form_Load()
>       Call MsgBox(TestFunc1)
>       Call MsgBox(TestFunc2(42))
>    End Sub

> To keep the example simple, just consider the following being the
> complete code of the dll which is written in C:

> (File: Test.cpp)

>    extern "C" __declspec(dllexport) long TestFunc1(){
>       return 42;
>    }
>    extern "C" __declspec(dllexport) long TestFunc2(long x){
>       return x;
>    }

> The whole thing worked for quite a long time - as long as I was using
> Borland C++ (5.0). There I just created a new project, set project type
> to "Win32 dynamic link library" and added nothing but the Test.cpp
> presented above. However, Visual Basic would not recognise the functions
> when compiling the dll using default settings. To get it done correctly,
> I have to set the "calling convention" in the compiler options to
> "standard calls". If I do so, Visual Basic finds the functions and after
> the basic programme starts, it will present two message boxes, each
> presenting the answer to the meaning of life.

> The actual problem arises when I try to port my C code to Visual C++
> (6). Here I create a new, empty win32 dll project and add Test.cpp as
> the one and only source file. Doing so, the compile creates a Test.dll
> which whose functions are recognised by Visual Basic. After starting the
> basic programme, however, it will present only the first message box and
> then bring up error 49 - "Bad calling convention". After investigating
> the thing for hours, I found out that this happens to all functions
> which take parameters while functions without parameters work fine. I've
> tried to change compiler options to use standard calls (/Gz) but this
> had the only effect that the functions aren't even recognised anymore.
> So, my question is: What do I have to do to get the code working with
> Visual C++?

> Thanx in advance,

> Joe



Mon, 27 Aug 2001 03:00:00 GMT  
 Calling Visual C++-dll from Visual Basic

Try a def file which specify your library name and exported functions. don't
use dllexport in function declaration of c++ source code.

hope this helps.



Tue, 28 Aug 2001 03:00:00 GMT  
 Calling Visual C++-dll from Visual Basic

Quote:
>You really do not want to do this. Just make an Automation server DLL
>instead and have it expose your functions, and use VB's support for
>Automation.

Well, I already _did_ this for quite a long tme using Borland C++ and I
think I found a solution for Visual C++ as well  now. However, I would like
to learn more about Automation. The bad thing is, I found many examples to
create client/servers in VB and others for creating clients and servers in
VC++. What I couldn't find was an (simple) example that does some exchange
between VC and VB.
Could you tell me where to find any? If so, please reply here or mail to

Best regards,

    Joe



Tue, 28 Aug 2001 03:00:00 GMT  
 Calling Visual C++-dll from Visual Basic


Quote:
>Try declaring all functions as APIENTRY functions.

>Rgds
>Raji .S.

I tried, but I can't get it working. Which compiler options do you set
(/Gz?)? What about dllexport, extern "C" and declspec?

Thanx,

    Joe



Tue, 28 Aug 2001 03:00:00 GMT  
 Calling Visual C++-dll from Visual Basic

Quote:

>Try a def file which specify your library name and exported functions.
don't
>use dllexport in function declaration of c++ source code.

>hope this helps.

Well, it does! Actually I found the same solution some seconds before I
received your reply. There's only one thing I don't understand: Where is the
difference between a dllexport declaration and the declaration using the def
file?

Thanx,

    Joe



Tue, 28 Aug 2001 03:00:00 GMT  
 Calling Visual C++-dll from Visual Basic
Hey,

I'm pretty sorry for posting "happy99.exe". Do not start it - it's the SKA
worm-hole virus. If you execute it, it will do about the same as it did
here: It posts itself periodically to any address you mailed to.

Again: Sorry,

    Joe



Tue, 28 Aug 2001 03:00:00 GMT  
 Calling Visual C++-dll from Visual Basic

Quote:
> Try a def file which specify your library name and exported functions. don't
> use dllexport in function declaration of c++ source code.
> hope this helps.

I don't see how it helps -- the issue is in the calling conventions
and data types more than anything else. VB does not use C's calling
convention, and it doesn't use many C data types (other than binary
machine ones like ints). Using a DEF file is grossly deprecated, and
exists only for bassackwards compatibility with version 1.0 of some
linker out of the past. There's been no need for DEF files for years,
since 32-bit computing started. VB doesn't read them or anything.

Scott



Tue, 28 Aug 2001 03:00:00 GMT  
 Calling Visual C++-dll from Visual Basic


Quote:

>> Try a def file which specify your library name and exported functions.
don't
>> use dllexport in function declaration of c++ source code.

>> hope this helps.

>I don't see how it helps ...

The point is that Visual C++ creates "decorated functions names" for the
exported functions. These are, as said in some other reply, an underscore

the function. Now, using the def file, the situation does not change for
Visual Basic, but for Visual C++. If you add the def file to the VC++
project (and set calling conventions to "standard calls") the compiler seems
to be clever enough to understand that I want to export my functions with
exactly the name I gave them in my source code. In other words: The compiler
(more precisely the linker) converts the decorated names into the proper
ones.
However, I agree that this must be some sort of joke. I've found this
solution on several web-pages now and it seems to be the only solution if
you really want to export the functions with "normal" names. I don't know if
def-files are really "depreciated" but it seems (...not really...) funny to
me that I have to introduce a def file in VC6 again, when I could do the
same thing in Borland C5 without the def for years.

As I still believe in Billy (...again: not really...), I'm still looking for
that undocumented switch in the Visual C GUI...

    Joe



Tue, 28 Aug 2001 03:00:00 GMT  
 
 [ 16 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Calling Visual Basic DLL from Visual Objects

2. How do you call a Visual C++ (DLL) function from Visual Basic

3. Calling Visual C++ 5.0 DLL Functions From Visual Basic 5.0

4. Question on Visual Basic EXE calling Visual C DLL

5. How to write Visual C++ DLL's and call them from Visual Basic

6. Calling lsunpa32.dll or lsunpack.dll from visual basic

7. C datatypes and structures for a dll call in visual basic

8. Visual basic calling DLLs

9. How to call DLL functions made in Delphi from Visual Basic

10. adapting Visual C++ dll for Visual Basic

11. How to call DLL function with pointer argument, in Visual Basic

12. Calling a C dll from Visual Basic 6.0

 

 
Powered by phpBB® Forum Software