IOperation in C# - some help neded  SOLVED

PDF-XChange Editor SDK for Developers

Moderators: PDF-XChange Support, Daniel - PDF-XChange, Chris - PDF-XChange, Sean - PDF-XChange, Paul - PDF-XChange, Vasyl - PDF-XChange, Ivan - Tracker Software, Stefan - PDF-XChange

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.
zarkogajic
User
Posts: 1527
Joined: Thu Sep 05, 2019 12:35 pm

IOperation in C# - some help neded

Post by zarkogajic »

Hi Support,

This topic is about me trying to make a simple IOperation implementation in C# to provide you with some code related to a few topics I raised:
https://forum.pdf-xchange.com/viewtopic.php?f=66&t=35299
https://forum.pdf-xchange.com/viewtopic.php?f=66&t=35252

Now, I have it done in Delphi (all working as expected how I have it there), but I guess you would prefer C# - so this is what I'm trying to do - make a simple IOperation implementation in C#.

Now, it's been a while since I did some real coding in C#, and it seems I've gotten rusty :)

I know you do not help with writing code, but please just push me in the right direction - so I can finish the sample code and ask questions related to it.

Here's what I have:

A TaskWait is a simple IOperation implementation just waiting in its Do() method:

Code: Select all

    class TaskWait : PDFXEdit.IOperation
    {
        TaskRunner taskRunner;

        public TaskWait(TaskRunner tr)
        {
            taskRunner = tr;
        }

        public void Do(int nFlags = 0)
        {
            int myStrID = taskRunner.PXVInst.Str2ID("mystr"); //[b]EXCEPTION HERE[/b]

            System.Threading.Thread.Sleep(100 * (new Random().Next(1, 3)));
        }

        public void ShowSetupUI(uint hWndParent, int nFlags = 0)
        {
            throw new NotImplementedException();
        }

        public IOpInputItem CreateInputItem(object pSrc)
        {
            throw new NotImplementedException();
        }

        public int ID => throw new NotImplementedException();

        public ICab Params => throw new NotImplementedException();

        public COMThreadingModel COMThreadingModel => COMThreadingModel.COMThreadingModel_Any;
    }
 
This class is used in a TaskRunner class with an idea to run multiple such tasks in parallel:

Code: Select all

    class TaskRunner
    {
        public int TotalRunCount = 0;

        public PDFXEdit.IPXV_Inst PXVInst;
        
        public TaskRunner(int runCount, IPXV_Inst inst)
        {
            TotalRunCount = runCount;
            PXVInst = inst;

            for (int i = 0; i < runCount; i++)
            {
                TaskWait tw = new TaskWait(this);

                inst.AsyncDoAndWaitForFinish(tw);
            }

            MessageBox.Show("done");
        }
    }
And finally, trying to call this in Full Demo via some button, on MainFrm, click like:

Code: Select all

private void btnTaskRun_Click(object sender, EventArgs e)
{
	new TaskRunner(5, pdfCtl.Inst);
}
And now the problem:

In the implementation of the Do() method in TaskWait I'm trying to access PXVInst from TaskRunner as simple as :

Code: Select all

int myStrID = taskRunner.PXVInst.Str2ID("mystr");
This fails at run time with an exception:
System.InvalidCastException
HResult=0x80004002
Message=Unable to cast COM object of type 'PDFXEdit.PXV_InstClass' to interface type 'PDFXEdit.IPXV_Inst'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{D726366D-34D6-49FC-A341-7B84C54CCA3E}' failed due to the following error: Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE)).
Source=mscorlib
StackTrace:
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
at PDFXEdit.PXV_InstClass.Str2ID(String sStr, Boolean bAddIfNotExists)
at FullDemo.TaskWait.Do(Int32 nFlags) in D:\pdfxchangetest\PDFEditorSDKExamples-master\CSharp\FullDemo\Task.cs:line 58
This happens when an instance of TaskWait is executed via AsyncDoAndWaitForFinish. If executed in the same (/ main thread) by simply calling tw.Do() - all works.

