Trouble calling VB function from C DLL

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Trouble calling VB function from C DLL

 
0
  #1
May 11th, 2009
I've been able to design a front-end for a C DLL, calling the DLL functions from the VB program but I'm having trouble calling the VB functions from the C DLL. Maybe I should have put this in the C forum, but I don't know.

If I have this in a VB module:
  1. Public Declare Function callback Lib "C:\c\dll\mydll" (ByVal cbAddress As Long)
  2.  
  3. Public Sub cb()
  4. MsgBox "Called back."
  5. End Sub
and callback AddressOf cb is used for the even of a button click or something. In a C DLL I would use this:
  1. EXPORT void __stdcall callback(long cbAddress){
  2. typedef void (__stdcall *funcp)();
  3. funcp VBCallback;
  4. VBCallback = (funcp)cbAddress;
  5. VBCallback();
  6. }
Well, the DLL compiles, and the VB program runs, but when I try to call the callback function, it says bad DLL calling convention. I tried taking the address by pointer as well (since I was able to pass values by pointer in the past, I figured it would work this time also) to no avail. And I commented out all the code in the function to see if that was the problem, but it wasn't--it's the VB program calling the callback function, which is using a "bad convention." The only difference I can see between the working code and the broken code, is that I'm using a Long data type, and that I'm not returning a value (which shouldn't matter...right?)
I used DUMPBIN /EXPORTS to check the exports of the DLL and I get:
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. 1 0 00001190 callback
  2. 2 1 00001190 callback@4
I checked the data types for VB6 and it says a long is 4 bytes, and if I compile the DLL using int, long, or short the DLL export always asks for 4 bytes in parameters, so I don't know what I'm doing wrong... Any ideas? Maybe I'll link to this in the C forum as well
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 109
Reputation: winrawr is an unknown quantity at this point 
Solved Threads: 1
winrawr's Avatar
winrawr winrawr is offline Offline
Junior Poster

Re: Trouble calling VB function from C DLL

 
0
  #2
May 12th, 2009
fix'd.
Know what the problem was?

  1. Public Declare Function callback Lib "C:\c\dll\mydll" (ByVal cbAddress As Long)
The function in the DLL doesn't return a value, so it should have been
  1. Public Declare Sub callback Lib "C:\c\dll\mydll" (ByVal cbAddress As Long)

you would think VB could have just told me that since I didn't add "As ___" to the end of it, but, oh well.
I wake up! And my mind's out, never again will I sell out. Converting vegetarians.
Into the midnight giving it to you, I don't know it just feels right.
This is the time of the revolution, Cooking the next step.
Converting vegetarians, minding the gap since 1996
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC