Beispiel #1
0
        private CustomAttributeData? GetDllImportAttributeData()
        {
            if ((Attributes & MethodAttributes.PinvokeImpl) == 0)
                return null;

            string entryPoint;
            string? dllName = null;
            PInvokeAttributes flags = 0;

            GetPInvoke(out flags, out entryPoint, out dllName);

            CharSet charSet = (flags & PInvokeAttributes.CharSetMask) switch
            {
                PInvokeAttributes.CharSetNotSpec => CharSet.None,
                PInvokeAttributes.CharSetAnsi => CharSet.Ansi,
                PInvokeAttributes.CharSetUnicode => CharSet.Unicode,
                PInvokeAttributes.CharSetAuto => CharSet.Auto,
                // Invalid: default to CharSet.None
                _ => CharSet.None,
            };

            InteropServicesCallingConvention callingConvention = (flags & PInvokeAttributes.CallConvMask) switch
            {
                PInvokeAttributes.CallConvWinapi => InteropServicesCallingConvention.Winapi,
                PInvokeAttributes.CallConvCdecl => InteropServicesCallingConvention.Cdecl,
                PInvokeAttributes.CallConvStdcall => InteropServicesCallingConvention.StdCall,
                PInvokeAttributes.CallConvThiscall => InteropServicesCallingConvention.ThisCall,
                PInvokeAttributes.CallConvFastcall => InteropServicesCallingConvention.FastCall,
                // Invalid: default to CallingConvention.Cdecl
                _ => InteropServicesCallingConvention.Cdecl,
            };

            bool exactSpelling = (flags & PInvokeAttributes.NoMangle) != 0;
            bool setLastError = (flags & PInvokeAttributes.SupportsLastError) != 0;
            bool bestFitMapping = (flags & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
            bool throwOnUnmappableChar = (flags & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
            bool preserveSig = (GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;

            var ctorArgs = new CustomAttributeTypedArgument[] {
                new CustomAttributeTypedArgument (typeof(string), dllName),
            };

            Type attrType = typeof(DllImportAttribute);

            var namedArgs = new CustomAttributeNamedArgument[] {
                new CustomAttributeNamedArgument (attrType.GetField ("EntryPoint")!, entryPoint),
                new CustomAttributeNamedArgument (attrType.GetField ("CharSet")!, charSet),
                new CustomAttributeNamedArgument (attrType.GetField ("ExactSpelling")!, exactSpelling),
                new CustomAttributeNamedArgument (attrType.GetField ("SetLastError")!, setLastError),
                new CustomAttributeNamedArgument (attrType.GetField ("PreserveSig")!, preserveSig),
                new CustomAttributeNamedArgument (attrType.GetField ("CallingConvention")!, callingConvention),
                new CustomAttributeNamedArgument (attrType.GetField ("BestFitMapping")!, bestFitMapping),
                new CustomAttributeNamedArgument (attrType.GetField ("ThrowOnUnmappableChar")!, throwOnUnmappableChar)
            };

            return new CustomAttributeData(
                attrType.GetConstructor(new[] { typeof(string) })!,
                ctorArgs,
                namedArgs);
        }
Beispiel #2
0
        static DR.CallingConvention ConvertCallingConvention(CR.CallingConvention callingConvention)
        {
            switch (callingConvention)
            {
            case CR.CallingConvention.Winapi:       return(DR.CallingConvention.Default);

            case CR.CallingConvention.Cdecl:        return(DR.CallingConvention.C);

            case CR.CallingConvention.StdCall:      return(DR.CallingConvention.StdCall);

            case CR.CallingConvention.ThisCall:     return(DR.CallingConvention.ThisCall);

            case CR.CallingConvention.FastCall:     return(DR.CallingConvention.FastCall);

            default: throw new ApplicationException($"Unknown CallingConvention {callingConvention}");
            }
        }
Beispiel #3
0
            public static Type CreateDelegateType(Type ReturnType, Type[] Args, CCnv CConv = CCnv.Cdecl)
            {
                string DelegateName = CreateDelegateName(ReturnType, Args);

                if (DelegateTypes.ContainsKey(DelegateName))
                {
                    return(DelegateTypes[DelegateName]);
                }

                TypeBuilder TB = DefMod.DefineType(DelegateName, TypeAttributes.AutoClass | TypeAttributes.AnsiClass |
                                                   TypeAttributes.Sealed);

                TB.SetParent(typeof(MulticastDelegate));
                TB.SetCustomAttribute(new CustomAttributeBuilder(typeof(UnmanagedFunctionPointerAttribute).GetConstructor
                                                                     (new[] { typeof(CCnv) }),
                                                                 new object[] { CConv }));

                TB.DefineConstructor(MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName |
                                     MethodAttributes.RTSpecialName, CallingConventions.Standard, new Type[] { typeof(Object), typeof(IntPtr) })
                .SetImplementationFlags(MethodImplAttributes.Runtime);

                TB.DefineMethod("BeginInvoke", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.NewSlot |
                                MethodAttributes.Virtual, CallingConventions.HasThis, typeof(IAsyncResult),
                                Args.Concat <Type>(new[] { typeof(AsyncCallback), typeof(object) }).ToArray())
                .SetImplementationFlags(MethodImplAttributes.Runtime);

                TB.DefineMethod("EndInvoke", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.NewSlot |
                                MethodAttributes.Virtual, CallingConventions.HasThis, ReturnType, new[] { typeof(AsyncCallback) })
                .SetImplementationFlags(MethodImplAttributes.Runtime);

                TB.DefineMethod("Invoke", MethodAttributes.HideBySig | MethodAttributes.Public | MethodAttributes.NewSlot |
                                MethodAttributes.Virtual, CallingConventions.HasThis, ReturnType, Args)
                .SetImplementationFlags(MethodImplAttributes.Runtime);

                Type T = TB.CreateType();

                DelegateTypes.Add(DelegateName, T);
                return(T);
            }
Beispiel #4
0
        private static string ResolveCallingConventionCompilerServicesType(
            SysInterop.CallingConvention callingConvention
            )
        {
            switch (callingConvention)
            {
            case SysInterop.CallingConvention.Cdecl:
                return("CallConvCdecl");

            case SysInterop.CallingConvention.ThisCall:
                return("CallConvThiscall");

            case SysInterop.CallingConvention.StdCall:
                return("CallConvStdcall");

            case SysInterop.CallingConvention.FastCall:
                return("CallConvFastcall");

            case SysInterop.CallingConvention.Winapi:
            default:
                throw new NotSupportedException($"{callingConvention} is not supported for Reverse PInvoke!");
            }
        }
Beispiel #5
0
 ExportDefinition(MethodDef methodDefinition, SysInterop.CallingConvention callingConvention, string alias)
 {
     MethodDefinition  = methodDefinition ?? throw new ArgumentNullException(nameof(methodDefinition));
     CallingConvention = callingConvention;
     Alias             = alias ?? throw new ArgumentNullException(nameof(alias));
 }
 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 new PlatformNotSupportedException();
 }
Beispiel #7
0
 public static Type CreateDelegateType(this Delegate D, CCnv CConv = CCnv.Cdecl)
 {
     return(CreateDelegateType(D.Method.ReturnType, D.Method.GetParamTypes(), CConv));
 }