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.

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...

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Concatenate 2 userform comboboxes to a userform text box 4 56
String manipulation in Visual Basic 7 59
Add a task in Outlook from access 11 35
TT Auto Dashboard 13 82
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

914 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now