?
Solved

Call C DLL from VB6

Posted on 2003-03-03
10
Medium Priority
?
165 Views
Last Modified: 2008-02-01
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
Comment
Question by:VolleyMan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Expert Comment

by:Hex01
ID: 8061381
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
 

Expert Comment

by:Hex01
ID: 8061964
Sorry, That should be
"I believe in VC++ 6 char is 4 bytes."

0
 
LVL 28

Accepted Solution

by:
AzraSound earned 150 total points
ID: 8062071
What you are seeing is referred to as "name mangling".  See this article:

http://www2.ari.net/tobywan/SimpCPP.html
0
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!

 
LVL 6

Expert Comment

by:marconovaro
ID: 8062749

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
 
LVL 5

Expert Comment

by:avya2k
ID: 8064008
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
 
LVL 5

Expert Comment

by:avya2k
ID: 8064017
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
 
LVL 5

Expert Comment

by:avya2k
ID: 8064019
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
 
LVL 5

Expert Comment

by:avya2k
ID: 8064023
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
 
LVL 5

Expert Comment

by:avya2k
ID: 8064026
replace project1.def wih UR CPP file Name
0
 

Author Comment

by:VolleyMan
ID: 8067117
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 20 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question