So , any help would be much appreciated :)

p.s.
Seems I must do some kind of casting - but am not sure what kind.

-žarko
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: IOperation in C# - some help neded

Post by Sasha - PDF-XChange »

Hello zarkogajic,

Nope, you did not miss a thing - if you try that in the main thread - everything will work as intended. Something has to do with C# COM handling. What you can try is invoking such a problem methods from the main thread.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
zarkogajic
User
Posts: 1527
Joined: Thu Sep 05, 2019 12:35 pm

Re: IOperation in C# - some help neded

Post by zarkogajic »

Hi Alex,

LOL. That's why I use Delphi ;)

Meaning: in C# I cannot implement an IOperation to be used with AsyncDo()?

p.s.
Yes, in the main thread, simply calling taskWait.Do() - all works.

-žarko
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: IOperation in C# - some help neded

Post by Sasha - PDF-XChange »

Hello zarkogajic,

Well some time ago, one could not implement the IUIX_ListCallbacks due to some similar error. As I said, COM handling in .Net is not it's strong side.
You can implement an operation with AsyncDo(), though as you see, some interfaces can behave inadequate. I stumbled upon few years ago when I tried multi-thread work of some operations. The thing is that some interfaces do work and some don't. I just obtained all of the needed data beforehand and passed that to the operation.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
zarkogajic
User
Posts: 1527
Joined: Thu Sep 05, 2019 12:35 pm

Re: IOperation in C# - some help neded

Post by zarkogajic »

Hi Alex,
I just obtained all of the needed data beforehand and passed that to the operation.
Well, the "only" thing I would need is the IProgressMon, if I pass that in TaskWait's constructor (sending Inst.DefaultProgressMon) then when I try to use it, in the Do() method via AsyncDo, I get:
System.InvalidCastException
HResult=0x80004002
Message=Unable to cast COM object of type 'System.__ComObject' to interface type 'PDFXEdit.IProgressMon'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{E663B47E-D10C-4690-B1EE-1BDB10C73EBE}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Source=mscorlib
StackTrace:
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
at PDFXEdit.IProgressMon.SetButtonTitle(String sButtonTitle)
at FullDemo.TaskWait.Do(Int32 nFlags) in D:\pdfxchangetest\PDFEditorSDKExamples-master\CSharp\FullDemo\Task.cs:line 62
So, it would seem IProgressMon is one such interface - I cannot get my hand on it in the Do() when called asnyc.

If that's it in C# - unfortunately I cannot do anything with it :(

-žarko
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: IOperation in C# - some help neded

Post by Sasha - PDF-XChange »

Hello zarkogajic,

Does that work correctly in Delphi?

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
zarkogajic
User
Posts: 1527
Joined: Thu Sep 05, 2019 12:35 pm

Re: IOperation in C# - some help neded

Post by zarkogajic »

Hi Alex

Yes.

Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

Re: IOperation in C# - some help neded

Post by Sasha - PDF-XChange »

Hello zarkogajic,

I see. Well, as I said before - .Net sometimes gives us troubles with COM.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
zarkogajic
User
Posts: 1527
Joined: Thu Sep 05, 2019 12:35 pm

Re: IOperation in C# - some help neded  SOLVED

Post by zarkogajic »

Hi Alex,

Ah, then nothing else I can do except closing this topic .. until some brighter time for C#.

p.s.
Anyhow, the reason I wanted to do this is is the "problem" that in my IOperation Delphi implementation the IProgresMon's dialog - when it is displayed (during AsyncDo calls) - it is not a modal dialog - as it seems the case is when I execute any of standard op.something operations.

However I have found a way to solve this by injecting my code into the message loop and "eating" all messages from WM_MOUSEFIRST to WM_MOUSELAST except WM_MOUSEMOVE while the "DlgProgress" window is displayed (of course not ignoring for that window). I also do FlashWindowEx it so it appears as if modal dialog. And all ok. :)

-žarko
Sasha - PDF-XChange
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am

IOperation in C# - some help neded

Post by Sasha - PDF-XChange »

:)
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