Events of VB handled in VC and vice versa

Posted on 2002-07-25
Last Modified: 2011-09-20
My problem is.
I have a VB activeX exe raising some events and which are required to be handled in VC++ component but i dont know how to do it.
I also want a VC++ component raising an event and which is handled in VB.
PLease give me some sample code as i already have gone through many theories but of no use so ssample code is required to perform both the tasks.
Question by:iamvikky

Expert Comment

ID: 7176650
What sort of data do you want to pass between the two exe's. If it is simply a pair of long values, then you could just Broadcast Windows messages. I did a similar thing between a C++ DLL and a VB Client application.

In both your applications you will register a windows message:

In VB app:-

lngEventMsgFromC++App = RegisterWindowMessage("MyWindowsEventMsgFromC++")
lngEventMsgToC++App = RegisterWindowMessage("MyWindowsEventMsgToC++")

Now you will also have to subclass this applications messages using (using APIs SetWindowLong(Hwnd,GWL_WNDPROC,lpPrevWndProc) ).

So in the WindowProc API you will have something like this:

If uMsg = lngEventMsgFromC++App Then
   ' This windows message has been fired by your C++ App
   ' so deal with it here, you can pass two parameters
   ' wParam and lParam, so you will have to design your
   ' own method of deciphering what you send in the event
End If

Now to raise an event in your VB program so that your C++ program will receive an event you will need to use the API

BroadcastSystemMessage(BSF_SENDNOTIFYMESSAGE + BSD_IGNORECURRENTTASK,lngRecipients, lngEventMsgToC++App, wParam, lParam)

I use this because it is system wide and my VB app was an NT service.

Now in your C++ application you will have to do the same (Select case the windows messages to the app)to receive messages, you must also register the same two windows message in your C++ app:

UINT uEventMsgToC++App
UINT uEventMsgFromC++App

uEventMsgToC++App = RegisterWindowMessage("MyWindowsEventMsgToC++")

 and when you want to raise an event to the VB app, you could use the following sub routine:

VOID BroadcastEvent(long wParam, long lParam)
   DWORD dwRec = 0;
   DWORD *lpdwRecipients = NULL;
   uEventMsgFromC++App = RegisterWindowMessage("MyWindowsEventMsgFromC++")

   lpdwRecipients = &dwRec;



---------- Hope this may have sparked some ideas ------


Author Comment

ID: 7176706
Thanks RainUK,
But I my problem is something else and is much simpler then what u thought.I'll explain u in detail.:
I have a VB activeX exe which raises an event with a Recordset(of ADO) having some some records picked from the database. Now my requirement is to handle that raised event in a VC++ component. I cannot change that VB ActiveX exe as it part of the existing system which is raising this event to all other VB clients . I want to introduce a VC client which can also handle the same event.

I tried raising an event with VB activeX dll and handling in VC it is working fine but I am not been able to do the same with VB active X exe and handling it in VC.
Now if you can help me in this......

Expert Comment

ID: 7176724
If the ActiveX EXE (The one you cannot change) can fire events which is trapped by other VB clients, then can you not create another VB client to receive those events and pass them onto the VC app?

Basically you create another VB.exe app which sits between the two components, in a sense a VB bit of middleware which handles messaging between the two components.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 7176760
my problem is related to event handling in VC not to look for work arounds ...secondly the kind of system i am working with requires much faster speed and by adding more layers that will not serve the purpose..
So if you can provide me with very basic event hadling in VC as we do in VB then its my requirement is just the same..i can provide you with the sample of ActiveX exe's (which is raising the event)code and if you can provide me with its client in VC..

just paste this code in the class module of activeX exe project of vb and then compile it to make exe..

Option Explicit

Public Event TimeUpdate(sTime As String)

Public Sub CallMeBack()
    RaiseEvent TimeUpdate(Time())
End Sub

Public Function ServerTime() As String
    ServerTime = Time
End Function

'''Code ends here ...

Now i want to trap this even in the VC component directly without any middleware...If you can help...


Expert Comment

ID: 7176815
Sorry I am out of ideas on this one. If you can't change the ActiveX exe in anyway which is raising the event. I am a VB programmer at heart so I suggest you try posing this question to the C++ programmers section of the forum.

Good luck

Expert Comment

ID: 7180029

What is you VC environment ?

If you are using MFC, try to add a class for your component "From a type library" with the MFC ClassWizard and select the .exe file.

LVL 49

Expert Comment

ID: 8040615
Hi iamvikky,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Refund points and save as a 0-pt PAQ.

iamvikky, Please DO NOT accept this comment as an answer.
EXPERTS: Post a comment if you are certain that an expert deserves credit.  Explain why.
DanRollins -- EE database cleanup volunteer

Accepted Solution

SpideyMod earned 0 total points
ID: 8095722
per recommendation

Community Support Moderator @Experts Exchange

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
String manipulation in Visual Basic 7 73
Updates not working for MS Windows 7 12 183
VB6 - Compare and highlight cell not the same 3 54
Macro Excel - Multiple If conditions 2 82
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question