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); }
private Attribute GetDllImportAttribute() { string entryPoint, dllName = null; int token = MetadataToken; PInvokeAttributes flags = 0; GetPInvoke(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 = InteropServicesCallingConvention.Cdecl; switch (flags & PInvokeAttributes.CallConvMask) { case PInvokeAttributes.CallConvWinapi: callingConvention = InteropServicesCallingConvention.Winapi; break; case PInvokeAttributes.CallConvCdecl: callingConvention = InteropServicesCallingConvention.Cdecl; break; case PInvokeAttributes.CallConvStdcall: callingConvention = InteropServicesCallingConvention.StdCall; break; case PInvokeAttributes.CallConvThiscall: callingConvention = InteropServicesCallingConvention.ThisCall; break; case PInvokeAttributes.CallConvFastcall: callingConvention = InteropServicesCallingConvention.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 = (GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0; return(new DllImportAttribute(dllName) { EntryPoint = entryPoint, CharSet = charSet, SetLastError = setLastError, ExactSpelling = exactSpelling, PreserveSig = preserveSig, BestFitMapping = bestFitMapping, ThrowOnUnmappableChar = throwOnUnmappableChar, CallingConvention = callingConvention }); }
/// <summary> /// Set or clear flags in <see cref="attributes"/> /// </summary> /// <param name="set"><c>true</c> if flags should be set, <c>false</c> if flags should /// be cleared</param> /// <param name="flags">Flags to set or clear</param> private void ModifyAttributes(bool set, PInvokeAttributes flags) { #if THREAD_SAFE int origVal, newVal; do { origVal = attributes; if (set) { newVal = origVal | (int)flags; } else { newVal = origVal & ~(int)flags; } } while (Interlocked.CompareExchange(ref attributes, newVal, origVal) != origVal); #else if (set) { attributes |= (int)flags; } else { attributes &= ~(int)flags; } #endif }
public unsafe ImplMap(byte *ptr, int moduleRefSize, int memberForwardedTagRefSize, int stringHandleSize) { MappingFlags = (PInvokeAttributes)Helpers.GetValue(ptr, 2); MemberForwarded = Helpers.FromMemberForwardedTag((uint)Helpers.GetValue(ptr + 2, memberForwardedTagRefSize)); ImportName = MetadataTokens.StringHandle(Helpers.GetValue(ptr + 2 + memberForwardedTagRefSize, stringHandleSize)); ImportScope = MetadataTokens.ModuleReferenceHandle(Helpers.GetValue(ptr + 2 + memberForwardedTagRefSize + stringHandleSize, moduleRefSize)); }
public void Read(ClrModuleReader reader) { this.MappingFlags = (PInvokeAttributes)reader.Binary.ReadUInt16(); this.MemberForwarded = reader.ReadCodedIndex <MemberForwarded>(); this.ImportName = reader.ReadString(); this.ImportScope = reader.ReadTableIndex(TableKind.ModuleRef); }
public void Read(ClrModuleReader reader) { this.MappingFlags = (PInvokeAttributes)reader.Binary.ReadUInt16(); this.MemberForwarded = reader.ReadCodedIndex<MemberForwarded>(); this.ImportName = reader.ReadString(); this.ImportScope = reader.ReadTableIndex(TableKind.ModuleRef); }
public PInvokeInfo (MethodDefinition meth, PInvokeAttributes attrs, string entryPoint, ModuleReference mod) : this (meth) { m_attributes = attrs; m_entryPoint = entryPoint; m_module = mod; }
public PInvokeInfo(MethodDefinition meth, PInvokeAttributes attrs, string entryPoint, ModuleReference mod) : this(meth) { m_attributes = attrs; m_entryPoint = entryPoint; m_module = mod; }
/// <summary> /// Initializes a new instance of the <see cref="ImplMapRow" /> struct. /// </summary> /// <param name="mappingFlags">The mapping flags.</param> /// <param name="memberForwarded">The member forwarded table.</param> /// <param name="importNameString">The import name string.</param> /// <param name="importScopeTable">The import scope table.</param> public ImplMapRow(PInvokeAttributes mappingFlags, Token memberForwarded, HeapIndexToken importNameString, Token importScopeTable) { MappingFlags = mappingFlags; MemberForwarded = memberForwarded; ImportNameString = importNameString; ImportScopeTable = importScopeTable; }
} // Read public static uint Read(this NativeReader reader, uint offset, out PInvokeAttributes value) { uint ivalue; offset = reader.DecodeUnsigned(offset, out ivalue); value = (PInvokeAttributes)ivalue; return(offset); } // Read
/// <summary> /// Initializes a new instance of the <see cref="ImplMapRow"/> struct. /// </summary> /// <param name="mappingFlags">The mapping flags.</param> /// <param name="memberForwardedTableIdx">The member forwarded table idx.</param> /// <param name="importNameStringIdx">The import name string idx.</param> /// <param name="importScopeTableIdx">The import scope table idx.</param> public ImplMapRow(PInvokeAttributes mappingFlags, Token memberForwardedTableIdx, HeapIndexToken importNameStringIdx, Token importScopeTableIdx) { _mappingFlags = mappingFlags; _memberForwardedTableIdx = memberForwardedTableIdx; _importNameStringIdx = importNameStringIdx; _importScopeTableIdx = importScopeTableIdx; }
[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)); }
string StringifyPInvokeAttrs(PInvokeAttributes attrs) { var sb = new StringBuilder(); sb.Append(Map(pinvoke_char_set_map, (uint)(attrs & PInvokeAttributes.CharSetMask))); sb.Append(Map(pinvoke_cconv_map, (uint)(attrs & PInvokeAttributes.CallConvMask))); sb.Append(MapFlags(pinvoke_flags_map, (uint)(attrs))); return(sb.ToString()); }
private void LoadData(CLIFile pFile) { MappingFlags = (PInvokeAttributes)pFile.ReadUInt16(); MemberForwarded.LoadData(pFile); ImportName = pFile.ReadStringHeap(pFile.ReadHeapIndex(HeapOffsetSizes.Strings32Bit)); int moduleRefIndex = 0; if (pFile.ModuleRefTable.Length >= 0xFFFF) moduleRefIndex = pFile.ReadInt32() - 1; else moduleRefIndex = pFile.ReadUInt16() - 1; if (moduleRefIndex >= 0) ImportScope = pFile.ModuleRefTable[moduleRefIndex]; }
public ImplMapRow CreateImplMapRow(PInvokeAttributes _mappingFlags, MetadataToken _memberForwarded, uint _importName, uint _importScope) { ImplMapRow row = new ImplMapRow(); row.MappingFlags = _mappingFlags; row.MemberForwarded = _memberForwarded; row.ImportName = _importName; row.ImportScope = _importScope; return(row); }
void SetFlagValue(PInvokeAttributes flag, bool value) { if (value) { Attributes |= flag; } else { Attributes &= ~flag; } }
public unsafe void GetPInvokeMap(int token, out PInvokeAttributes attributes, out string importName, out string importDll) { int attributes1; void *pStringHeap1; void *pStringHeap2; MetadataImport._GetPInvokeMap(this.m_metadataImport2, token, out attributes1, &pStringHeap1, &pStringHeap2); importName = new Utf8String(pStringHeap1).ToString(); importDll = new Utf8String(pStringHeap2).ToString(); attributes = (PInvokeAttributes)attributes1; }
public unsafe void GetPInvokeMap(int token, out PInvokeAttributes attributes, out string importName, out string importDll) { int num; void *voidPtr; void *voidPtr2; _GetPInvokeMap(this.m_metadataImport2, out MetadataArgs.Skip, token, out num, &voidPtr, &voidPtr2); importName = new Utf8String(voidPtr).ToString(); importDll = new Utf8String(voidPtr2).ToString(); attributes = (PInvokeAttributes)num; }
/// <summary> /// Modify <see cref="attributes"/> property: <see cref="attributes"/> = /// (<see cref="attributes"/> & <paramref name="andMask"/>) | <paramref name="orMask"/>. /// </summary> /// <param name="andMask">Value to <c>AND</c></param> /// <param name="orMask">Value to OR</param> void ModifyAttributes(PInvokeAttributes andMask, PInvokeAttributes orMask) { #if THREAD_SAFE int origVal, newVal; do { origVal = attributes; newVal = (origVal & (int)andMask) | (int)orMask; } while (Interlocked.CompareExchange(ref attributes, newVal, origVal) != origVal); #else attributes = (attributes & (int)andMask) | (int)orMask; #endif }
/// <summary> /// Modify <see cref="attributes"/> property: <see cref="attributes"/> = /// (<see cref="attributes"/> & <paramref name="andMask"/>) | <paramref name="orMask"/>. /// </summary> /// <param name="andMask">Value to <c>AND</c></param> /// <param name="orMask">Value to OR</param> private void ModifyAttributes(PInvokeAttributes andMask, PInvokeAttributes orMask) { #if THREAD_SAFE int origVal, newVal; do { origVal = attributes; newVal = (origVal & (int)andMask) | (int)orMask; } while (Interlocked.CompareExchange(ref attributes, newVal, origVal) != origVal); #else attributes = (attributes & (int)andMask) | (int)orMask; #endif }
public unsafe void GetPInvokeMap( int token, out PInvokeAttributes attributes, out string importName, out string importDll) { void *_importName, _importDll; _GetPInvokeMap(m_metadataImport2, token, out int _attributes, &_importName, &_importDll); importName = new MdUtf8String(_importName).ToString(); importDll = new MdUtf8String(_importDll).ToString(); attributes = (PInvokeAttributes)_attributes; }
/// <summary> /// Initialises a new instance of the ImplMapMetadataTableRow class /// </summary> /// <param name="content">The content of the file</param> /// <param name="offset">The offset of the current row</param> public ImplMapMetadataTableRow(byte[] content, Offset offset, ICodedIndexResolver resolver, IIndexDetails indexDetails) { this.FileOffset = offset; int sizeOfMemberForwardedIndex = resolver.GetSizeOfIndex(CodedIndexes.MemberForwarded); byte sizeOfStringIndex = indexDetails.GetSizeOfStringIndex(); byte sizeOfModuleRefIndex = indexDetails.GetSizeOfIndex(MetadataTables.ModuleRef); _mappingFlags = (PInvokeAttributes)FieldReader.ToUInt16(content, offset.Shift(2)); _memberForward = resolver.Resolve(CodedIndexes.MemberForwarded, FieldReader.ToUInt32(content, offset.Shift(sizeOfMemberForwardedIndex), sizeOfMemberForwardedIndex) ); _importName = new StringIndex(content, sizeOfStringIndex, offset); _importScope = new Index(content, offset, sizeOfModuleRefIndex); }
/// <summary> /// Converts unmanaged calling convention encoded as PInvokeAttributes to unmanaged /// calling convention encoded as MethodSignatureFlags. /// </summary> public static MethodSignatureFlags GetUnmanagedCallingConvention(PInvokeAttributes attributes) { switch (attributes & PInvokeAttributes.CallingConventionMask) { case PInvokeAttributes.CallingConventionWinApi: return MethodSignatureFlags.UnmanagedCallingConventionStdCall; // TODO: CDecl for varargs case PInvokeAttributes.CallingConventionCDecl: return MethodSignatureFlags.UnmanagedCallingConventionCdecl; case PInvokeAttributes.CallingConventionStdCall: return MethodSignatureFlags.UnmanagedCallingConventionStdCall; case PInvokeAttributes.CallingConventionThisCall: return MethodSignatureFlags.UnmanagedCallingConventionThisCall; default: throw new BadImageFormatException(); } }
/// <summary> /// Charset for marshalling strings /// </summary> private PInvokeAttributes GetCharSet() { PInvokeAttributes charset = _importMetadata.Attributes & PInvokeAttributes.CharSetMask; if (charset == 0) { // ECMA-335 II.10.1.5 - Default value is Ansi. charset = PInvokeAttributes.CharSetAnsi; } if (charset == PInvokeAttributes.CharSetAuto) { charset = PInvokeAttributes.CharSetUnicode; } return(charset); }
/// <summary> /// Set or clear flags in <see cref="attributes"/> /// </summary> /// <param name="set"><c>true</c> if flags should be set, <c>false</c> if flags should /// be cleared</param> /// <param name="flags">Flags to set or clear</param> void ModifyAttributes(bool set, PInvokeAttributes flags) { #if THREAD_SAFE int origVal, newVal; do { origVal = attributes; if (set) newVal = origVal | (int)flags; else newVal = origVal & ~(int)flags; } while (Interlocked.CompareExchange(ref attributes, newVal, origVal) != origVal); #else if (set) attributes |= (int)flags; else attributes &= ~(int)flags; #endif }
/// <summary> /// Converts unmanaged calling convention encoded as PInvokeAttributes to unmanaged /// calling convention encoded as MethodSignatureFlags. /// </summary> public static MethodSignatureFlags GetUnmanagedCallingConvention(PInvokeAttributes attributes) { switch (attributes & PInvokeAttributes.CallingConventionMask) { case PInvokeAttributes.CallingConventionWinApi: return(MethodSignatureFlags.UnmanagedCallingConventionStdCall); // TODO: CDecl for varargs case PInvokeAttributes.CallingConventionCDecl: return(MethodSignatureFlags.UnmanagedCallingConventionCdecl); case PInvokeAttributes.CallingConventionStdCall: return(MethodSignatureFlags.UnmanagedCallingConventionStdCall); case PInvokeAttributes.CallingConventionThisCall: return(MethodSignatureFlags.UnmanagedCallingConventionThisCall); default: throw new BadImageFormatException(); } }
internal static string ToString(PInvokeAttributes flags) { var sb = new StringBuilder(); switch ((flags & PInvokeAttributes.CharSetMask)) { case PInvokeAttributes.CharSetNotSpec: sb.Append("CharSetNotSpec"); break; case PInvokeAttributes.CharSetAnsi: sb.Append("CharSetAnsi"); break; case PInvokeAttributes.CharSetUnicode: sb.Append("CharSetUnicode"); break; case PInvokeAttributes.CharSetAuto: sb.Append("CharSetAuto"); break; } if ((flags & PInvokeAttributes.NoMangle) != 0) sb.Append(" | NoMangle"); switch ((flags & PInvokeAttributes.BestFitMask)) { case PInvokeAttributes.BestFitUseAssem: sb.Append(" | BestFitUseAssem"); break; case PInvokeAttributes.BestFitEnabled: sb.Append(" | BestFitEnabled"); break; case PInvokeAttributes.BestFitDisabled: sb.Append(" | BestFitDisabled"); break; default: sb.Append(" | BestFit_UNKNOWN"); break; } switch ((flags & PInvokeAttributes.ThrowOnUnmappableCharMask)) { case PInvokeAttributes.ThrowOnUnmappableCharUseAssem: sb.Append(" | ThrowOnUnmappableCharUseAssem"); break; case PInvokeAttributes.ThrowOnUnmappableCharEnabled: sb.Append(" | ThrowOnUnmappableCharEnabled"); break; case PInvokeAttributes.ThrowOnUnmappableCharDisabled: sb.Append(" | ThrowOnUnmappableCharDisabled"); break; default: sb.Append(" | ThrowOnUnmappableChar_UNKNOWN"); break; } if ((flags & PInvokeAttributes.SupportsLastError) != 0) sb.Append(" | SupportsLastError"); switch ((flags & PInvokeAttributes.CallConvMask)) { case PInvokeAttributes.CallConvWinapi: sb.Append(" | CallConvWinapi"); break; case PInvokeAttributes.CallConvCdecl: sb.Append(" | CallConvCdecl"); break; case PInvokeAttributes.CallConvStdcall: sb.Append(" | CallConvStdcall"); break; case PInvokeAttributes.CallConvThiscall: sb.Append(" | CallConvThiscall"); break; case PInvokeAttributes.CallConvFastcall: sb.Append(" | CallConvFastcall"); break; default: sb.Append(" | CallConv_UNKNOWN"); break; } return sb.ToString(); }
private void Add(PInvokeAttributes a) { }
} // Read public static uint Read(this NativeReader reader, uint offset, out PInvokeAttributes value) { uint ivalue; offset = reader.DecodeUnsigned(offset, out ivalue); value = (PInvokeAttributes)ivalue; return offset; } // Read
void Add(PInvokeAttributes a) { }
private CustomAttributeData GetDllImportAttributeData() { if ((Attributes & MethodAttributes.PinvokeImpl) == 0) { return(null); } string entryPoint, dllName = null; PInvokeAttributes flags = 0; GetPInvoke(out flags, out entryPoint, out dllName); CharSet charSet; 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: charSet = CharSet.None; break; } InteropServicesCallingConvention callingConvention; switch (flags & PInvokeAttributes.CallConvMask) { case PInvokeAttributes.CallConvWinapi: callingConvention = InteropServicesCallingConvention.Winapi; break; case PInvokeAttributes.CallConvCdecl: callingConvention = InteropServicesCallingConvention.Cdecl; break; case PInvokeAttributes.CallConvStdcall: callingConvention = InteropServicesCallingConvention.StdCall; break; case PInvokeAttributes.CallConvThiscall: callingConvention = InteropServicesCallingConvention.ThisCall; break; case PInvokeAttributes.CallConvFastcall: callingConvention = InteropServicesCallingConvention.FastCall; break; // Invalid: default to CallingConvention.Cdecl default: callingConvention = InteropServicesCallingConvention.Cdecl; 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 = (GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0; var ctorArgs = new CustomAttributeTypedArgument [] { new CustomAttributeTypedArgument(typeof(string), dllName), }; var 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)); }
internal static string ToString(PInvokeAttributes flags) { var sb = new StringBuilder(); switch ((flags & PInvokeAttributes.CharSetMask)) { case PInvokeAttributes.CharSetNotSpec: sb.Append("CharSetNotSpec"); break; case PInvokeAttributes.CharSetAnsi: sb.Append("CharSetAnsi"); break; case PInvokeAttributes.CharSetUnicode: sb.Append("CharSetUnicode"); break; case PInvokeAttributes.CharSetAuto: sb.Append("CharSetAuto"); break; } if ((flags & PInvokeAttributes.NoMangle) != 0) { sb.Append(" | NoMangle"); } switch ((flags & PInvokeAttributes.BestFitMask)) { case PInvokeAttributes.BestFitUseAssem: sb.Append(" | BestFitUseAssem"); break; case PInvokeAttributes.BestFitEnabled: sb.Append(" | BestFitEnabled"); break; case PInvokeAttributes.BestFitDisabled: sb.Append(" | BestFitDisabled"); break; default: sb.Append(" | BestFit_UNKNOWN"); break; } switch ((flags & PInvokeAttributes.ThrowOnUnmappableCharMask)) { case PInvokeAttributes.ThrowOnUnmappableCharUseAssem: sb.Append(" | ThrowOnUnmappableCharUseAssem"); break; case PInvokeAttributes.ThrowOnUnmappableCharEnabled: sb.Append(" | ThrowOnUnmappableCharEnabled"); break; case PInvokeAttributes.ThrowOnUnmappableCharDisabled: sb.Append(" | ThrowOnUnmappableCharDisabled"); break; default: sb.Append(" | ThrowOnUnmappableChar_UNKNOWN"); break; } if ((flags & PInvokeAttributes.SupportsLastError) != 0) { sb.Append(" | SupportsLastError"); } switch ((flags & PInvokeAttributes.CallConvMask)) { case PInvokeAttributes.CallConvWinapi: sb.Append(" | CallConvWinapi"); break; case PInvokeAttributes.CallConvCdecl: sb.Append(" | CallConvCdecl"); break; case PInvokeAttributes.CallConvStdcall: sb.Append(" | CallConvStdcall"); break; case PInvokeAttributes.CallConvThiscall: sb.Append(" | CallConvThiscall"); break; case PInvokeAttributes.CallConvFastcall: sb.Append(" | CallConvFastcall"); break; default: sb.Append(" | CallConv_UNKNOWN"); break; } return(sb.ToString()); }
void SetFlagValue(PInvokeAttributes flag, bool value) { if (value) Attributes |= flag; else Attributes &= ~flag; }
} // Write public static void Write(this NativeWriter writer, PInvokeAttributes value) { writer.WriteUnsigned((uint)value); } // Write
/// <summary> /// Constructor /// </summary> /// <param name="scope">Scope</param> /// <param name="name">Name</param> /// <param name="flags">Flags</param> public ImplMapUser(ModuleRef scope, UTF8String name, PInvokeAttributes flags) { this.module = scope; this.name = name; this.attributes = (int)flags; }
bool GetFlagValue(PInvokeAttributes flag) { return((Attributes & flag) != 0); }
[System.Security.SecurityCritical] // auto-generated public unsafe void GetPInvokeMap( int token, out PInvokeAttributes attributes, out String importName, out String importDll) { int _attributes; void* _importName, _importDll; _GetPInvokeMap(m_metadataImport2, token, out _attributes, &_importName, &_importDll); importName = new Utf8String(_importName).ToString(); importDll = new Utf8String(_importDll).ToString(); attributes = (PInvokeAttributes)_attributes; }
internal extern void GetPInvoke (out PInvokeAttributes flags, out string entryPoint, out string dllName);
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)); }
public unsafe static bool GetPinvokeMapProps(IMetaDataImport mdi, uint token, out PInvokeAttributes attrs, out uint moduleToken) { attrs = 0; moduleToken = 0; if (mdi == null) return false; uint dwMappingFlags, mrImportDLL; int hr = mdi.GetPinvokeMap(token, new IntPtr(&dwMappingFlags), IntPtr.Zero, 0, IntPtr.Zero, new IntPtr(&mrImportDLL)); if (hr != 0) return false; attrs = (PInvokeAttributes)dwMappingFlags; moduleToken = mrImportDLL; return true; }
public PInvokeMetadata(string module, string entrypoint, PInvokeAttributes attributes) { Name = entrypoint; Module = module; Attributes = attributes; }
bool GetFlagValue(PInvokeAttributes flag) { return (Attributes & flag) != 0; }
public PInvokeInfo(PInvokeAttributes attributes, string entryPoint, ModuleReference module) { this.attributes = (ushort) attributes; this.entry_point = entryPoint; this.module = module; }
internal extern void GetPInvoke(out PInvokeAttributes flags, out string entryPoint, out string dllName);
/// <summary> /// Constructor /// </summary> /// <param name="scope">Scope</param> /// <param name="name">Name</param> /// <param name="flags">Flags</param> public ImplMapUser(ModuleRef scope, UTF8String name, PInvokeAttributes flags) { this.scope = scope; this.name = name; this.flags = flags; }
public unsafe void GetPInvokeMap(int token, out PInvokeAttributes attributes, out string importName, out string importDll) { int num; void* voidPtr; void* voidPtr2; _GetPInvokeMap(this.m_metadataImport2, out MetadataArgs.Skip, token, out num, &voidPtr, &voidPtr2); importName = new Utf8String(voidPtr).ToString(); importDll = new Utf8String(voidPtr2).ToString(); attributes = (PInvokeAttributes) num; }
string StringifyPInvokeAttrs (PInvokeAttributes attrs) { var sb = new StringBuilder (); sb.Append (Map (pinvoke_char_set_map, (uint)(attrs & PInvokeAttributes.CharSetMask))); sb.Append (Map (pinvoke_cconv_map, (uint)(attrs & PInvokeAttributes.CallConvMask))); sb.Append (MapFlags (pinvoke_flags_map, (uint)(attrs))); return sb.ToString (); }
public PInvokeMetadata(string name, PInvokeAttributes attributes) { Name = name; Attributes = attributes; }
public ImplMapRow CreateImplMapRow(PInvokeAttributes _mappingFlags, MetadataToken _memberForwarded, uint _importName, uint _importScope) { ImplMapRow row = new ImplMapRow (); row.MappingFlags = _mappingFlags; row.MemberForwarded = _memberForwarded; row.ImportName = _importName; row.ImportScope = _importScope; return row; }
/// <summary> /// Fills the row from the array of bytes. /// </summary> unsafe public void FromRawData(byte [] buff, int offs) { if (buff == null) throw new Exception("buff == null"); if (offs + Size > buff.Length) throw new Exception("bounds"); this.MappingFlags = (PInvokeAttributes) LEBitConverter.ToUInt16(buff, offs); offs += sizeof (ushort); this.MemberForwarded = TabsDecoder.DecodeToken(CodedTokenId.MemberForwarded, LEBitConverter.ToInt32(buff, offs)); offs += 4; this.ImportName = LEBitConverter.ToInt32(buff, offs); offs += 4; this.ImportScope = LEBitConverter.ToInt32(buff, offs); }
public PInvokeInfo(PInvokeAttributes attributes, string entryPoint, ModuleReference module) { this.attributes = (ushort)attributes; this.entry_point = entryPoint; this.module = module; }