C#中的MathType(OLE)

时间:2009-08-29 01:17:08

标签: c# com ole mathml mathtype

我正在尝试在C#应用程序中运行MathType ...在表单中使用OLE来表示方程式/图像。

这就是我开始使用代码的方式。 我得到了数学类型方程的CLSID对象。我创建一个新实例并运行动词来启动数学类型。这很有效,直到我尝试设置或获取我拥有的IDataItem属性的数据。

代码:

string progID = "Equation.DSMT4";
comRetVal= CLSIDFromProgID(progID, out eqContainerGUID);
Type t = Type.GetTypeFromProgID(progID); //ok-> MT6 Equation
Object instance = Activator.CreateInstance(t);

IDataObject oleDataObject = instance as IDataObject;
MTSDKDN.MathTypeSDK.IOleObject oleObject = instance as IDataObject;

//run verb Run For Conversion - I'm not sure what this verb does
oleObject.DoVerb(2, (IntPtr)0, activeSite, 0, (IntPtr)this.Handle, new MathTypeSDK.COMRECT());

//up to here everything is find

//Now say I want to put a MathML string into the IDataObject
//set format 
formatEtc.cfFormat = (Int16)dataFormatMathMLPres.Id; //<-this overflows. I verified that the only format that works is Presentation MAthML
formatEtc.dwAspect = System.Runtime.InteropServices.ComTypes.DVASPECT.DVASPECT_CONTENT;
formatEtc.lindex = -1;
formatEtc.ptd = (IntPtr)0;
formatEtc.tymed = TYMED.TYMED_HGLOBAL;

//set medium
ConnectSTGMEDIUM stgMedium = new ConnectSTGMEDIUM();
string mathEqn = "<math><mi>x</mi></math>";
stgMedium.unionmember = Marshal.StringToHGlobalAuto(mathEqn);
stgMedium.pUnkForRelease = 0;

//if now i write the equation to console from STGMEDIUM i see that after each char there is a null. Is this normal?

//now I try to set data in IDataObject and the OLE object
//I thought this set the data of the ole object to the MathML string I put in STGMEDIUM
oleDataObject.SetData(ref formatEtc, ref stgMedium, false);

该应用程序现在因此异常而崩溃:

  

System.Runtime.InteropServices.COMException未处理Message =“无效的FORMATETC结构(来自HRESULT的异常:0x80040064(DV_E_FORMATETC))”Source =“System”ErrorCode = -2147221404 StackTrace:at System.Runtime.InteropServices.ComTypes.IDataObject .GetData(FORMATETC&amp; format,STGMEDIUM&amp; medium)

我不确定这意味着什么,但我认为这可能与此有关 formatEtc.cfFormat = (Int16)dataFormatMathMLPres.Id; 因为那个ID是50000并且不适合短(cfFormat很短)所以它溢出到-15000之类。

1 个答案:

答案 0 :(得分:2)

我通过将其从无符号值转换为有符号值来解决类似问题。这意味着如果值(dataFormatMathMLPres.Id)大于32767.请改用(dataFormatMathMLPres.Id - 65536)。它符合签名的简短。