2009-08-29 7 views
1

J'essaie d'exécuter un MathType dans une application C# ... en utilisant OLE dans des formulaires pour signifier les équations/images.MathType en C# (OLE)

Voici comment j'ai commencé avec le code. J'ai obtenu l'objet CLSID pour l'équation de type mathématique. Je crée une nouvelle instance et exécute un verbe pour démarrer le type mathématique. Cela fonctionne bien jusqu'à ce que j'essaie de définir ou d'obtenir des données de l'attribut IDataItem que j'ai.

code:

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

L'application se bloque maintenant avec cette exception:

System.Runtime.InteropServices.COMException était non gérée Message = "Structure non valide FORMATETC (Exception de HRESULT: 0x80040064 (DV_E_FORMATETC)) "Source =" Système "ErrorCode = -2147221404 StackTrace: à System.Runtime.InteropServices.ComTypes.IDataObject.GetData (format FORMATETC &, STGMEDIUM & moyen)

Je ne suis pas sûr de ce que cela veut dire, mais je pense qu'il pourrait avoir à faire avec formatEtc.cfFormat = (Int16)dataFormatMathMLPres.Id; parce que ID est et ne correspond 50000 pas dans un court (cfFormat est un court) de sorte qu'il déborde à quelque chose comme - 15000.

Répondre

2

J'ai résolu un problème similaire en le convertissant d'une valeur non signée en valeur signée. cela signifie que si la valeur (dataFormatMathMLPres.Id) est supérieure à 32767. Utilisez plutôt (dataFormatMathMLPres.Id - 65536). Il s'adaptera à court signé.

Questions connexes