• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 166
  • Last Modified:

Call C DLL from VB6

I have seen variations on this question asked before, but I have tried those suggestions and nothing seems to work correctly for me.  I have a simple C DLL in which I have added the following routine (built in VC++ 6):

char __declspec(dllexport) _stdcall Silly_VB
(char number)
{

   return (5);
}

In my VB6 code:

Private Declare Function Silly_VB _
Lib "MyDLL.dll" _
    (ByVal a As Byte) As Byte


    Dim bytA As Boolean
    bytA = Silly_VB(1)

When I run the VB app I get a runtime 453 saying that DLL entry by that name could not be found.  I used Dependency Walker and it shows an entry _Silly_VB@4 so that is why I can't see it in my VB app.  Anybody have any suggestions?

thanks

0
VolleyMan
Asked:
VolleyMan
1 Solution
 
Hex01Commented:
The first thing I notice is that in the DLL you have number declared as a char.  I beleive in VB6 char is 4 bytes.  (hence the _Silly_VB@4)  So your declaration in VB should read:
Private Declare Function Silly_VB Lib "MyDLL.dll" _
(ByVal a As Long) as Long


If I am wrong on this sombody please correct me!  :)

Hope this helps
0
 
Hex01Commented:
Sorry, That should be
"I believe in VC++ 6 char is 4 bytes."

0
 
AzraSoundCommented:
What you are seeing is referred to as "name mangling".  See this article:

http://www2.ari.net/tobywan/SimpCPP.html
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
marconovaroCommented:

You'll have to recompile the C++ project using the "stdcall" compilation (you can find it on the project properties).
This will create a function name that is compatible with the one you need.

HTH
M.
0
 
avya2kCommented:
Ofcourse u will get error cause you have declared return Data Type for ur function as char and using as Byte in Declaration in vb Ty using Integer
cause in c we can evaluate C char with integer
0
 
avya2kCommented:
Also if You remove __declspec(dllexpot) it doesn't matters
it will work for VB
only Make sure to declare it in project1.def as
Exports
0
 
avya2kCommented:
Also if You remove __declspec(dllexpot) it doesn't matters
it will work for VB
only Make sure to declare it in project1.def as
Exports
0
 
avya2kCommented:
Also if You remove __declspec(dllexpot) it doesn't matters
it will work for VB
only Make sure to declare it in project1.def as
Exports
     Silly_VB
0
 
avya2kCommented:
replace project1.def wih UR CPP file Name
0
 
VolleyManAuthor Commented:
Actually the answer was a little more complicated in that the name was getting mangled, but it needed to be either alias'ed in the DEF file OR in the VB source.

Here is the url to the correct answer:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/html/_core_calling_dll_functions_from_visual_basic_applications.asp

thanks everyone.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now