I have a rountine in an ActiveX control that is called from another thread (made by some other DLL). I want to be able to raise an event ('Error' in this case which takes a long and a string as argument) which will be caught by the VB6 app which will contain this ActiveX control. Unless I'm mistaken, the reason my VB6 app keeps crashing is because I'm not performing any kind of thread safety. Can someone please let me know what I should be doing here (or if my assumption is wrong)?
void __stdcall MyActiveXCtrl::OnReplyAudio(LPVOID pData)
//this next line calls a DLL which will in turn call this rountine againif (!m_audioHardware->RequestAudio())
After some debugging, I found that the reason it was crashing out was because a FileSystemObject I had in the vb6 code was not playing nice on the thread that raised the Error event. When I take that out, the app stop crashing.
If I was in .Net, I'm sure I could do this stuff with delegates, but I'm not in .Net.
My question still remains, does anyone know the best way in an MFC ActiveX control to raise an event on another thread?
does anyone know the best way in an MFC ActiveX control to raise an event on another thread?
There's only one correct way, even though it may look differently depending on the implementation: marshalling.
"Raising an event" in fact means calling an interface implemented by the client from the server. The same rules apply as when calling an interface implemented by the server from a client.
As always, when crossing apartment boundaries you have to marshal the interface. Since the secondary thread in your case is another apartment, you have to marshal the client interface to the secondary apartment.
You also have to make sure you set up the secondary apartment properly by calling ::CoInitialize() from that thread before doing any COM related stuff and consequently call ::CoUninitialize() before exiting the thread.
Marshalling the client interface, or the "event sink", can be done using the GIT (Global Interface Table). You add an interface to the GIT that you want to marshal and get it in the apartment where you want to use it. When you're done with the interface you revoke it from the GIT.
Read more about the GIT here[^].
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
hello everybody in this forum . i would like to ask one question about calling com file from c++
i have a com file that is written in c++ also , i need to import that dll and then create object from one of its object classes and invoke its methods , how can i import and initialize Com files in c++ to be able to call its objects and methods in my c++ project ?
I am currently writing an application using C++ and MFC. As you might expect in the method InitInstance, I call new to create an instance of the main window class. I assign this pointer to m_pMainWnd. However, I am never freeing the object being created there. I feel this is a problem.
I plan to delete the object in the routine ExitInstance. I would think this would be standard practice but I have not seen any examples in my MFC books that do this. I am wondering why. Could it be that these authors do not do this because they feel it is does not matter since the application will terminate anyway? I kind of feel that they should.
I plan to delete the object in the routine ExitInstance
I would think this would be standard practice but I have not seen any examples in my MFC books that do this. I am wondering why. Could it be that these authors do not do this because they feel it is does not matter since the application will terminate anyway?
Personally, I use smart pointers[^] to hold pointers. For this case, I might well use a boost::scoped_ptr, which would deallocate the memory in the CWinApp destructor.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
It is actually third party rtd server .I linked to it via excel and getting the data in excel file .
data is updating within milliseconds .I see the rtd formula on the cell. I donot know it is excel file or stream of data . please suggest the solution for both condition .My AIM is to store every updated data in the database .
Hai in my application i have to display time taken for execution, at present i am using GetTickCount (), this function gives time in milliseconds, i need more detailed result i.e in microseconds, is there any similiar function?
Thanks, but the way I have the program set up is hWnd refers to a hidden window which allows me to get a system tray. hWndConsole refers to the console window (which is external to my program). I want to close hWnd when hWndConsole is closed.
Any ideas on how to do this? Sorry for the confusing post.
Alternatively, you can wait inside a thread on an infinite loop, checking for the window (HWND) existence on a regular-interval of your choice with the help of Sleep(nMilliSecs) + IsWindow() and do a PostQuitMessage() to your application's main window after the said window (HWND) is closed.
This way, you can avoid all the ugly manual message translation and pumping stuff.
It is a crappy thing, but it's life -^Carlo Pallini