
VB callback function, called from a VC DLL, crashes
Hi,
I wrote a C++ DLL full of functions called from a VB 6.0 program. I use
a type library ( ODL to describe the interface ) and everything worked
fine until I wanted to call (from C++) a VB function or a VB procedure
as a callback function : the VB program and VB itself crash when I
invoke the VB callback function through the pointer I obtained using the
AddressOf VB's function.
Here is a simplified version of what I do. It also crashes lamentably.
In a .cpp file:
void (__stdcall * testPtr)(void); // global here for simplicity
// the callback
function will be call through this pointer
ALMSIM_API void __stdcall SetVBtestFnPtr( void * pf )
{ // put
the callback function's address in the global variable
if ( pf ) // equiv. !=0
testPtr = (void (__stdcall *)(void)) pf; // this cast is, in
principle, not necessary
Quote:
}
ALMSIM_API int __stdcall testBug( int context, int num, double frac )
{ // the
parameters (arguments) are not the problem
// checked by
writing to a file
ofstream myFile; // writing to a file for
debugging purposes
myFile.open( ".\\zztestbug.txt" );
myFile.setmode();
myFile << "testPtr = " << testPtr << endl;
myFile.close();
if ( testPtr ) // i.e., !=0
testPtr(); // this call crashes
our VB program and VB.exe
return 99;
Quote:
}
In the ODL file I have: (the decorated names are not the problem : the
functions are called correctly, except the VB callback function which is
not described in the ODL file)
[
usesgetlasterror,
helpstring("rien que pour un test ; bug de la progresss bar")
]
int __stdcall testBug([in] int c, [in] int n, [in] double f );
[
usesgetlasterror,
helpstring("TEST : Set the function pointer to the VB function showing
the progress bars. The pointer is obtained using the AddressOf
keyword.")
]
void __stdcall SetVBtestFnPtr([in] void * pf );
Finally, in VB I do:
Call SetVBtestFnPtr(AddressOf myProgressFn)
Dim retx As Long
retx = testBug(0, 10, 0.1) 'executing this will invoke the
callback function and crash the program and VB.exe too
and the callback function (Sub/procedure) is
Sub myProgressFn()
Dim x As Long
x = 33 'filler
x = x + 7 'filler
End Sub
This last function (VB proc/sub) "is", in C/C++, void myProgressFn(
void ).
The function pointer that is used to call it has type void (__stdcall *
)(void)
So, what is the problem????
Note: the C++ functions are exported correctly (ALMSIM_API is given by
#ifdef ALMSIM_EXPORTS
#define ALMSIM_API __declspec(dllexport)
#else
#define ALMSIM_API __declspec(dllimport)
#endif
)
Again, what is the problem? Any ideas?
Fran?ois