VB callback function, called from a VC DLL, crashes 
Author Message
 VB callback function, called from a VC DLL, crashes


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


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 << "testPtr = " << testPtr << endl;

 if ( testPtr )  // i.e., !=0
  testPtr();                                        // this call crashes
our VB program and VB.exe

 return 99;


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)


  helpstring("rien que pour un test ; bug de la progresss bar")

  int __stdcall testBug([in] int c, [in] int n, [in] double f );


  helpstring("TEST : Set the function pointer to the VB function showing
the progress bars. The pointer is obtained using the AddressOf

  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 *

So, what is the problem????

Note: the C++ functions are exported correctly (ALMSIM_API is given by
#define ALMSIM_API __declspec(dllexport)
#define ALMSIM_API __declspec(dllimport)

Again, what is the problem?  Any ideas?


Sat, 10 May 2003 09:18:04 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Return values from VB CallBack Function to VC Dll

2. How do you call VB ActiveX DLL functions from VC

3. How to call function in VB from Dll with VC

4. Help: Need to export VC 5.0 function in DLL to be called from VB 5.0

5. How to call VB functions in VB dlls from VC++

6. Calling VB DLLs from VC/C++

7. Problem calling DLL writen in VC 5.0 from VB 5.0

8. Calling VB DLL from VC problem

9. Problem : Calling VC DLL in VB

10. Debugging DLLs written in VC but called from VB

11. Debugging VC DLL called from VB

12. Problem calling DLL writen in VC 5.0 from VB 5.0


Powered by phpBB® Forum Software