none
Unable to cast COM object error when calling COM from .NET C# worker thread RRS feed

  • Question

  • I'm having an issue calling a COM object function from .NET C# client program. The COM object is written in C++ unmanaged. The problem only appears when I'm calling from a worker thread, it works fine if I call it from the UI thread. This is the error:

    Unable to cast COM object of type 'gvAgileFileReaderLib.gvAgileFileReaderClass' to interface type 'gvAgileFileReaderLib.IgvMpiRead'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{616B5C39-C7FB-4C8C-85D4-27559DD83304}' failed due to the following error: Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL)).

    I have tried the following methods but all of them fails:

    1) Create the COM object on UI thread, and call its function via its interface (IgvMpiRead) in the worker thread

    2) Create the COM object on the worker thread & access its function from worker thread

    3) Tried with worker thread set to STA & MTA

    I need to use the COM function is a worker thread to prevent a long processing from holding up my UI, but it just doesn't work when I try to call the COM function inside the worker thread.

    The weird thing is that doing the same from a unmanaged C++ client works fine (I can create the com object on UI, and then call its function from worker thread). Is this problem caused by interop issues? I'm using the interop that Visual Studio 2010 creates for me when I add reference to the C++ COM object.

     

     

     

     

    Tuesday, October 19, 2010 11:01 AM

Answers

  • hi Eric,

    We have resolved the issue. It wasn't due to an older manifest file. The COM module developer gave me a new manifest file where he added ThreadingModel "Both" in it. Previously it was undefined. I don't really understand how it worked though. I wasn't setting my worker thread to MTA, it was always on STA, but anyway that change did resolve the issue :) 

    • Marked as answer by eryang Monday, November 1, 2010 3:19 AM
    Tuesday, October 26, 2010 1:47 PM

All replies

  •  

    Hi,

     

    I cannot reproduce the issue, on both MTA and STA worker thread, the thread method works well, I just interop with Excel, my code is:

     

            static void Main(string[] args)

            {

                Thread t = new Thread(new ThreadStart(Do));

                t.SetApartmentState(ApartmentState.STA);

                t.Start();

     

                Console.WriteLine("Press any key to exit");

                Console.ReadLine();

            }

     

            static void Do()

            {

                Application app = new Application();

                app.Visible = true;

                Workbook book = app.Workbooks.Open(filePath);

     

                try

                {

                    Console.WriteLine("worker thread");

                }

                finally

                {

                    book.Close(true);

                    app.Quit();

                }

               

                Console.WriteLine("worker thread done");

            }

     

    Could you provide some relevant code snippets to help us reproduce this case?


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, October 20, 2010 7:26 AM
  • hi Eric,

    Thanks for the reply. We just found out that the issue was caused by the manifest file that we embbed into the C# program (for reg-free COM). The unmanaged C++ client didn't have this problem as I was using normal dll registration method when testing with that. After I remove the manifest from the C# program, it worked fine... I was able to create and use the COM function in worker thread.

    I think our manifest is broken or I might be using an older version, sorry about the false alert! I'm going to test the latest manifest file and see if the issue is still there. However it's still weird that it worked fine in UI thread, but not in worker thread (with the earlier manifest). I guess anything can go wrong as the dll is 'partially' registered? I'll share again my result after testing with the latest manifest file.

    Thursday, October 21, 2010 1:20 AM
  • Hi,

    May I know the issue status? please feel free to let us know if you have any concern.


    Sincerely,
    Eric
    MSDN Subscriber Support in Forum
    If you have any feedback of our support, please contact msdnmg@microsoft.com.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, October 26, 2010 3:26 AM
  • hi Eric,

    We have resolved the issue. It wasn't due to an older manifest file. The COM module developer gave me a new manifest file where he added ThreadingModel "Both" in it. Previously it was undefined. I don't really understand how it worked though. I wasn't setting my worker thread to MTA, it was always on STA, but anyway that change did resolve the issue :) 

    • Marked as answer by eryang Monday, November 1, 2010 3:19 AM
    Tuesday, October 26, 2010 1:47 PM