Exception when calling Core Api from a Thread

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

Moderators: Tracker Support, TrackerSupp-Daniel, Chris - Tracker Supp, Vasyl-Tracker Dev Team, 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
Posts: 99
Joined: Fri Sep 18, 2015 9:49 pm

Exception when calling Core Api from a Thread

Post by mmasood » Mon Jul 23, 2018 9:13 pm


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?

(24.17 KiB) Downloaded 48 times

User avatar
Sasha - Tracker Dev Team
Posts: 4075
Joined: Fri Nov 21, 2014 8:27 am

Re: Exception when calling Core Api from a Thread

Post by Sasha - Tracker Dev Team » Tue Jul 24, 2018 6:23 am

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

Join us at Google+:
Subscribe at:

Roman - Tracker Supp
Power User
Posts: 263
Joined: Sun Nov 21, 2004 3:19 pm

Re: Exception when calling Core Api from a Thread

Post by Roman - Tracker Supp » Tue Jul 24, 2018 9:12 pm

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.
Tracker Software Products

Please ZIP any files uploaded to this forum or they will not be posted !
You may also use a RAR or 7Z archive format

Post Reply