Wierd com behavior

Posted on 2002-04-04
Last Modified: 2013-11-25
k, here is what I do, and I'd like to know why.

1. I create a stock COM project (dll) (lets call it TestProj) with 1 object in it that is both free threaded and custom interface (lets call it TestFC).

2. I create a stock COM project (exe) with no objects in it (lets call it TestRunProj).

3. #import "testproj.tlb" no_namespace, named_guids

4. I add the following code right before the msg loop in the exe.

     CComPtr<ITestFC> m_pTestThis;

          HRESULT hr = m_pTestLink.CoCreateInstance( CLSID_TestFC );

          if ( TRUE == FAILED( hr ) )
               ATLTRACE("Failed %x\n", hr );

     } // End try
     catch( _com_error e )
          HRESULT hr = e.Error();

          ATLTRACE("Com Error %x\n", hr );
     } // End catch

5. run it.

I ALWAYS get a failure to create the INTERFACE.  However, if I change either the free threading to apartment OR the custom interface to dual, it WORKS.  wtf is going on here.  Why in the world would this be happening?  This is even further disturbing because the final construct and final release both happen on the object.

Question by:Adept
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 6920078
Possibly you haven't got your proxies stubbed properly.

Check out the "Merging Proxy/Stub Pairs" section of

Author Comment

ID: 6920297
im not using any custom proxies or stubs, just the stuff that the atl com wiz setup.  :/

Accepted Solution

GGRUNDY earned 200 total points
ID: 6920365
I'm saying you DO need a proxy/stub dll, either in your dll or separately.

When you change the custom interface to dual then you get an IDispatch interface built into your project and COM can use the information in your type library to do automatic on-the-fly marshalling, so you get away without the need for proxy/stub code.

When you designate your threading model as "appartment", COM can run your Object in the same STA as you are calling the object from. But when you designate it as "free" com is forced to create a separte MTA and run your object in that, marshaling across the gap in the process (and regretting the absence of proxy/stubs that you are supposed to provide for that purpose).

You probably didn't mean "free" in anycase. "free" means:
I guarantee to correctly handle mutlithreaded access to all instance, static and global data, but I DON'T guarantee not to create other threads and pass them unmarshalled interface pointers.

If you acually guarantee to correctly handle multithreaded access to all instance, static and global data, but DO guarantee not to muck about creating treads yourself, then you should designate your model as "both". Thereby COM would not be forced to run your object in a MTA (which it may have had to create specially for that purpose), but can run your object in the STA from which it is created. This means that you won't need proxy/stubs (at least in that instance).

The basic answer is...
You have not taken any steps to create proxy/stubs for your dll, but in some instances you need them. So...

and follow the section headed
"The Single DLL configuration (or merged)"    
PAYING PARTICULAY ATTENTION TO "Step 2 - Edit project settings".

Author Comment

ID: 6920429

excellent.  that completely answers my question, thank you very much.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to event 3 123
Is there a simple front-end menu system. 9 146
Problem to App source 6 120
Developing a front end to SPLUNK 1 105
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

734 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