/*-------------------- Constructors ---------------------------------*/ /// <summary> /// Create a signature for a calli instruction /// </summary> /// <param name="cconv">calling conventions</param> /// <param name="retType">return type</param> /// <param name="pars">parameter types</param> public CalliSig(CallConv cconv, Type retType, Type[] pars) { callConv = cconv; this.retType = retType; parTypes = pars; if (pars != null) numPars = (uint)pars.Length; }
public MethodDef AddDefinition(MethAttr method_attr, ImplAttr impl_attr, CallConv call_conv, string name, TypeRef return_type, Param[] param_list, TypeRef[] param_type_list, Location location) { string signature = GetSignature(name, return_type, param_type_list); if (MethodDefinedEvent != null) { MethodDefinedEvent(this, new MethodDefinedEventArgs(signature, name, return_type, param_list, table.Contains(signature), method_attr, impl_attr, call_conv)); } MethodTableItem item = (MethodTableItem)table[signature]; if (item == null) { MethodDef method = parent_class.AddMethod(method_attr, impl_attr, name, return_type.Type, param_list); method.AddCallConv(call_conv); AddDefined(signature, method, location); return(method); } item.Method.AddMethAttribute(method_attr); item.Method.AddImplAttribute(impl_attr); item.Method.AddCallConv(call_conv); item.Defined = true; return(item.Method); }
/// <summary> /// Loads a native procedure from a DLL. /// </summary> /// <typeparam name="T">The procedure type.</typeparam> /// <param name="dllPath">The path to the DLL.</param> /// <param name="procName">The name of the procedure.</param> /// <param name="callConv">The <see cref="CallConv"/> of the native procedure.</param> /// <returns>The procedure callable from C#.</returns> public static T LoadNativeProcedure <T>( string dllPath, string procName, CallConv callConv) where T : Delegate { return((T)LoadNativeProcedure(dllPath, procName, typeof(T), callConv)); }
public MethodDefinedEventArgs(string signature, string name, TypeRef return_type, Param[] param_list, bool is_in_table, MethAttr method_attr, ImplAttr impl_attr, CallConv call_conv) : base(signature, name, return_type, param_list, is_in_table) { MethodAttributes = method_attr; ImplAttributes = impl_attr; CallConv = call_conv; }
/// <summary> /// Add the optional parameters to a vararg method /// This method sets the vararg calling convention /// </summary> /// <param name="optPars">the optional pars for the vararg call</param> public void AddVarArgs(Type[] optPars) { optParams = optPars; if (optPars != null) { numOptPars = (uint)optPars.Length; } callConv |= CallConv.Vararg; }
public MethodDefinedEventArgs (string signature, string name, TypeRef return_type, Param[] param_list, bool is_in_table, MethAttr method_attr, ImplAttr impl_attr, CallConv call_conv) : base (signature, name, return_type, param_list, is_in_table) { MethodAttributes = method_attr; ImplAttributes = impl_attr; CallConv = call_conv; }
/*-------------------- Constructors ---------------------------------*/ /// <summary> /// Create a signature for a calli instruction /// </summary> /// <param name="cconv">calling conventions</param> /// <param name="retType">return type</param> /// <param name="pars">parameter types</param> public CalliSig(CallConv cconv, Type retType, Type[] pars) { callConv = cconv; this.retType = retType; parTypes = pars; if (pars != null) { numPars = (uint)pars.Length; } }
/// <summary> /// Create a signature for a calli instruction /// </summary> /// <param name="cconv">calling conventions</param> /// <param name="retType">return type</param> /// <param name="pars">parameter types</param> public CalliSig(CallConv cconv, Type retType, Type[] pars) { tabIx = MDTable.StandAloneSig; callConv = cconv; returnType = retType; parameters = pars; if (pars != null) { numPars = (uint)pars.Length; } }
/// <summary> /// Loads a native procedure from a DLL. /// </summary> /// <param name="dllPath">The path to the DLL.</param> /// <param name="procName">The name of the procedure.</param> /// <param name="delegateType">The <see cref="Delegate"/> type of the procedure..</param> /// <param name="callConv">The <see cref="CallConv"/> of the native procedure.</param> /// <returns>The procedure's <see cref="Delegate"/> callable from C#.</returns> public static Delegate LoadNativeProcedure( string dllPath, string procName, Type delegateType, CallConv callConv) { MethodInfo?method = delegateType.GetMethod("Invoke"); Debug.Assert(method != null); var returnType = method.ReturnType; var parameterTypes = method.GetParameters().Select(x => x.ParameterType).ToArray(); var nativeMethod = LoadNativeProcedure(dllPath, procName, returnType, parameterTypes, callConv); return(Delegate.CreateDelegate(delegateType, nativeMethod)); }
public DecodedMethodImportAttributes(MethodImportAttributes attrs) { this.exact_spelling = (attrs & MethodImportAttributes.ExactSpelling) != 0; { MethodImportAttributes char_set_attr = attrs & MethodImportAttributes.CharSetMask; this.char_set = char_set_attr switch { MethodImportAttributes.None => CharSet.none, MethodImportAttributes.CharSetAnsi => CharSet.ansi, MethodImportAttributes.CharSetUnicode => CharSet.unicode, MethodImportAttributes.CharSetAuto => CharSet.auto, _ => throw new InvalidDataException(string.Format("unknown MethodImportAttributes char_set {0}", char_set_attr)), }; } { MethodImportAttributes best_fit_attr = attrs & MethodImportAttributes.BestFitMappingMask; this.best_fit = best_fit_attr switch { MethodImportAttributes.None => null, MethodImportAttributes.BestFitMappingDisable => false, MethodImportAttributes.BestFitMappingEnable => true, _ => throw new InvalidDataException(string.Format("unknown MethodImportAttributes best_fit {0}", best_fit_attr)), }; } this.set_last_error = (attrs & MethodImportAttributes.SetLastError) != 0; { MethodImportAttributes call_conv_attr = attrs & MethodImportAttributes.CallingConventionMask; this.call_conv = call_conv_attr switch { MethodImportAttributes.CallingConventionWinApi => CallConv.winapi, MethodImportAttributes.CallingConventionCDecl => CallConv.cdecl, MethodImportAttributes.CallingConventionStdCall => CallConv.stdcall, MethodImportAttributes.CallingConventionThisCall => CallConv.thiscall, MethodImportAttributes.CallingConventionFastCall => CallConv.fastcall, _ => throw new InvalidDataException(string.Format("unknown MethodImportAttributes call_conv {0}", call_conv_attr)), }; } { MethodImportAttributes throw_attr = attrs & MethodImportAttributes.ThrowOnUnmappableCharMask; this.throw_on_unmappable_char = throw_attr switch { MethodImportAttributes.None => null, MethodImportAttributes.ThrowOnUnmappableCharDisable => false, MethodImportAttributes.ThrowOnUnmappableCharEnable => true, _ => throw new InvalidDataException(string.Format("unknown MethodImportAttributes throw_on_unmappable {0}", throw_attr)), }; } }
internal sealed override void Resolve(PEReader buff) { MethSig mSig = buff.ReadMethSig(null, sigIx); callConv = mSig.callConv; retType = mSig.retType; parTypes = mSig.parTypes; if (parTypes != null) { numPars = (uint)parTypes.Length; } optParTypes = mSig.optParTypes; if (optParTypes != null) { numOptPars = (uint)optParTypes.Length; } }
/// <summary> /// Loads a native procedure from a DLL. /// </summary> /// <param name="dllPath">The path to the DLL.</param> /// <param name="procName">The name of the procedure.</param> /// <param name="returnType">The return type of the procedure.</param> /// <param name="parameterTypes">The parameter types of the procedure.</param> /// <param name="callConv">The <see cref="CallConv"/> of the native procedure.</param> /// <returns>The <see cref="MethodInfo"/> of the procedure.</returns> public static MethodInfo LoadNativeProcedure( string dllPath, string procName, Type returnType, Type[] parameterTypes, CallConv callConv) { var asmName = new AssemblyName("TempAssembly"); var asmBuilder = AssemblyBuilder.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run); var modBuilder = asmBuilder.DefineDynamicModule("TempModule"); var typeBuilder = modBuilder.DefineType("TempType", TypeAttributes.Class | TypeAttributes.Public); // Optional: Use if you need to set properties on DllImportAttribute ConstructorInfo?dllImportCtor = typeof(DllImportAttribute).GetConstructor(new Type[] { typeof(string) }); Debug.Assert(dllImportCtor != null); var dllImportBuilder = new CustomAttributeBuilder(dllImportCtor, new object[] { dllPath }); var pinvokeBuilder = typeBuilder.DefinePInvokeMethod( name: procName, dllName: dllPath, entryName: procName, attributes: MethodAttributes.Static | MethodAttributes.Public, callingConvention: CallingConventions.Standard, returnType: returnType, parameterTypes: parameterTypes, nativeCallConv: ToCallingConvention(callConv), nativeCharSet: CharSet.Unicode); pinvokeBuilder.SetCustomAttribute(dllImportBuilder); Type?tempType = typeBuilder.CreateType(); Debug.Assert(tempType != null); MethodInfo?result = tempType.GetMethod(procName, BindingFlags.Static | BindingFlags.Public); Debug.Assert(result != null); return(result); }
/// <summary> /// Create a new function pointer type /// </summary> /// <param name="meth">the function to be referenced</param> public MethPtrType (CallConv callconv, Type retType, Type[] pars, bool varArgMeth, Type[] optPars) : base(0x1B) { this.retType = retType; callConv = callconv; parList = pars; this.varArgMeth = varArgMeth; if (parList != null) numPars = (uint)parList.Length; if (varArgMeth) { optParList = optPars; if (optParList != null) numOptPars = (uint)optParList.Length; callConv |= CallConv.Vararg; } tabIx = MDTable.TypeSpec; }
public abstract void AddCallConv(CallConv cconv);
/// <summary> /// Add extra calling conventions to this callsite signature /// </summary> /// <param name="cconv"></param> public void AddCallingConv(CallConv cconv) { callConv |= cconv; }
public override void AddCallConv (CallConv cconv) { throw new Exception ("Should not be used."); }
/// <summary> /// Create a signature for a calli instruction /// </summary> /// <param name="cconv">calling conventions</param> /// <param name="retType">return type</param> /// <param name="pars">parameter types</param> public CalliSig(CallConv cconv, Type retType, Type[] pars) { tabIx = MDTable.StandAloneSig; callConv = cconv; returnType = retType; parameters = pars; if (pars != null) numPars = (uint)pars.Length; }
/// <summary> /// Add the optional parameters to a vararg method /// This method sets the vararg calling convention /// </summary> /// <param name="optPars">the optional pars for the vararg call</param> public void AddVarArgs(Type[] optPars) { optParams = optPars; if (optPars != null) numOptPars = (uint)optPars.Length; callConv |= CallConv.Vararg; }
/// <summary> /// Add calling conventions to this method descriptor /// </summary> /// <param name="cconv"></param> public void AddCallConv(CallConv cconv) { sig.callConv |= cconv; }
/// <summary> /// Check to see if the method signature has a particular calling convention. /// </summary> /// <param name="callCon">The convention to check to see if the method has.</param> /// <returns>Ture if the calling convention exists on the method.</returns> internal bool HasCallConv(CallConv callCon) { return ((callConv & callCon) == callCon); }
internal override sealed void Resolve(PEReader buff) { MethSig mSig = buff.ReadMethSig(null,sigIx); callConv = mSig.callConv; retType = mSig.retType; parTypes = mSig.parTypes; if (parTypes != null) numPars = (uint)parTypes.Length; optParTypes = mSig.optParTypes; if (optParTypes != null) numOptPars = (uint)optParTypes.Length; }
/// <summary> /// Check to see if the method signature has a particular calling convention. /// </summary> /// <param name="callCon">The convention to check to see if the method has.</param> /// <returns>Ture if the calling convention exists on the method.</returns> internal bool HasCallConv(CallConv callCon) { return((callConv & callCon) == callCon); }
private static CallingConvention ToCallingConvention(CallConv callConv) => callConv switch {
internal MethodRef(MetaDataElement paren, string name, Type retType, Type[] pars, bool varArgMeth, Type[] optPars, int gen_param_count) : base(name) { parent = paren; parList = pars; this.retType = retType; if (parList != null) numPars = (uint)parList.Length; if (varArgMeth) { optParList = optPars; if (optParList != null) numOptPars = (uint)optParList.Length; callConv = CallConv.Vararg; } this.gen_param_count = gen_param_count; }
public MethodDef AddDefinition (MethAttr method_attr, ImplAttr impl_attr, CallConv call_conv, string name, TypeRef return_type, Param[] param_list, TypeRef[] param_type_list, Location location) { string signature = GetSignature (name, return_type, param_type_list); if (MethodDefinedEvent != null) MethodDefinedEvent (this, new MethodDefinedEventArgs (signature, name, return_type, param_list, table.Contains (signature), method_attr, impl_attr, call_conv)); MethodTableItem item = (MethodTableItem) table[signature]; if (item == null) { MethodDef method = parent_class.AddMethod (method_attr, impl_attr, name, return_type.Type, param_list); method.AddCallConv (call_conv); AddDefined (signature, method, location); return method; } item.Method.AddMethAttribute (method_attr); item.Method.AddImplAttribute (impl_attr); item.Method.AddCallConv (call_conv); item.Defined = true; return item.Method; }
public override void AddCallConv(CallConv cconv) { callConv |= cconv; }
public Proc(CallConv callConv, Type ret, IList <Type> parameters) { CallConv = callConv; Return = ret; Parameters = parameters.AsValueList(); }