Click to See Complete Forum and Search --> : DLL Callbacks


bcyde
October 5th, 1999, 01:03 PM
I was wondering if someone could point me in the direction of preferably a working example or some good reference material for a Visual Basic Callback function to a C dll. I've read through some articles in MSDN but can't get one to work correctly. As a test I just wanted to make VB executable that on a button click would call a DLL function that in turn would call a VB function with an integer parameter specified by the DLL.

Here is the complete code (minus the .def file from my test dll)

typedef int (*LPCALLBACK)(long percent);

int __stdcall MyCFunction(LPCALLBACK lp)
{
lp(5);
return 0;
}

and here is the code from my test VB module
Option Explicit
Public Declare Function MyCFunctionLib _
"testdll" (ByVal lngFnPtr As Long) As Long

Public Function how(a As Integer)
MsgBox a
End Function

and here is the code from my main test form:

Private Sub Command1_Click()
Call MyCFunction(AddressOf how)
End Sub

When I run the executable the messagebox always comes up with -3028 and then I get an object variable or block with not set error. Can anyone help me please =). Thanks in advance. This is all in Visual Studio 6 btw.

ATM
October 5th, 1999, 02:10 PM
Using C/C++ or VC++ wrotten DLLs in VB explains in my example at: http://www.tair.freeservers.com
dllWorks Project.

bcyde
October 7th, 1999, 12:02 PM
I checked your link, and maybe I am missing something, but I didn't find anything about callbacks in the code. As of right now, I have gotten other DLLs to work with my program, but I just want to know how to specifically do a callback correctly. I know that I am passing the function (via addressof) in my code, because that function does get called, however the DLL is not passing parameters to this function correctly. Thanks for your assistance.

April 20th, 2000, 10:10 PM
OK, try this one more time...

In your C function you declared your callback as returning an int. But your VB Declaration returns nothing.
Also, keep in mind the difference between int in C and Integer in VB. The first is 4 bytes wide, the latter is 2 bytes wide. I prefer to stick to Long (long) which is the same width in both - avoids confusion...

Also, ByVal is the preferred way to pass variables around.
If you're sending a pointer (that would allow the function to change the value) then use ByRef.

Try this VB declaration...

Public Function how(Byval a As Long) as long


ERX

TCartwright
April 21st, 2000, 08:56 AM
Another point to consider is that the how() function MUST reside in a module. AddresOf will not work for any other function or sub placed any where else. Great huh?

Tim Cartwright 'Will write code for food.
Information Systems
Splitrock Services Inc.

Crazy D
April 21st, 2000, 09:38 AM
"Another point to consider is that the how() function MUST reside in a module. AddresOf will not work for any other function or sub placed any where else. Great huh?"

Uhmm well, at least, VB is object oriented at that part.... since a form is a class, you can't use a function inside it for callback., just like in C++ where a function in a class can't be used for callbacks (ok, it can, but takes a huge pain to do so....)

Crazy D :-)
"One ring rules them all"