943,701 Members | Top Members by Rank

Ad:
May 11th, 2009
0

Trouble calling VB function from C DLL

Expand Post »
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:
vb Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008
May 12th, 2009
0

Re: Trouble calling VB function from C DLL

fix'd.
Know what the problem was?

vb Syntax (Toggle Plain Text)
  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
vb Syntax (Toggle Plain Text)
  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.
Reputation Points: 19
Solved Threads: 1
Junior Poster
winrawr is offline Offline
110 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Update Binary Field in sql server 2000
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Convert byte Array to string + VB 6.0





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC