示例#1
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
        {
            if ((method.Attributes & MethodAttributes.PinvokeImpl) == 0)
            {
                return(null);
            }

            MetadataImport    scope = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
            string            entryPoint, dllName = null;
            int               token = method.MetadataToken;
            PInvokeAttributes flags = 0;

            scope.GetPInvokeMap(token, out flags, out entryPoint, out dllName);

            CharSet charSet = CharSet.None;

            switch (flags & PInvokeAttributes.CharSetMask)
            {
            case PInvokeAttributes.CharSetNotSpec: charSet = CharSet.None; break;

            case PInvokeAttributes.CharSetAnsi: charSet = CharSet.Ansi; break;

            case PInvokeAttributes.CharSetUnicode: charSet = CharSet.Unicode; break;

            case PInvokeAttributes.CharSetAuto: charSet = CharSet.Auto; break;

            // Invalid: default to CharSet.None
            default: break;
            }

            CallingConvention callingConvention = CallingConvention.Cdecl;

            switch (flags & PInvokeAttributes.CallConvMask)
            {
            case PInvokeAttributes.CallConvWinapi: callingConvention = CallingConvention.Winapi; break;

            case PInvokeAttributes.CallConvCdecl: callingConvention = CallingConvention.Cdecl; break;

            case PInvokeAttributes.CallConvStdcall: callingConvention = CallingConvention.StdCall; break;

            case PInvokeAttributes.CallConvThiscall: callingConvention = CallingConvention.ThisCall; break;

            case PInvokeAttributes.CallConvFastcall: callingConvention = CallingConvention.FastCall; break;

            // Invalid: default to CallingConvention.Cdecl
            default: break;
            }

            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           = (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;

            return(new DllImportAttribute(
                       dllName, entryPoint, charSet, exactSpelling, setLastError, preserveSig,
                       callingConvention, bestFitMapping, throwOnUnmappableChar));
        }
示例#2
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));
        }
        internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
        {
            if ((method.Attributes & MethodAttributes.PinvokeImpl) == MethodAttributes.PrivateScope)
            {
                return(null);
            }
            MetadataImport    metadataImport    = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
            string            dllName           = null;
            int               metadataToken     = method.MetadataToken;
            PInvokeAttributes pinvokeAttributes = PInvokeAttributes.CharSetNotSpec;
            string            entryPoint;

            metadataImport.GetPInvokeMap(metadataToken, out pinvokeAttributes, out entryPoint, out dllName);
            CharSet charSet = CharSet.None;

            switch (pinvokeAttributes & PInvokeAttributes.CharSetMask)
            {
            case PInvokeAttributes.CharSetNotSpec:
                charSet = CharSet.None;
                break;

            case PInvokeAttributes.CharSetAnsi:
                charSet = CharSet.Ansi;
                break;

            case PInvokeAttributes.CharSetUnicode:
                charSet = CharSet.Unicode;
                break;

            case PInvokeAttributes.CharSetMask:
                charSet = CharSet.Auto;
                break;
            }
            CallingConvention callingConvention  = CallingConvention.Cdecl;
            PInvokeAttributes pinvokeAttributes2 = pinvokeAttributes & PInvokeAttributes.CallConvMask;

            if (pinvokeAttributes2 <= PInvokeAttributes.CallConvCdecl)
            {
                if (pinvokeAttributes2 != PInvokeAttributes.CallConvWinapi)
                {
                    if (pinvokeAttributes2 == PInvokeAttributes.CallConvCdecl)
                    {
                        callingConvention = CallingConvention.Cdecl;
                    }
                }
                else
                {
                    callingConvention = CallingConvention.Winapi;
                }
            }
            else if (pinvokeAttributes2 != PInvokeAttributes.CallConvStdcall)
            {
                if (pinvokeAttributes2 != PInvokeAttributes.CallConvThiscall)
                {
                    if (pinvokeAttributes2 == PInvokeAttributes.CallConvFastcall)
                    {
                        callingConvention = CallingConvention.FastCall;
                    }
                }
                else
                {
                    callingConvention = CallingConvention.ThisCall;
                }
            }
            else
            {
                callingConvention = CallingConvention.StdCall;
            }
            bool exactSpelling         = (pinvokeAttributes & PInvokeAttributes.NoMangle) > PInvokeAttributes.CharSetNotSpec;
            bool setLastError          = (pinvokeAttributes & PInvokeAttributes.SupportsLastError) > PInvokeAttributes.CharSetNotSpec;
            bool bestFitMapping        = (pinvokeAttributes & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
            bool throwOnUnmappableChar = (pinvokeAttributes & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
            bool preserveSig           = (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) > MethodImplAttributes.IL;

            return(new DllImportAttribute(dllName, entryPoint, charSet, exactSpelling, setLastError, preserveSig, callingConvention, bestFitMapping, throwOnUnmappableChar));
        }
示例#4
0
        internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
        {
            if ((method.Attributes & MethodAttributes.PinvokeImpl) == MethodAttributes.PrivateScope)
            {
                return((Attribute)null);
            }
            MetadataImport    metadataImport = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
            string            importDll      = (string)null;
            int               metadataToken  = method.MetadataToken;
            PInvokeAttributes attributes     = PInvokeAttributes.CharSetNotSpec;
            string            importName;

            metadataImport.GetPInvokeMap(metadataToken, out attributes, out importName, out importDll);
            CharSet charSet = CharSet.None;

            switch (attributes & PInvokeAttributes.CharSetMask)
            {
            case PInvokeAttributes.CharSetNotSpec:
                charSet = CharSet.None;
                break;

            case PInvokeAttributes.CharSetAnsi:
                charSet = CharSet.Ansi;
                break;

            case PInvokeAttributes.CharSetUnicode:
                charSet = CharSet.Unicode;
                break;

            case PInvokeAttributes.CharSetMask:
                charSet = CharSet.Auto;
                break;
            }
            CallingConvention callingConvention = CallingConvention.Cdecl;

            switch (attributes & PInvokeAttributes.CallConvMask)
            {
            case PInvokeAttributes.CallConvStdcall:
                callingConvention = CallingConvention.StdCall;
                break;

            case PInvokeAttributes.CallConvThiscall:
                callingConvention = CallingConvention.ThisCall;
                break;

            case PInvokeAttributes.CallConvFastcall:
                callingConvention = CallingConvention.FastCall;
                break;

            case PInvokeAttributes.CallConvWinapi:
                callingConvention = CallingConvention.Winapi;
                break;

            case PInvokeAttributes.CallConvCdecl:
                callingConvention = CallingConvention.Cdecl;
                break;
            }
            bool exactSpelling         = (uint)(attributes & PInvokeAttributes.NoMangle) > 0U;
            bool setLastError          = (uint)(attributes & PInvokeAttributes.SupportsLastError) > 0U;
            bool bestFitMapping        = (attributes & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
            bool throwOnUnmappableChar = (attributes & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
            bool preserveSig           = (uint)(method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) > 0U;

            return((Attribute) new DllImportAttribute(importDll, importName, charSet, exactSpelling, setLastError, preserveSig, callingConvention, bestFitMapping, throwOnUnmappableChar));
        }