
creating a callback function in c++ dll to callback a VB function in an exe
Hya. I'm trying to set up a DLL in Borland C++ that I can call from a Visual
Basic program. The DLL should be able to callback a function in the VB
program. I've no idea how to do this I don't have any decent books so I
basically just took a wild guess from looking through the Win32 reference
help file.
My function seems to be getting called back ok, but I'm unable to read
parameters that have been sent to my function. If I try to read the contents
of Msg I get an illegal operation error in vba5.dll. If I comment that line
out, the "callback is being called" message displays the correct number of
times no problem and no crashes. Anyway here's the code I'd be grateful for
The VB5 code...
In a form...
Private Sub Command1_Click()
TheBigTest AddressOf ToBeCalled, 5
End Sub
In a module...
Public Declare Sub TheBigTest Lib "callback.dll" Alias "callme" (ByVal Addy
As Long, Times as Long)
Public Sub ToBeCalled(Msg As String)
Debug.Print Msg ' This line caused an illegal operation - program works
fine when commented out
Debug.Print "Callback is being called :-)"
End Sub
The C++ DLL code...
#include <windows.h>
typedef VOID (*MYPROC)(LPTSTR);
void _export _stdcall callme(FARPROC addr, int times){
MYPROC ProcAdd;
ProcAdd=(MYPROC) addr;
(ProcAdd) ("Messages are kewlykewly\n");
for(int time=0;time<times;time++){
(ProcAdd)("message");
}