Click to See Complete Forum and Search --> : VB Crash after callback function is called


kwade
March 21st, 2001, 11:29 AM
Hello all,

Ultimately I'm writing a VB interface to a message broker written in java. The message broker has a C API that I'm using to interface to. My problem is that I've written a callback function in VB (it's in a standard module) and passed the address to the C API. Everything works great. I even get the callback telling me I have a message. However, for some reason, the VB development environment keeps crashing everytime the callback function is called. I've place Debug.Print statements in the callback and they all execute so I know the callback does its thing and leaves.

The error I get is "The instruction at "blah" referenced memory at "blah". The memory could not be "read".

The strange things is that the instruction address and the memory location address are the same.

I activate the callback from a form that pushes data to the message broker who in turn sends it back to me.

Any ideas on why the VB environment keeps crashing?

Thanks,

Kenny

Clearcode
March 21st, 2001, 11:43 AM
It sounds as if the heap (or instruction) pointer is in the wrong place after the call.
What's the expected return type of the C prototype callback?
If its a VOID your callback must be a Sub(), if its INT your callback must be a Function() As Long etc.
Double check this. If unsure, post us the prototype, e.g. 'typedef LRESULT (CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM); and we'll see what we can come up with.

HTH,
Duncan

-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com

kwade
March 21st, 2001, 11:57 AM
Thanks for the reply. The expected return type is an int. Here is the prototype:

typedef int (*ohMsgHandlerFunction)(ohContext, ohSession, ohEnvelope, void *)

where ohContext, ohSession, and ohEnvelope are defined as void*

Here is my function:

Public Function GPS_MessageHandler(ByVal context As Long, _
ByVal session As Long, _
ByVal envelope As Long, _
ByVal usercontext As Long) As Long

On Error Resume Next
GPS_MessageHandler = 0
End Function

I'm stumped :(

Clearcode
March 21st, 2001, 12:14 PM
The only thing I can think of is that your callback is passing pointers (i.e. call by reference) but you are reading them as ByVal. Maybe try:

public Function GPS_MessageHandler(context as Long, _
session as Long, _
envelope as Long, _
usercontext as Long) as Long

on error resume next
GPS_MessageHandler = 0
End Function




Then use IsBadReadPtr() before using those values to prevent any crashes.

HTH,
Duncan


-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com