示例#1
0
        public ExportableMethod(MethodDef method, string name, System.Runtime.InteropServices.CallingConvention callingConvention)
        {
            Method = method;
            Name   = name;
            switch (callingConvention)
            {
            case System.Runtime.InteropServices.CallingConvention.Winapi:
            case System.Runtime.InteropServices.CallingConvention.StdCall:
                CallingConvention = CallingConvention.StdCall;
                break;

            case System.Runtime.InteropServices.CallingConvention.Cdecl:
                CallingConvention = CallingConvention.C;
                break;

            case System.Runtime.InteropServices.CallingConvention.ThisCall:
                CallingConvention = CallingConvention.ThisCall;
                break;

            case System.Runtime.InteropServices.CallingConvention.FastCall:
                throw new NotImplementedException("Sorry FastCall is not supported by clr (https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.callconvfastcall)");

            default:
                throw new ArgumentOutOfRangeException(nameof(callingConvention), callingConvention, null);
            }
        }
        static string MakeDllImportArguments(string entryPoint, System.Runtime.InteropServices.CharSet charSet,
                                             bool setLastError, bool exactSpelling, bool preserveSig,
                                             System.Runtime.InteropServices.CallingConvention callingConvention,
                                             bool bestFitMapping, bool throwOnUnmappableChar)
        {
            var retval = new StringBuilder();

            if (!String.IsNullOrWhiteSpace(entryPoint))
            {
                retval.Append(", EntryPoint=\"" + entryPoint + '"');
            }
            if (charSet != DefaultDllImportAttribute.CharSet)
            {
                retval.Append(", CharSet = System.Runtime.InteropServices.CharSet." + charSet.ToString());
            }

            MakeDllImportArgument(retval, "SetLastError", setLastError, DefaultDllImportAttribute.SetLastError);
            MakeDllImportArgument(retval, "ExactSpelling", exactSpelling, DefaultDllImportAttribute.ExactSpelling);
            MakeDllImportArgument(retval, "PreserveSig", preserveSig, DefaultDllImportAttribute.PreserveSig);

            if (callingConvention != DefaultDllImportAttribute.CallingConvention)
            {
                retval.Append(", CallingConvention=" + callingConvention.ToString());
            }

            MakeDllImportArgument(retval, "BestFitMapping", bestFitMapping, DefaultDllImportAttribute.BestFitMapping);
            MakeDllImportArgument(retval, "ThrowOnUnmappableChar", throwOnUnmappableChar, DefaultDllImportAttribute.ThrowOnUnmappableChar);

            return(retval.ToString());
        }
示例#3
0
 internal DllImportAttribute(string dllName, string entryPoint, System.Runtime.InteropServices.CharSet charSet, bool exactSpelling, bool setLastError, bool preserveSig, System.Runtime.InteropServices.CallingConvention callingConvention, bool bestFitMapping, bool throwOnUnmappableChar)
 {
     this._val                  = dllName;
     this.EntryPoint            = entryPoint;
     this.CharSet               = charSet;
     this.ExactSpelling         = exactSpelling;
     this.SetLastError          = setLastError;
     this.PreserveSig           = preserveSig;
     this.CallingConvention     = callingConvention;
     this.BestFitMapping        = bestFitMapping;
     this.ThrowOnUnmappableChar = throwOnUnmappableChar;
 }
 internal DllImportAttribute(string dllName, string entryPoint, System.Runtime.InteropServices.CharSet charSet, bool exactSpelling, bool setLastError, bool preserveSig, System.Runtime.InteropServices.CallingConvention callingConvention, bool bestFitMapping, bool throwOnUnmappableChar)
 {
     this._val = dllName;
     this.EntryPoint = entryPoint;
     this.CharSet = charSet;
     this.ExactSpelling = exactSpelling;
     this.SetLastError = setLastError;
     this.PreserveSig = preserveSig;
     this.CallingConvention = callingConvention;
     this.BestFitMapping = bestFitMapping;
     this.ThrowOnUnmappableChar = throwOnUnmappableChar;
 }
示例#5
0
            private static TypeRef GetCallingConventionClass(ModuleDefMD module, CallingConvention callingConvention)
            {
                var corLibTypes = module.CorLibTypes;

                switch (corLibTypes.AssemblyRef.Name.ToLower())
                {
                case "system.runtime":
                    return(GetCallingConventionClassForNetCore(module, callingConvention));

                case "mscorlib":
                    return(GetCallingConventionClassForNetFramework(module, callingConvention));

                default:
                    throw new Exception($"Unmapped CorLibTypes {corLibTypes.AssemblyRef.FullName}");
                }
            }
示例#6
0
        public static void VerifyProcCallingConvention(string code, string name, System.Runtime.InteropServices.CallingConvention conv)
        {
            CodeMemberMethod         mem  = ConvertToSingleProc(code, name);
            CodeAttributeDeclaration decl = null;

            VerifyAttribute(mem.CustomAttributes, typeof(System.Runtime.InteropServices.DllImportAttribute), ref decl);
            if (conv == System.Runtime.InteropServices.CallingConvention.Winapi)
            {
                VerifyNoArgument(decl, "CallingConvention");
            }
            else
            {
                CodeAttributeArgument arg = null;
                VerifyArgument(decl, "CallingConvention", ref arg);
                Assert.Equal("CallingConvention." + conv.ToString(), CodeDomPrinter.Convert(arg.Value));
            }
        }
示例#7
0
        public void ReflectUtil_GetDelegateForFunctionPointerTest()
        {
            var module = LowLevel.Windows.LoadLibrary("user32.dll");
            // Can't use MessageBoxW, seems to implicitly marshal to UTF8? Could have sworn .NET strings were internally UTF16
            // Then again, my system is setup for en-us...
            var proc_ptr = LowLevel.Windows.GetProcAddress(module, "MessageBoxA");
            const System.Runtime.InteropServices.CallingConvention call_conv =
                System.Runtime.InteropServices.CallingConvention.Winapi;

            var msg_box = Util.GetDelegateForFunctionPointer <MessageBoxDelegate>(proc_ptr, call_conv);

            msg_box(IntPtr.Zero, "Hello World", "Test1", 0);

            var msg_box_gen = Util.GetDelegateForFunctionPointer <MessageBoxDelegateGeneric>(proc_ptr, call_conv);

            msg_box_gen(IntPtr.Zero, "Goodbye World", "Test2", 0);

            LowLevel.Windows.FreeLibrary(module);
        }
示例#8
0
        public static void VerifyFPtrCallingConvention(string code, string name, System.Runtime.InteropServices.CallingConvention conv)
        {
            CodeTypeDeclarationCollection col  = ConvertToCodeDom(code);
            CodeTypeDeclaration           type = null;

            VerifyType(col, name, ref type);


            if (conv != System.Runtime.InteropServices.CallingConvention.Winapi)
            {
                CodeAttributeDeclaration decl = null;
                VerifyAttribute(type.CustomAttributes, typeof(System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute), ref decl);

                CodeAttributeArgument arg = null;
                VerifyArgument(decl, string.Empty, ref arg);
                Assert.Equal("CallingConvention." + conv.ToString(), CodeDomPrinter.Convert(arg.Value));
            }
            else
            {
                VerifyNoAttribute(type.CustomAttributes, typeof(System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute));
            }
        }
 public UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention callingConvention)
 {
 }
示例#10
0
文件: Signature.cs 项目: koush/mono
		// unmanaged calling convention
		internal static void WriteStandAloneMethodSig(ModuleBuilder module, ByteBuffer bb, CallingConvention callingConvention, Type returnType, Type[] parameterTypes)
		{
			switch (callingConvention)
			{
				case CallingConvention.Cdecl:
					bb.Write((byte)0x01);	// C
					break;
				case CallingConvention.StdCall:
				case CallingConvention.Winapi:
					bb.Write((byte)0x02);	// STDCALL
					break;
				case CallingConvention.ThisCall:
					bb.Write((byte)0x03);	// THISCALL
					break;
				case CallingConvention.FastCall:
					bb.Write((byte)0x04);	// FASTCALL
					break;
				default:
					throw new ArgumentOutOfRangeException("callingConvention");
			}
			bb.WriteCompressedInt(parameterTypes.Length);
			WriteType(module, bb, returnType);
			foreach (Type t in parameterTypes)
			{
				WriteType(module, bb, t);
			}
		}
