Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Wierd com behavior

Posted on 2002-04-04
4
Medium Priority
?
196 Views
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;

     try
     {
          HRESULT hr = m_pTestLink.CoCreateInstance( CLSID_TestFC );

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

     } // 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.


0
Comment
Question by:Adept
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

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

Check out the "Merging Proxy/Stub Pairs" section of
http://www.microsoft.com/msj/defaultframe.asp?page=/msj/1097/visualprog1097.htm&nav=/msj/1097/newnav.htm
0
 

Author Comment

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

Accepted Solution

by:
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...

SEE
http://www.codeproject.com/com/macrotoposervers.asp
and follow the section headed
"The Single DLL configuration (or merged)"    
PAYING PARTICULAY ATTENTION TO "Step 2 - Edit project settings".
0
 

Author Comment

by:Adept
ID: 6920429

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…

886 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