creating a callback function in c++ dll to callback a VB function in an exe 
Author Message
 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");
 }



Sun, 31 Mar 2002 03:00:00 GMT  
 creating a callback function in c++ dll to callback a VB function in an exe

Quote:
>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


David,

IIRC, VB only accepts BSTR type strings as in parameters to a
function. So you either have to use a BSTR string on the C++ side, or
type the parameter As Long in VB and then use the CopyMemory API to
get the string contents from the char pointer and copy it to a VB
string.

For more information on this, take a look at the file vbXdll.doc on
your VB CD, where X is your VB version number.

Mattias

________________________________________________
Mattias "beezo" Sj?gren

http://home1.2.sbbs.se/mattias.sjogren/sw/



Sun, 31 Mar 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. problems with C/C++ DLL passing arguments to VB Callback Function

2. Hohw to create callback function in VB

3. VB and UNRAR.dll callback function URGENT please

4. Return values from VB CallBack Function to VC Dll

5. String-Problems calling callback function in VB6 ActiveX-Control from C++

6. HOWTO: Call a VB callback function from a VC++ DLL

7. ActiveX DLL and AddressOf callback function

8. HOWTO Call a VB Callback function from VB ActiveX control

9. Callback in VB from C++ DLL

10. Q: Wininet.dll callback function w/ VB4

11. async callback function in VB.Net

12. VB callback function

 

 
Powered by phpBB® Forum Software