Calling a VB function/procedure from Visual C++ DLL 
Author Message
 Calling a VB function/procedure from Visual C++ DLL

Hello,

I have a {*filter*} problem and I really could not solve it myselfe.

What I wanted to try is calling a Visual Basic (v 5.0) function/
procedure from within a Windows DLL written in Visual C++
(v 4.2). For instance the dll could then message an event to
the VB programm.

I do not want to use OLE or something like this because it
would be a little bit too much overhead for this programm.

So I thougt I would call a dll-function and provide it the
adress of the VB function to call back. If a certain event
occurs, the dll should then call this VB function.

I tried it with the little test program attached to this posting
but what happens is excatly nothing - not even a crash.
(I know, with my method I will run into some other troubles
like the VB function gets sorted to another place in memory,
but this are things I wanted to deal with later.)

Could anyone help me with this?

Thanks in advance very much,
Anton Engel

------  VB part  begin  ------
Declare Sub initme Lib "testdll" (ByVal theCallBackFunct As Long)
Declare Sub thecallback Lib "testdll" ()

Private Sub Command1_Click()
        initme AddressOf CallBackMe
        thecallback
End Sub

Public Sub CallBackMe()
        MsgBox "got it :-)"
End Sub
------  VB part  end  ------

------  VC++ testdll.c  begin  ------
#include "windows.h"
FARPROC lpfnForgProc;

void APIENTRY initme(FARPROC theProc)
{
        lpfnForgProc = theProc;

Quote:
}

void APIENTRY thecallback(void)
{
         (*lpfnForgProc);
Quote:
}

------  VC++ testdll.c end  ------

------  VC++ testdll.def begin  ------
NAME         testdll.dll
DESCRIPTION  'testing VB callback'
CODE         PRELOAD MOVEABLE DISCARDABLE
DATA         PRELOAD MOVEABLE MULTIPLE
HEAPSIZE     1024
STACKSIZE    5120
EXPORTS


------  VC++ testdll.def end  ------



Sat, 24 Feb 2001 03:00:00 GMT  
 Calling a VB function/procedure from Visual C++ DLL

Quote:

>snipped
>What I wanted to try is calling a Visual Basic (v 5.0) function/
>procedure from within a Windows DLL written in Visual C++
>(v 4.2). For instance the dll could then message an event to
>the VB programm.

I may be wrong, but I do not think that this is possible.  VB doesn't
provide a way to handle Callback functions like that.  The closest thing
would be to make some sort of OCX or something (I know you said that you
didn't want to do that) and have it fire some sort of event.

Like I said, I may be wrong but I am pretty sure that this is so.

--michael



Sat, 24 Feb 2001 03:00:00 GMT  
 Calling a VB function/procedure from Visual C++ DLL

Quote:

>------  VB part  begin  ------
>Declare Sub initme Lib "testdll" (ByVal theCallBackFunct As Long)
>Declare Sub thecallback Lib "testdll" ()

>Private Sub Command1_Click()
>        initme AddressOf CallBackMe
>        thecallback
>End Sub

>Public Sub CallBackMe()
>        MsgBox "got it :-)"
>End Sub
>------  VB part  end  ------

>------  VC++ testdll.c  begin  ------
>#include "windows.h"
>FARPROC lpfnForgProc;

>void APIENTRY initme(FARPROC theProc)
>{
>        lpfnForgProc = theProc;
>}

>void APIENTRY thecallback(void)
>{
>         (*lpfnForgProc);
>}
>------  VC++ testdll.c end  ------

The problem is in the last C function.  Your syntax for calling a function
from a pointer is wrong.  Try:

(*lpfnForgProc)();

Without the parameter braces it does not evaluate to a function call.



Sun, 25 Feb 2001 03:00:00 GMT  
 Calling a VB function/procedure from Visual C++ DLL

Quote:


>>------  VB part  begin  ------
>>Declare Sub initme Lib "testdll" (ByVal theCallBackFunct As Long)
>>Declare Sub thecallback Lib "testdll" ()

>>Private Sub Command1_Click()
>>        initme AddressOf CallBackMe
>>        thecallback
>>End Sub

>>Public Sub CallBackMe()
>>        MsgBox "got it :-)"
>>End Sub
>>------  VB part  end  ------

>>------  VC++ testdll.c  begin  ------
>>#include "windows.h"
>>FARPROC lpfnForgProc;

>>void APIENTRY initme(FARPROC theProc)
>>{
>>        lpfnForgProc = theProc;
>>}

>>void APIENTRY thecallback(void)
>>{
>>         (*lpfnForgProc);
>>}
>>------  VC++ testdll.c end  ------

>The problem is in the last C function.  Your syntax for calling a function
>from a pointer is wrong.  Try:

>(*lpfnForgProc)();

>Without the parameter braces it does not evaluate to a function call.

Oh, of course. Yes, I know, the stupid VB programmsers... ;-)
Thank you very much! That works quite well,

Anton



Sun, 25 Feb 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How do you call a Visual C++ (DLL) function from Visual Basic

2. Calling Visual C++ 5.0 DLL Functions From Visual Basic 5.0

3. Calling a C++ DLL function which takes an C++ object as parameter

4. VB 5.0 calling C++ 5.0 DLL that calls winsock.dll

5. Calling a VB function from within a C++ DLL interrupt

6. VB calling C++ Dll function twice?

7. Calling a function in a C++ Win32 Dll from VB 6

8. Calling a VB function from a C++ DLL

9. VB 4.0 calling a Visual C++ 4.0 DLL

10. Calling VB ActiveX DLL from Visual C++ problem

11. How to write Visual C++ DLL's and call them from Visual Basic

12. Calling DLL Function causes recursion of VB procedure

 

 
Powered by phpBB® Forum Software