views:

83

answers:

1

Hi,

I'm trying to use use the LoadInk method of an mathinputcontrol but I can't figure out where to create the IIDispInk object from, as it just appears to be an interface.

http://msdn.microsoft.com/en-us/library/dd372605(VS.85).aspx

Any guidance would be highly appreciated.

Thanks :)

Edit: for clarity, here is my code so far [edit 2: by "so far", I mean of what's been added. Pretty much all the rest of my code can be found on SO under how to create the MIC in C#] (thanks to Hans Passant)

MSINKAUTLib.InkDispClass loadInkTest = new MSINKAUTLib.InkDispClass();

Stream stream = File.Open("C:\\Tim\\bytes.isf", FileMode.Open);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
loadInkTest.Load(bytes);


ctrl.LoadInk((micautLib.IInkDisp)loadInkTest);

Unfortunately this throws exactly the same exception

Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

C:\Tim\bytes.isf contain bytes saved from an InkPicture control which loads and saves this file file OK, so I assume that because the loadInkTest.Load() method did not throw an exception (normally it is not shy to do so) that it loaded the data OK. If there is a suggestion on a better (or more obvious) place to get the bytes, please let me know.

+1  A: 

Use Project + Add Reference, Browse tab. Navigate to c:\program files\common files\microsoft shared\ink and select InkObj.dll. You can now create an instance of MSINKAUTLib.InkDispClass. It implements IInkDisp and has Save and Load methods.

You will have to cast the object to micautLib.IInkDisp, the interfaces come from different type libraries. And the clincher, you have to call the MathInputControl's Show() method first before using LoadInk(). Error reporting is miserable, everything is E_UNEXPECTED. The code I got to work:

        var ctl = new micautLib.MathInputControl();
        var ink = new MSINKAUTLib.InkDisp();
        ink.Load(System.IO.File.ReadAllBytes("c:\\temp\\test.isf"));
        var iink = (micautLib.IInkDisp)ink;
        ctl.Show();
        ctl.LoadInk(iink);

Plus event handlers for the Insert and Close event. And the glue to get the window in the right place.

Also beware that the micautLib type library has a dependency on the bit-ness of the machine. The troublemaker is the SetOwnerWindow() method, you really want to use it to prevent the dialog from disappearing behind another window. Its argument is declared as LONG_PTR, a type that is 32-bits on a 32-bit operating system, 64-bits on x64. The window handle. When you use Visual Studio, you will always get the 32-bit version of that method since VS is a 32-bit program.

If you plan on supporting 64-bit operating systems then you will have to build a separate version of your program. Starting with running the 64-bit version of Tlbimp.exe (not Visual Studio) to create the interop wrapper. So that the argument will be a 64-bit value and compatible with the window Handle you pass to the method.

Ah, the joys of COM. No real accident that this wasn't wrapped by Microsoft.Ink.dll :)

Hans Passant
Thanks for your answer :) Unfortunately I tried this as suggested: MSINKAUTLib.InkDispClass loadInkTest = new MSINKAUTLib.InkDispClass();ctrl.LoadInk(loadInkTest); but it says The best overloaded method match for 'micautLib.IMathInputControl.LoadInk(micautLib.IInkDisp)' has some invalid arguments and Argument 1: cannot convert from 'MSINKAUTLib.InkDispClass' to 'micautLib.IInkDisp'. If I cast it, it throws a "COMException was unhandled". Any ideas? Is there another way of building the MathIputControl without the micautLib library?
Tim Green
Tell us more about the COMException, a message or HRESULT at least.
Hans Passant
Terribly sorry: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))
Tim Green
Partial stack trace incase it helps: at micautLib.MathInputControlClass.LoadInk(IInkDisp Ink) at LatexTablet.net.Program.Main(String[] args
Tim Green
Well, I didn't expect that either. Did you actually use the loadInkTest.Load() method?
Hans Passant
I tried that too, I've updated the main question with more code and some details as I think it would be easier to read up there. Thank you so much for your time!
Tim Green
Check the Note at the bottom http://msdn.microsoft.com/en-us/library/bb416546.aspx Try calling Save first.
Hans Passant
When I say I love you: I mean it. I can only award the bounty in 4 hours though, so I'll do it then. Thanks so much.
Tim Green
You'll love me more, I found the problem. You have to call ctrl.Show() first. High suck factor.
Hans Passant
Top job man, top job! Out of interest, how did you find that out?! Trial and error, natural geniusness, or epic googling ability?
Tim Green
I found the MSDN page. Quote: "This method will only work when the control is visible". http://msdn.microsoft.com/en-us/library/dd372605%28VS.85%29.aspx
Hans Passant
Infinitely many facepalms do not describe how I feel right now. Thanks, once again!
Tim Green