示例#11
0
 public MethodBuilder DefinePInvokeMethod(string name, string dllName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet)
 {
     return(default(MethodBuilder));
 }
示例#12
0
        internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
        {
            string str;

            if ((method.Attributes & MethodAttributes.PinvokeImpl) == MethodAttributes.PrivateScope)
            {
                return(null);
            }
            MetadataImport    metadataImport  = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
            string            importDll       = null;
            int               metadataToken   = method.MetadataToken;
            PInvokeAttributes bestFitUseAssem = PInvokeAttributes.BestFitUseAssem;

            metadataImport.GetPInvokeMap(metadataToken, out bestFitUseAssem, out str, out importDll);
            System.Runtime.InteropServices.CharSet none = System.Runtime.InteropServices.CharSet.None;
            switch ((bestFitUseAssem & PInvokeAttributes.CharSetAuto))
            {
            case PInvokeAttributes.BestFitUseAssem:
                none = System.Runtime.InteropServices.CharSet.None;
                break;

            case PInvokeAttributes.CharSetAnsi:
                none = System.Runtime.InteropServices.CharSet.Ansi;
                break;

            case PInvokeAttributes.CharSetUnicode:
                none = System.Runtime.InteropServices.CharSet.Unicode;
                break;

            case PInvokeAttributes.CharSetAuto:
                none = System.Runtime.InteropServices.CharSet.Auto;
                break;
            }
            System.Runtime.InteropServices.CallingConvention cdecl = System.Runtime.InteropServices.CallingConvention.Cdecl;
            switch ((bestFitUseAssem & PInvokeAttributes.CallConvMask))
            {
            case PInvokeAttributes.CallConvStdcall:
                cdecl = System.Runtime.InteropServices.CallingConvention.StdCall;
                break;

            case PInvokeAttributes.CallConvThiscall:
                cdecl = System.Runtime.InteropServices.CallingConvention.ThisCall;
                break;

            case PInvokeAttributes.CallConvFastcall:
                cdecl = System.Runtime.InteropServices.CallingConvention.FastCall;
                break;

            case PInvokeAttributes.CallConvWinapi:
                cdecl = System.Runtime.InteropServices.CallingConvention.Winapi;
                break;

            case PInvokeAttributes.CallConvCdecl:
                cdecl = System.Runtime.InteropServices.CallingConvention.Cdecl;
                break;
            }
            bool exactSpelling         = (bestFitUseAssem & PInvokeAttributes.NoMangle) != PInvokeAttributes.BestFitUseAssem;
            bool setLastError          = (bestFitUseAssem & PInvokeAttributes.SupportsLastError) != PInvokeAttributes.BestFitUseAssem;
            bool bestFitMapping        = (bestFitUseAssem & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
            bool throwOnUnmappableChar = (bestFitUseAssem & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;

            return(new DllImportAttribute(importDll, str, none, exactSpelling, setLastError, (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != MethodImplAttributes.IL, cdecl, bestFitMapping, throwOnUnmappableChar));
        }
 public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type returnType, System.Type[] parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet)
 {
     throw new PlatformNotSupportedException();
 }
示例#14
0
 public DynamicDllImport(string dllName, System.Runtime.InteropServices.CharSet charSet = System.Runtime.InteropServices.CharSet.Auto, System.Runtime.InteropServices.CallingConvention callingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)
 {
     this.dllName           = dllName;
     this.CharSet           = charSet;
     this.CallingConvention = callingConvention;
 }
示例#15
0
 public static SignatureHelper GetMethodSigHelper(System.Reflection.Module mod, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, Type returnType)
 {
     CodeContract.Requires((int)unmanagedCallConv == 2 || (int)unmanagedCallConv == 3 || (int)unmanagedCallConv == 4 || (int)unmanagedCallConv == 5);
     return(default(SignatureHelper));
 }
示例#16
0
            private static TypeRef GetCallingConventionClassForNetCore(ModuleDefMD module, CallingConvention callingConvention)
            {
                const string callingConventionNamespace    = "System.Runtime.CompilerServices";
                const string callingConventionAssemblyName = "System.Runtime.CompilerServices.VisualC";

                var callingConventionAssemblyRef = GetCallingConventionAssemblyRefForNetCore(module, callingConventionAssemblyName);

                if (callingConventionAssemblyRef == null)
                {
                    Debugger.Launch();
                    throw new Exception($"Could not find assembly reference for {callingConventionAssemblyName}.");
                }

                switch (callingConvention)
                {
                case CallingConvention.Winapi:
                    return(GetTypeRef(module, callingConventionNamespace, "CallConvStdcall", callingConventionAssemblyRef));

                case CallingConvention.Cdecl:
                    return(GetTypeRef(module, callingConventionNamespace, "CallConvCdecl", callingConventionAssemblyRef));

                case CallingConvention.StdCall:
                    return(GetTypeRef(module, callingConventionNamespace, "CallConvStdcall", callingConventionAssemblyRef));

                case CallingConvention.ThisCall:
                    return(GetTypeRef(module, callingConventionNamespace, "CallConvThiscall", callingConventionAssemblyRef));

                case CallingConvention.FastCall:
                    return(GetTypeRef(module, callingConventionNamespace, "CallConvFastcall", callingConventionAssemblyRef));

                default:
                    throw new ArgumentOutOfRangeException(nameof(callingConvention), callingConvention, "Unhandled calling convention.");
                }
            }
示例#17
0
        // unmanaged calling convention
        internal static void WriteStandAloneMethodSig(ModuleBuilder module, ByteBuffer bb, CallingConvention callingConvention, Type returnType, Type[] parameterTypes)
        {
            switch (callingConvention)
            {
            case CallingConvention.Cdecl:
                bb.Write((byte)0x01);                           // C
                break;

            case CallingConvention.StdCall:
            case CallingConvention.Winapi:
                bb.Write((byte)0x02);                           // STDCALL
                break;

            case CallingConvention.ThisCall:
                bb.Write((byte)0x03);                           // THISCALL
                break;

            case CallingConvention.FastCall:
                bb.Write((byte)0x04);                           // FASTCALL
                break;

            default:
                throw new ArgumentOutOfRangeException("callingConvention");
            }
            bb.WriteCompressedInt(parameterTypes.Length);
            WriteType(module, bb, returnType);
            foreach (Type t in parameterTypes)
            {
                WriteType(module, bb, t);
            }
        }
 public UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention callingConvention)
 {
     this.m_callingConvention = callingConvention;
 }
示例#19
0
 public static SignatureHelper GetMethodSigHelper(System.Runtime.InteropServices.CallingConvention unmanagedCallingConvention, Type returnType)
 {
 }
示例#20
0
            private static TypeRef GetCallingConventionClassForNetFramework(ModuleDefMD module, CallingConvention callingConvention)
            {
                const string callingConventionNamespace = "System.Runtime.CompilerServices";

                var corLibTypes = module.CorLibTypes;

                switch (callingConvention)
                {
                case CallingConvention.Winapi:
                    return(corLibTypes.GetTypeRef(callingConventionNamespace, "CallConvStdcall"));

                case CallingConvention.Cdecl:
                    return(corLibTypes.GetTypeRef(callingConventionNamespace, "CallConvCdecl"));

                case CallingConvention.StdCall:
                    return(corLibTypes.GetTypeRef(callingConventionNamespace, "CallConvStdcall"));

                case CallingConvention.ThisCall:
                    return(corLibTypes.GetTypeRef(callingConventionNamespace, "CallConvThiscall"));

                case CallingConvention.FastCall:
                    return(corLibTypes.GetTypeRef(callingConventionNamespace, "CallConvFastcall"));

                default:
                    throw new ArgumentOutOfRangeException(nameof(callingConvention), callingConvention, "Unhandled calling convention.");
                }
            }
示例#21
0
        internal static __StandAloneMethodSig ReadStandAloneMethodSig(ModuleReader module, ByteReader br, IGenericContext context)
        {
            CallingConventions callingConvention = 0;

            System.Runtime.InteropServices.CallingConvention unmanagedCallingConvention = 0;
            bool unmanaged;
            byte flags = br.ReadByte();

            switch (flags & 7)
            {
            case DEFAULT:
                callingConvention = CallingConventions.Standard;
                unmanaged         = false;
                break;

            case 0x01:                          // C
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl;
                unmanaged = true;
                break;

            case 0x02:                          // STDCALL
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall;
                unmanaged = true;
                break;

            case 0x03:                          // THISCALL
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.ThisCall;
                unmanaged = true;
                break;

            case 0x04:                          // FASTCALL
                unmanagedCallingConvention = System.Runtime.InteropServices.CallingConvention.FastCall;
                unmanaged = true;
                break;

            case VARARG:
                callingConvention = CallingConventions.VarArgs;
                unmanaged         = false;
                break;

            default:
                throw new BadImageFormatException();
            }
            if ((flags & HASTHIS) != 0)
            {
                callingConvention |= CallingConventions.HasThis;
            }
            if ((flags & EXPLICITTHIS) != 0)
            {
                callingConvention |= CallingConventions.ExplicitThis;
            }
            if ((flags & GENERIC) != 0)
            {
                throw new BadImageFormatException();
            }
            int paramCount = br.ReadCompressedInt();

            SkipCustomModifiers(br);
            Type        returnType             = ReadRetType(module, br, context);
            List <Type> parameterTypes         = new List <Type>();
            List <Type> optionalParameterTypes = new List <Type>();
            List <Type> curr = parameterTypes;

            for (int i = 0; i < paramCount; i++)
            {
                if (br.PeekByte() == SENTINEL)
                {
                    br.ReadByte();
                    curr = optionalParameterTypes;
                }
                SkipCustomModifiers(br);
                curr.Add(ReadParam(module, br, context));
            }
            return(new __StandAloneMethodSig(unmanaged, unmanagedCallingConvention, callingConvention, returnType, parameterTypes.ToArray(), optionalParameterTypes.ToArray()));
        }
示例#22
0
        public static SignatureHelper GetMethodSigHelper(System.Reflection.Module mod, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, Type returnType)
        {
            Contract.Ensures(Contract.Result <System.Reflection.Emit.SignatureHelper>() != null);

            return(default(SignatureHelper));
        }
示例#23
0
 public DynamicDllImport(string dllName, System.Runtime.InteropServices.CharSet charSet = System.Runtime.InteropServices.CharSet.Auto, System.Runtime.InteropServices.CallingConvention callingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)
 {
     this.dllName = dllName;
     this.CharSet = charSet;
     this.CallingConvention = callingConvention;
 }
示例#24
0
        public MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet)
        {
            Contract.Ensures(Contract.Result <System.Reflection.Emit.MethodBuilder>() != null);

            return(default(MethodBuilder));
        }
