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
  • 2
  • 2

Expert Comment

Comment Utility
Possibly you haven't got your proxies stubbed properly.

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

Author Comment

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

Accepted Solution

GGRUNDY earned 200 total points
Comment Utility
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

Comment Utility

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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.…

772 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

15 Experts available now in Live!

Get 1:1 Help Now