我正在尝试在 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="Invalid FORMATETC 结构(来自 HRESULT 的异常:0x80040064 (DV_E_FORMATETC))" Source="System" ErrorCode=-2147221404 StackTrace:在 System.Runtime.InteropServices.ComTypes.IDataObject.GetData( FORMATETC& 格式,STGMEDIUM& 中等)
我不确定这意味着什么,但我认为这可能与
formatEtc.cfFormat = (Int16)dataFormatMathMLPres.Id;
该 ID 是 50000 并且不适合短(cfFormat 是短)有关,因此它会溢出到 -15000 之类的东西。