示例#25
0
 public virtual void EmitCalli(System.Reflection.Emit.OpCode opcode, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, System.Type?returnType, System.Type[]?parameterTypes)
 {
 }
 public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type?returnType, System.Type[]?parameterTypes, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet)
 {
     throw null;
 }
 public System.Reflection.Emit.MethodBuilder DefinePInvokeMethod(string name, string dllName, string entryName, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, System.Type returnType, System.Type[] returnTypeRequiredCustomModifiers, System.Type[] returnTypeOptionalCustomModifiers, System.Type[] parameterTypes, System.Type[][] parameterTypeRequiredCustomModifiers, System.Type[][] parameterTypeOptionalCustomModifiers, System.Runtime.InteropServices.CallingConvention nativeCallConv, System.Runtime.InteropServices.CharSet nativeCharSet)
 {
     throw null;
 }
 public virtual new void EmitCalli(OpCode opcode, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes)
 {
 }
示例#29
0
 public __StandAloneMethodSig MakeStandAloneMethodSig(System.Runtime.InteropServices.CallingConvention callingConvention, Type returnType, CustomModifiers returnTypeCustomModifiers, Type[] parameterTypes, CustomModifiers[] parameterTypeCustomModifiers)
 {
     return(new __StandAloneMethodSig(true, callingConvention, 0, returnType ?? this.System_Void, Util.Copy(parameterTypes), Type.EmptyTypes,
                                      PackedCustomModifiers.CreateFromExternal(returnTypeCustomModifiers, parameterTypeCustomModifiers, Util.NullSafeLength(parameterTypes))));
 }
示例#30
0
 public abstract void EmitCalli(System.Reflection.Emit.OpCode opcode, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes);