Linking an exe with a VB app via C/C++ DLL

Hi

I'm trying to write a C/C++ DLL and a VB application that would receive events from an EXE. Basically: I am writing a DLL that connects to the exe as a plugin, receives events, processes some math and returns an array of results to the exe. that's done already, working perfectly. What I need to do now is to send a signal to a VB application when certain events/conditions are met in that DLL. Then VB app would do something else based on this signal . So it's EXE -> DLL -> VB.
I have been browsing the forums for sometime all I could find was how to call a function in DLL from VB. It seems to me that I need to reverse the process here. The DLL needs to call VB if some conditions are met within DLL. How would I go about it, what to look for, where to start, etc... ? I will appreciate any ideas, links, code, anything to get me started. Thanx in advance

Jaba
[910 byte] By [Jaba_z] at [2007-11-17 17:07:42]
# 1 Re: Linking an exe with a VB app via C/C++ DLL
ok, what I need is a way for VB application to intercept/receive an event that the C++ DLL creates. I found an example of a C++ application receiving events from a C++ DLL, but I'm completely clueless how to get these messages to a VB application. Since VB has to receive these events in timely fashion I can't write a loop that checks for changes in some shared variable inside the DLL. It has to be event driven. I will appreciate if you could point me in the right direction. Please help!

Jaba
Jaba_z at 2007-11-10 0:22:43 >
# 2 Re: Linking an exe with a VB app via C/C++ DLL
Yuo can use a CallBack System. For example if you want that a Vb SubRoutine Sub01 is executed when the condition in Dll if (a==1) ... you can do it in this way:

Define in Dll

typedef void (__stdcall *fun)(double param1,int param2);

fun FunctionCallBack=0;

void __stdcall SetCallBack(fun fz)
{
FunctionCallBack=fz;
}

void __stdcall ExeFunction()
{
...
...
if (a==1) & ( FunctionCallBack!=0) FunctionCallBack(0.001,a)
}

in your VB code, in a module define the CallBack

Declare Sub SetCallBack Lib "DllName.dll" (ByVal lpCallBack As Long)

sub Sub01 (param1 as double,param2 as long)
{
label1.caption=Param1 '' for exemple
label2.caption=Param2
}

and in your FormLoad (for example) :

Private Sub Form_Load()
.
.
.
SetCallBack AddressOf Sub01
.
.
.
end sub

In this way whe the condition (a==1) in dllfunction ExeFunction is rigth the function VB Sub01 is executed...

I hope i help yuo...
ringos75 at 2007-11-10 0:23:46 >
# 3 Re: Linking an exe with a VB app via C/C++ DLL
Why not write a C++ ActiveX control instead of the DLL? The ActiveX control could fire events, which can be handled by your VB app.
Markus W. at 2007-11-10 0:24:46 >
# 4 Re: Linking an exe with a VB app via C/C++ DLL
well, unfortunately it has to be a DLL. As you can read in my first post, i'm trying to link some other app with the VB one, and that app can only use DLLs as plugins, or that's my impression at least.

can I create ActiveX C++ control that would have all the export functions as a regular DLL? Or maybe I could add ActiveX to a regular DLL?
Jaba_z at 2007-11-10 0:25:51 >
# 5 Re: Linking an exe with a VB app via C/C++ DLL
ringos 75

thank you very much for your code. Unfortunately I couldn't make it work, "can't find DLL entry point SetCallback...". Funny, I have downloaded some other project from this website on callbacks from DLL, it gives me the same error after compiling. Yes, VB app can find the DLL.

any idea what can be wrong?

I needed to change the code a bit:

if (a==1 && FunctionCallBack!=0) FunctionCallBack(0.001,a);
Jaba_z at 2007-11-10 0:26:43 >
# 6 Re: Linking an exe with a VB app via C/C++ DLL
ok, I got it to work, or actually I took a code from MSDN and it is working. Thank you for pointing me in the right direction.

There's another problem however. I need to store the pointer to the callback function somewhere in the DLL, and that pointer must be accessible to all the instances of the DLL, Since they are actually plug-ins in the host aplication, there can be quote a few running at the same time. So the question is how to store that pointer in the shared block of data?

hmm... let me try to keep it as simple as possible. How could I make 1 VB application register it's callback function with a DLL, and then another (VB) application retrieve that 1st app's function address? For example, if the 1st app's callback function displays a message box, how could I (using DLL) make the message appear in app #1 by pressing a button in application #2?

If I can get an answer to that question, I'll be more than happy :)
Thanx in advance for all your help.

Jaba
Jaba_z at 2007-11-10 0:27:54 >
# 7 Re: Linking an exe with a VB app via C/C++ DLL
Sotty.. but i don't understand your second problem (my english is no good :( )) Can You do an example... ??
ringos75 at 2007-11-10 0:28:51 >