Exception when calling Core Api from a Thread

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
mmasood
User
Posts: 101
Joined: Fri Sep 18, 2015 9:49 pm

Exception when calling Core Api from a Thread

Post 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
Attachments
TrackerTest.rar
(24.17 KiB) Downloaded 207 times
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Exception when calling Core Api from a Thread

Post 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
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
User avatar
Roman - Tracker Supp
Site Admin
Posts: 303
Joined: Sun Nov 21, 2004 3:19 pm

Re: Exception when calling Core Api from a Thread

Post 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.
shefnic
User
Posts: 1
Joined: Fri Aug 16, 2019 3:33 pm
Location: Kuwait
Contact:

Exception when calling Core Api from a Thread

Post 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.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Exception when calling Core Api from a Thread

Post by Sasha - Tracker Dev Team »

Hello shefnic,

Glad that you have found a working solution for you.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply