Page 1 of 1

Exception when calling Core Api from a Thread

Posted: Mon Jul 23, 2018 9:13 pm
by mmasood
Hi,

I am trying to use Core Api (7.0.325.1) from a thread in C# and I am getting exceptions when I try to run it:

"Unable to cast COM object of type 'System.__ComObject' to interface type 'PDFXCoreAPI.IPXS_Inst'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{9B44A054-D66C-4AC6-90DB-2E23FBB67EF5}' failed due to the following error: Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE))."

I have created a small sample project that shows this error. Can you please take a look and let me know of what is missing that is causing this exception?

Regards,
M

Re: Exception when calling Core Api from a Thread

Posted: Tue Jul 24, 2018 6:23 am
by Sasha - Tracker Dev Team
Hello mmasood,

We will try to investigate this. If you were using threads then you could have changed the apartment state so that everything would work correctly:

Code: Select all

    Thread t = new Thread(o);
        t.SetApartmentState(ApartmentState.STA);
        t.Start();

Cheers,
Alex

Re: Exception when calling Core Api from a Thread

Posted: Tue Jul 24, 2018 9:12 pm
by Roman - Tracker Supp
Hello mmasood,

The problem occurs because you try to call PDF-XChange SDK interface method (IPXC_Inst::GetExtension) from a COM apartment that is different from the one PXC_Inst in created in.
Cross-apartment call requires marshalling and PDF-XChange SDK interfaces are generally not marshallable.
Specifically you create PXC_Inst object in the application main thread that is marked as STA (notice [STAThread] attribute of your TrackerTest.Program.Main function). When you create a new thread it becomes associated with another STA apartment (there can be only one thread per STA apartment - STA means "single thread apartment").
Another mistake is that you do Threading.Thread.Sleep in the main thread after delegating access to the PXC_Inst object (IPXC_Inst::GetExtension) to a worker thread. This prevents marshalling to the main thread. STA thread must do message pumping while waiting. This is a general COM requirement, regardless of marshalling capabilities of our interfaces.

So there are several ways to solve this problem. You can either make all your work with PDF-XChange SDK in the same STA thread or all the threads that access the same PDF-XChange SDK object must run in the MTA apartment.
For example you can change your TrackerTest.Program.Main function attribute from [STAThread] to [MTAThread]. In this case all your application threads will run in the MTA.

Exception when calling Core Api from a Thread

Posted: Tue Aug 27, 2019 5:50 pm
by shefnic
Grand Poobah

The method you have mentioned above works. When I use MQOP_START_WAIT on MQCTL it users our main thread rather than creating a one of its own.

Re: Exception when calling Core Api from a Thread

Posted: Wed Aug 28, 2019 6:43 am
by Sasha - Tracker Dev Team
Hello shefnic,

Glad that you have found a working solution for you.

Cheers,
Alex