protected void AddParams(FUNCDESC funcDesc, String[] names, int paramCount) { IntPtr elemPtr = funcDesc.lprgelemdescParam; for (int i = 0; i < paramCount; i++) { ELEMDESC elemDesc = (ELEMDESC)Marshal.PtrToStructure(elemPtr, typeof(ELEMDESC)); ComParamInfo pi = new ComParamInfo (names[i + 1], TypeLibUtil.TYPEDESCToString (_typeLib, _typeInfo, elemDesc.tdesc, TypeLibUtil.COMTYPE), elemDesc.desc.paramdesc.wParamFlags); _parameters.Add(pi); // Point to the next one elemPtr = new IntPtr(elemPtr.ToInt64() + Marshal.SizeOf(elemDesc)); } }
internal ComTypeDefInfo(TypeLibrary typeLib, TYPEKIND typeKind, int index) : base(typeLib, typeKind, index) { TYPEATTR typeAttr; IntPtr typeAttrPtr; _typeInfo.GetTypeAttr(out typeAttrPtr); typeAttr = (TYPEATTR)Marshal.PtrToStructure(typeAttrPtr, typeof(TYPEATTR)); _infoType = "TypeDef"; if (TraceUtil.If(this, TraceLevel.Verbose)) { Trace.WriteLine("TypeDefInfo: " + _name); } _varComType = TypeLibUtil.TYPEDESCToString(typeLib, _typeInfo, typeAttr.tdescAlias, TypeLibUtil.COMTYPE); _varClrType = TypeLibUtil.TYPEDESCToString(typeLib, _typeInfo, typeAttr.tdescAlias, !TypeLibUtil.COMTYPE); _typeInfo.ReleaseTypeAttr(typeAttrPtr); // Add to the typelibrary for resolution typeLib.TypeDefHash.Add(_name, this); }
protected void AddDomParams(FUNCDESC funcDesc, CodeMemberMethod meth, int limit) { IntPtr elemPtr = funcDesc.lprgelemdescParam; for (int i = 0; i < limit; i++) { ELEMDESC elemDesc = (ELEMDESC)Marshal.PtrToStructure(elemPtr, typeof(ELEMDESC)); ComParamInfo parameter = (ComParamInfo) _parameters[i]; String paramType = TypeLibUtil.TYPEDESCToString (_typeLib, _typeInfo, elemDesc.tdesc, !TypeLibUtil.COMTYPE); String paramInOut = null; if ((parameter._paramFlags & PARAMFLAG.PARAMFLAG_FIN) != 0) { paramInOut = "In"; } else if ((parameter._paramFlags & PARAMFLAG.PARAMFLAG_FOUT) != 0) { paramInOut = "Out"; // Ref becomes out for an output parameter if (paramType.StartsWith("ref ")) { paramType = "out " + paramType.Substring(4); } else { paramType = "out " + paramType; } } CodeParameterDeclarationExpression paramExpr = new CodeParameterDeclarationExpression (paramType, parameter._name); if (paramInOut != null) { paramExpr.CustomAttributes.Add (new CodeAttributeDeclaration ("System.Runtime.InteropServices." + paramInOut)); } meth.Parameters.Add(paramExpr); // Point to the next one elemPtr = new IntPtr(elemPtr.ToInt64() + Marshal.SizeOf(elemDesc)); } }
protected override CodeObject GenerateCodeDom() { IntPtr varDescPtr; CORRECT_VARDESC varDesc; CodeMemberField codeDom; codeDom = new CodeMemberField(); _typeInfo.GetVarDesc(_index, out varDescPtr); varDesc = (CORRECT_VARDESC) Marshal.PtrToStructure(varDescPtr, typeof(CORRECT_VARDESC)); codeDom.Name = Name; codeDom.Attributes = MemberAttributes.Public; if (_typeKind == TYPEKIND.TKIND_ENUM) { try { Object value = Marshal.GetObjectForNativeVariant (varDesc.u.lpvarValue); codeDom.InitExpression = new CodePrimitiveExpression(value); } catch { // Ignore, was handled earlier } } TYPEDESC typeDesc = varDesc.elemdescVar.tdesc; codeDom.Type = new CodeTypeReference (TypeLibUtil.TYPEDESCToString(_typeLib, _typeInfo, typeDesc, !TypeLibUtil.COMTYPE)); _typeInfo.ReleaseVarDesc(varDescPtr); return(codeDom); }
protected override CodeObject GenerateCodeDom() { CodeTypeMember codeDom; IntPtr funcDescPtr; FUNCDESC funcDesc; // Have to get the FUNCDESC from the _typeInfo because // it cannot be saved. The reason for this is that a TYPEDESC // may have a pointer to another TYPEDESC which is allocated // in the unmanaged memory and that pointer won't be any good // outside of the scope where the FUNCDESC is held. _typeInfo.GetFuncDesc(_index, out funcDescPtr); funcDesc = (FUNCDESC)Marshal.PtrToStructure(funcDescPtr, typeof(FUNCDESC)); if (funcDesc.invkind == INVOKEKIND.INVOKE_FUNC) { CodeMemberMethod meth; meth = new CodeMemberMethod(); codeDom = meth; TYPEDESC retType = funcDesc.elemdescFunc.tdesc; if (_parameters.Count > 0) { int limit = _parameters.Count; // If the last parameter is a retval and the // function returns an HRESULT, then make // the function return the last parameter if ((VarEnum)funcDesc.elemdescFunc.tdesc.vt == VarEnum.VT_HRESULT) { ComParamInfo lastParam = (ComParamInfo) _parameters[_parameters.Count - 1]; if ((lastParam._paramFlags & PARAMFLAG.PARAMFLAG_FRETVAL) != 0) { IntPtr elemPtr = funcDesc.lprgelemdescParam; ELEMDESC elemDesc = new ELEMDESC(); // Point to the last one elemPtr = new IntPtr(elemPtr.ToInt64() + ((_parameters.Count - 1) * Marshal.SizeOf(elemDesc))); elemDesc = (ELEMDESC) Marshal.PtrToStructure(elemPtr, typeof(ELEMDESC)); // Make the return type the last parameter's retType = elemDesc.tdesc; limit--; } } // Only add up to the limit // (may omit the last paremeter) AddDomParams(funcDesc, meth, limit); } // HRESULT becomes void because its handled by the exception // mechanism, we just leave the return type null if ((VarEnum)retType.vt != VarEnum.VT_HRESULT) { String typeName = TypeLibUtil.TYPEDESCToString (_typeLib, _typeInfo, retType, !TypeLibUtil.COMTYPE); // Get rid of the ref since this is now a return type if (typeName.StartsWith("ref ")) { typeName = typeName.Substring(4); } if (TraceUtil.If(this, TraceLevel.Info)) { Trace.WriteLine(this, "CG - " + Name + " return: " + typeName); } meth.ReturnType = new CodeTypeReference(typeName); } } else { CodeMemberProperty prop; prop = new CodeMemberProperty(); codeDom = prop; prop.Type = new CodeTypeReference (TypeLibUtil.TYPEDESCToString (_typeLib, _typeInfo, funcDesc.elemdescFunc.tdesc, !TypeLibUtil.COMTYPE)); } codeDom.Name = Name; codeDom.Attributes = MemberAttributes.Public; _typeInfo.ReleaseFuncDesc(funcDescPtr); return(codeDom); }
internal ComMemberInfo(BasicInfo parent, TYPEKIND typeKind, UCOMITypeInfo typeInfo, int index, bool dispatch, FUNCDESC funcDesc) : base(typeInfo) { int actLen; String[] memberNames = new String[100]; _typeInfo.GetNames(funcDesc.memid, memberNames, memberNames.Length, out actLen); _memberId = funcDesc.memid; // the high order part of the memberId is flags // for a dispatch interface if (dispatch) { _memberId &= 0xffff; // Make sure we get a 16 bit integer so that negative // DISPIDs show up correctly _memberId = (Int16)_memberId; } String docString; // Note, use the original unmasked memberId _typeInfo.GetDocumentation(funcDesc.memid, out _name, out docString, out _helpContext, out _helpFile); // Set using property so that nulls are checked DocString = docString; _parameters = new ArrayList(); _typeLib = parent.TypeLib; _index = index; _container = parent; _printName = _name; _nameKey = (String)_name; // Add the DISPID to the dispatch interfaces if (dispatch) { _dispatch = true; _printName += " - " + _memberId; } if (TraceUtil.If(this, TraceLevel.Verbose)) { Trace.WriteLine("MemberInfo: " + _name); } _type = TypeLibUtil.TYPEDESCToString (_typeLib, _typeInfo, funcDesc.elemdescFunc.tdesc, TypeLibUtil.COMTYPE); if (funcDesc.invkind == INVOKEKIND.INVOKE_FUNC) { _infoType = "Function"; _property = false; if (funcDesc.cParams > 0) { AddParams(funcDesc, memberNames, funcDesc.cParams); } } else { if (funcDesc.invkind == INVOKEKIND.INVOKE_PROPERTYGET) { _printName += " (get)"; _nameKey += NAMEKEY_GET; } else if (funcDesc.invkind == INVOKEKIND.INVOKE_PROPERTYPUT) { _printName += " (put)"; _nameKey += NAMEKEY_SET; } else if (funcDesc.invkind == INVOKEKIND.INVOKE_PROPERTYPUTREF) { _printName += " (put ref)"; _nameKey += NAMEKEY_SETREF; } _infoType = "Property"; _property = true; } _flagsString = FlagsString((FUNCFLAGS)funcDesc.wFuncFlags); if (_property) { _presInfo = PresentationMap. GetInfo(PresentationMap.COM_PROPERTY); } else { _presInfo = PresentationMap. GetInfo(PresentationMap.COM_METHOD); } }
internal ComVariableInfo(BasicInfo parent, TYPEKIND typeKind, UCOMITypeInfo typeInfo, int index) : base(typeInfo) { IntPtr varDescPtr; CORRECT_VARDESC varDesc; _typeKind = typeKind; _typeLib = parent.TypeLib; _container = parent; _index = index; _typeInfo.GetVarDesc(_index, out varDescPtr); varDesc = (CORRECT_VARDESC) Marshal.PtrToStructure(varDescPtr, typeof(CORRECT_VARDESC)); int actLen; String[] memberNames = new String[100]; _typeInfo.GetNames(varDesc.memid, memberNames, memberNames.Length, out actLen); Name = memberNames[0]; if (TraceUtil.If(this, TraceLevel.Verbose)) { Trace.WriteLine("VariableInfo: " + _name); } if (_typeKind == TYPEKIND.TKIND_ENUM) { _infoType = "Constant"; try { Object value = Marshal.GetObjectForNativeVariant (varDesc.u.lpvarValue); _enumValue = value.ToString(); } catch (Exception ex) { _enumValue = "Unknown variant: 0x" + varDesc.u.lpvarValue.ToInt32().ToString("X"); TraceUtil.WriteLineWarning(this, "Exception reading enum value: " + ex); } } else { _infoType = "Variable"; } TYPEDESC typeDesc = varDesc.elemdescVar.tdesc; _varType = TypeLibUtil.TYPEDESCToString (_typeLib, _typeInfo, typeDesc, TypeLibUtil.COMTYPE); _typeInfo.ReleaseVarDesc(varDescPtr); _presInfo = PresentationMap. GetInfo(PresentationMap.COM_VARIABLE); }