Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.…

688 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