Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Wierd com behavior

Posted on 2002-04-04
Medium Priority
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

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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

578 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