Click here to Skip to main content
15,896,207 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionMarshaling an Interface Pointer Pin
BuckBrown6-Feb-07 9:22
BuckBrown6-Feb-07 9:22 
AnswerRe: Marshaling an Interface Pointer Pin
PJ Arends6-Feb-07 10:21
professionalPJ Arends6-Feb-07 10:21 
GeneralRe: Marshaling an Interface Pointer Pin
BuckBrown6-Feb-07 11:39
BuckBrown6-Feb-07 11:39 
AnswerRe: Marshaling an Interface Pointer Pin
Roger Stoltz6-Feb-07 12:12
Roger Stoltz6-Feb-07 12:12 
GeneralRe: Marshaling an Interface Pointer Pin
BuckBrown6-Feb-07 13:10
BuckBrown6-Feb-07 13:10 
GeneralRe: Marshaling an Interface Pointer Pin
Roger Stoltz6-Feb-07 13:44
Roger Stoltz6-Feb-07 13:44 
GeneralRe: Marshaling an Interface Pointer Pin
BuckBrown7-Feb-07 8:11
BuckBrown7-Feb-07 8:11 
GeneralRe: Marshaling an Interface Pointer Pin
Roger Stoltz7-Feb-07 10:59
Roger Stoltz7-Feb-07 10:59 
Ok...

First, a couple of suggestions and things to consider:

1. I'm a little suspicious about your reasons for using a second thread. You're performing some kind of tests from your secondary thread. How do you know when a test has finished and what the result of the test was? Perhaps this could be done from the primary thread, it depends on how your tests works. I'm not saying that it's wrong, I'm just saying "think about it because it will save you a lot of trouble".

2. To me it would feel more natural if the primary thread was responsible for launching Excel as out-of-process automation server. I suspect that you should get a filename from the user which requires interfacing with the GUI and that shall always be done from the primary thead. It's also a common guideline for each thread that creates COM servers to process messages. In your case the Excel proxy would reside in your secondary thread which I suspect doesn't process messages. If you launched Excel from your primary thread, then you would have the message pump for free. Wink | ;)

3. If you continue with your solution to launch Excel from your secondary thread and let the primary thread do the saving stuff to Excel, it won't work if your secondary thread doesn't process messages. You risk facing a nasty deadlock if you try to exit your thread, calling ::CoUninitialize(), before the primary thread has released its interface to Excel. This is because the RPC service, which handles the marshalling between apartment boundaries, sends messages to the proxies and stubs in the different apartments. Sending a message means to wait until the receiving thread has processed the message and if it doesn't your sending thread will deadlock.


Now for how to set up the marshalling and the use of IID_IDispatch:
Declare an IStream* variable in your class. This is going to be used when you're calling ::CoMarshalInterThreadInterfaceInStream() and ::CoGetInterfaceAndReleaseStream().
The code from which you marshal an interface would look something like this:
// Assume your IStream* variable is called m_stream
HRESULT hr;
hr = ::CoMarshalInterThreadInterfaceInStream( __uuidof( IID_IDispatch ), lpDispatch, &m_stream );
if( FAILED( hr ) )
{
    // Do whatever you have to do in order to clean up and handle this error
}
Now the lpDispatch interface is ready to be fetched from another thread.
The code in the other thread would look something like this:
HRESULT hr;
IDispatch* lpDisp;
hr = ::CoGetInterfaceAndReleaseStream( m_stream, __uuidof( IID_IDispatch ), (void**)&lpDisp );
if( FAILED( hr ) )
{
    lpDisp = NULL;
    // Do whatever you have to do in order to clean up and handle this error
}
Now you have a marshalled IDispatch interface.


"It's supposed to be hard, otherwise anybody could do it!" - selfquote

GeneralRe: Marshaling an Interface Pointer Pin
BuckBrown8-Feb-07 10:11
BuckBrown8-Feb-07 10:11 
GeneralRe: Marshaling an Interface Pointer Pin
Roger Stoltz8-Feb-07 10:37
Roger Stoltz8-Feb-07 10:37 
QuestionMFC SDI and Dialog Boxes Pin
werpa6-Feb-07 9:03
werpa6-Feb-07 9:03 
GeneralRe: MFC SDI and Dialog Boxes Pin
prasad_som6-Feb-07 18:11
prasad_som6-Feb-07 18:11 
AnswerRe: MFC SDI and Dialog Boxes (it can be a little offtopic) Pin
Joan M6-Feb-07 20:41
professionalJoan M6-Feb-07 20:41 
AnswerRe: MFC SDI and Dialog Boxes Pin
#realJSOP7-Feb-07 2:02
professional#realJSOP7-Feb-07 2:02 
QuestionOnReceive stop being called [modified] Pin
jpyp6-Feb-07 7:30
jpyp6-Feb-07 7:30 
QuestionRe: OnReceive stop being called Pin
Mark Salsbery6-Feb-07 14:04
Mark Salsbery6-Feb-07 14:04 
QuestionRe: OnReceive stop being called Pin
Mark Salsbery6-Feb-07 14:12
Mark Salsbery6-Feb-07 14:12 
AnswerRe: OnReceive stop being called Pin
jpyp7-Feb-07 11:41
jpyp7-Feb-07 11:41 
GeneralRe: OnReceive stop being called Pin
Mark Salsbery7-Feb-07 12:05
Mark Salsbery7-Feb-07 12:05 
GeneralRe: OnReceive stop being called Pin
Mike O'Neill7-Feb-07 15:49
Mike O'Neill7-Feb-07 15:49 
GeneralRe: OnReceive stop being called Pin
jpyp8-Feb-07 9:35
jpyp8-Feb-07 9:35 
QuestionSingleton inheritance Pin
Waldermort6-Feb-07 7:20
Waldermort6-Feb-07 7:20 
AnswerRe: Singleton inheritance Pin
Cedric Moonen6-Feb-07 7:48
Cedric Moonen6-Feb-07 7:48 
GeneralRe: Singleton inheritance Pin
Waldermort6-Feb-07 8:05
Waldermort6-Feb-07 8:05 
GeneralRe: Singleton inheritance Pin
Cedric Moonen6-Feb-07 8:16
Cedric Moonen6-Feb-07 8:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.