private CustomAttributeData?ComputeDllImportCustomAttributeDataIfAny() { if ((Attributes & MethodAttributes.PinvokeImpl) == 0) { return(null); } // Make sure all the necessary framework types exist in this MetadataLoadContext's core assembly. If one doesn't, skip. CoreTypes ct = Loader.GetAllFoundCoreTypes(); if (ct[CoreType.String] == null || ct[CoreType.Boolean] == null || ct[CoreType.DllImportAttribute] == null || ct[CoreType.CharSet] == null || ct[CoreType.CallingConvention] == null) { return(null); } ConstructorInfo?ctor = Loader.TryGetDllImportCtor(); if (ctor == null) { return(null); } Func <CustomAttributeArguments> argumentsPromise = () => { // The expensive work goes in here. Type attributeType = ctor.DeclaringType !; DllImportAttribute dia = _decoder.ComputeDllImportAttribute(); CustomAttributeTypedArgument[] cats = { new CustomAttributeTypedArgument(ct[CoreType.String] !, dia.Value) };
internal object[] GetPseudoCustomAttributes() { int num = 0; MonoMethodInfo methodInfo = MonoMethodInfo.GetMethodInfo(this.mhandle); if ((methodInfo.iattrs & MethodImplAttributes.PreserveSig) != MethodImplAttributes.IL) { num++; } if ((methodInfo.attrs & MethodAttributes.PinvokeImpl) != MethodAttributes.PrivateScope) { num++; } if (num == 0) { return(null); } object[] array = new object[num]; num = 0; if ((methodInfo.iattrs & MethodImplAttributes.PreserveSig) != MethodImplAttributes.IL) { array[num++] = new PreserveSigAttribute(); } if ((methodInfo.attrs & MethodAttributes.PinvokeImpl) != MethodAttributes.PrivateScope) { DllImportAttribute dllImportAttribute = MonoMethod.GetDllImportAttribute(this.mhandle); if ((methodInfo.iattrs & MethodImplAttributes.PreserveSig) != MethodImplAttributes.IL) { dllImportAttribute.PreserveSig = true; } array[num++] = dllImportAttribute; } return(array); }
public DllImportAttributeTest() { //create a dynamic assembly with the required attribute //and check for the validity dynAsmName.Name = "TestAssembly"; dynAssembly = Thread.GetDomain().DefineDynamicAssembly( dynAsmName, AssemblyBuilderAccess.Run ); // Set the required Attribute of the assembly. Type attribute = typeof(DllImportAttribute); ConstructorInfo ctrInfo = attribute.GetConstructor( new Type [] { typeof(string) } ); CustomAttributeBuilder attrBuilder = new CustomAttributeBuilder(ctrInfo, new object [1] { "libc.dylib" }); dynAssembly.SetCustomAttribute(attrBuilder); object [] attributes = dynAssembly.GetCustomAttributes(true); attr = attributes [0] as DllImportAttribute; }
public void SetCustomAttribute_DllImport1() { string mname = genMethodName(); TypeBuilder tb = module.DefineType(genTypeName(), TypeAttributes.Public); MethodBuilder mb = tb.DefineMethod( mname, MethodAttributes.Public, typeof(void), new Type [] { typeof(int), typeof(string) }); // Create an attribute with default values mb.SetCustomAttribute(new CustomAttributeBuilder(typeof(DllImportAttribute).GetConstructor(new Type[] { typeof(string) }), new object[] { "kernel32" })); Type t = tb.CreateType(); DllImportAttribute attr = (DllImportAttribute)((t.GetMethod(mname).GetCustomAttributes(typeof(DllImportAttribute), true)) [0]); Assert.AreEqual(CallingConvention.Winapi, attr.CallingConvention, "#1"); Assert.AreEqual(mname, attr.EntryPoint, "#2"); Assert.AreEqual("kernel32", attr.Value, "#3"); Assert.IsFalse(attr.ExactSpelling, "#4"); Assert.IsTrue(attr.PreserveSig, "#5"); Assert.IsFalse(attr.SetLastError, "#6"); Assert.IsFalse(attr.BestFitMapping, "#7"); Assert.IsFalse(attr.ThrowOnUnmappableChar, "#8"); }
public void PseudoCustomAttributes() { Type t = typeof(MethodInfoTest); DllImportAttribute attr = (DllImportAttribute)((t.GetMethod("dllImportMethod").GetCustomAttributes(typeof(DllImportAttribute), true)) [0]); Assert.AreEqual(CallingConvention.Winapi, attr.CallingConvention, "#1"); #if MONOTOUCH || FULL_AOT_RUNTIME Assert.AreEqual("readlink", attr.EntryPoint, "#2"); Assert.AreEqual("libc", attr.Value, "#3"); #else Assert.AreEqual("foo", attr.EntryPoint, "#2"); Assert.AreEqual("libfoo", attr.Value, "#3"); #endif Assert.AreEqual(CharSet.Unicode, attr.CharSet, "#4"); Assert.AreEqual(false, attr.ExactSpelling, "#5"); Assert.AreEqual(true, attr.PreserveSig, "#6"); Assert.AreEqual(true, attr.SetLastError, "#7"); Assert.AreEqual(true, attr.BestFitMapping, "#8"); Assert.AreEqual(true, attr.ThrowOnUnmappableChar, "#9"); PreserveSigAttribute attr2 = (PreserveSigAttribute)((t.GetMethod("preserveSigMethod").GetCustomAttributes(true)) [0]); // This doesn't work under MS.NET /* * MethodImplAttribute attr3 = (MethodImplAttribute)((t.GetMethod ("synchronizedMethod").GetCustomAttributes (true)) [0]); */ }
protected MethodInfo GrabPInvokeImpl(MethodInfo Original, TypeBuilder On) { if (On == null) { On = GrabType(Original.DeclaringType) as TypeBuilder; } DllImportAttribute Attr = FindCustomAttribute <DllImportAttribute>(Original); if (Attr == null) { throw new InvalidOperationException("P/Invoke method without a DllImportAttribute"); } Type ReturnType = GrabType(Original.ReturnType); Type[] Parameters = ParameterTypes(Original); if (MethodsDone.ContainsKey(Original)) { return(MethodsDone[Original]); } MethodBuilder PInvoke = On.DefinePInvokeMethod(Original.Name, Attr.Value, Original.Attributes, Original.CallingConvention, ReturnType, Parameters, Attr.CallingConvention, Attr.CharSet); PInvoke.SetImplementationFlags(Original.GetMethodImplementationFlags()); MethodsDone.Add(Original, PInvoke); return(PInvoke); }
static NvapiNativeMethods() { DllImportAttribute attribute = new DllImportAttribute("nvapi64.dll"); attribute.CallingConvention = CallingConvention.Cdecl; attribute.PreserveSig = true; attribute.EntryPoint = "nvapi_QueryInterface"; PInvokeDelegateFactory.CreateDelegate(attribute, typeof(NvQueryInterfaceDelegate), out object newDelegate); NvQueryInterface = (NvQueryInterfaceDelegate)newDelegate; try { IntPtr ptr = NvQueryInterface(0x0150E828); NvInitialize = (NvInitializeDelegate)Marshal.GetDelegateForFunctionPointer(ptr, typeof(NvInitializeDelegate)); } catch (Exception e) { Logger.ErrorDebugLine(e); return; } if (NvInitialize() == NvStatus.NVAPI_OK) { Type t = typeof(NvapiNativeMethods); var properties = t.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.SetProperty); foreach (var property in properties) { var id = ((IdAttribute)property.GetCustomAttributes(typeof(IdAttribute), inherit: false).First()).Id; SetDelegate(property, id); } } }
public void SetCustomAttribute_DllImport2() { string mname = genMethodName(); TypeBuilder tb = module.DefineType(genTypeName(), TypeAttributes.Public); MethodBuilder mb = tb.DefineMethod( mname, MethodAttributes.Public, typeof(void), new Type [] { typeof(int), typeof(string) }); CustomAttributeBuilder cb = new CustomAttributeBuilder(typeof(DllImportAttribute).GetConstructor(new Type [] { typeof(String) }), new object [] { "foo" }, new FieldInfo [] { typeof(DllImportAttribute).GetField("EntryPoint"), typeof(DllImportAttribute).GetField("CallingConvention"), typeof(DllImportAttribute).GetField("CharSet"), typeof(DllImportAttribute).GetField("ExactSpelling"), typeof(DllImportAttribute).GetField("PreserveSig") }, new object [] { "bar", CallingConvention.StdCall, CharSet.Unicode, true, false }); mb.SetCustomAttribute(cb); Type t = tb.CreateType(); DllImportAttribute attr = (DllImportAttribute)((t.GetMethod(mname).GetCustomAttributes(typeof(DllImportAttribute), true)) [0]); Assert.AreEqual(CallingConvention.StdCall, attr.CallingConvention, "#1"); Assert.AreEqual(CharSet.Unicode, attr.CharSet, "#2"); Assert.AreEqual("bar", attr.EntryPoint, "#3"); Assert.AreEqual("foo", attr.Value, "#4"); Assert.IsTrue(attr.ExactSpelling, "#5"); Assert.IsFalse(attr.PreserveSig, "#6"); Assert.IsFalse(attr.SetLastError, "#7"); Assert.IsFalse(attr.BestFitMapping, "#8"); Assert.IsFalse(attr.ThrowOnUnmappableChar, "#9"); }
private static Type CreateWrapperType(Type delegateType, DllImportAttribute dllImportAttribute) { TypeBuilder typeBuilder = moduleBuilder.DefineType("PInvokeDelegateFactoryInternalWrapperType" + wrapperTypes.Count); MethodInfo methodInfo = delegateType.GetMethod("Invoke"); ParameterInfo[] parameterInfos = methodInfo.GetParameters(); int parameterCount = parameterInfos.GetLength(0); Type[] parameterTypes = new Type[parameterCount]; for (int i = 0; i < parameterCount; i++) { parameterTypes[i] = parameterInfos[i].ParameterType; } MethodBuilder methodBuilder = typeBuilder.DefinePInvokeMethod( dllImportAttribute.EntryPoint, dllImportAttribute.Value, MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.PinvokeImpl, CallingConventions.Standard, methodInfo.ReturnType, parameterTypes, dllImportAttribute.CallingConvention, dllImportAttribute.CharSet); foreach (ParameterInfo parameterInfo in parameterInfos) { methodBuilder.DefineParameter(parameterInfo.Position + 1, parameterInfo.Attributes, parameterInfo.Name); } if (dllImportAttribute.PreserveSig) { methodBuilder.SetImplementationFlags(MethodImplAttributes.PreserveSig); } return(typeBuilder.CreateType()); }
static NVAPI() { DllImportAttribute attribute = new DllImportAttribute(GetDllName()); attribute.CallingConvention = CallingConvention.Cdecl; attribute.PreserveSig = true; attribute.EntryPoint = "nvapi_QueryInterface"; PInvokeDelegateFactory.CreateDelegate(attribute, out nvapi_QueryInterface); try { GetDelegate(0x0150E828, out NvAPI_Initialize); } catch (DllNotFoundException) { return; } catch (ArgumentNullException) { return; } if (NvAPI_Initialize() == NvStatus.OK) { GetDelegate(0xE3640A56, out NvAPI_GPU_GetThermalSettings); GetDelegate(0xCEEE8E9F, out _NvAPI_GPU_GetFullName); GetDelegate(0x9ABDD40D, out NvAPI_EnumNvidiaDisplayHandle); GetDelegate(0x34EF9506, out NvAPI_GetPhysicalGPUsFromDisplay); GetDelegate(0xE5AC921F, out NvAPI_EnumPhysicalGPUs); GetDelegate(0x5F608315, out NvAPI_GPU_GetTachReading); available = true; } }
static NVAPI() { DllImportAttribute attribute = new DllImportAttribute(GetDllName()); attribute.CallingConvention = CallingConvention.Cdecl; attribute.PreserveSig = true; attribute.EntryPoint = "nvapi_QueryInterface"; PInvokeDelegateFactory.CreateDelegate(attribute, out nvapi_QueryInterface); try { GetDelegate(0x0150E828, out NvAPI_Initialize); } catch (DllNotFoundException) { return; } catch (EntryPointNotFoundException) { return; } catch (ArgumentNullException) { return; } if (NvAPI_Initialize() == NvStatus.OK) { GetDelegate(0xE3640A56, out NvAPI_GPU_GetThermalSettings); GetDelegate(0xCEEE8E9F, out _NvAPI_GPU_GetFullName); GetDelegate(0x9ABDD40D, out NvAPI_EnumNvidiaDisplayHandle); GetDelegate(0x34EF9506, out NvAPI_GetPhysicalGPUsFromDisplay); GetDelegate(0xE5AC921F, out NvAPI_EnumPhysicalGPUs); GetDelegate(0x5F608315, out NvAPI_GPU_GetTachReading); GetDelegate(0x1BD69F49, out NvAPI_GPU_GetAllClocks); GetDelegate(0x60DED2ED, out NvAPI_GPU_GetPStates); GetDelegate(0x189A1FDF, out NvAPI_GPU_GetUsages); GetDelegate(0xDA141340, out NvAPI_GPU_GetCoolerSettings); GetDelegate(0x774AA982, out NvAPI_GPU_GetMemoryInfo); GetDelegate(0xF951A4D1, out NvAPI_GetDisplayDriverVersion); GetDelegate(0x01053FA5, out _NvAPI_GetInterfaceVersionString); available = true; } }
static Nvapi() { DllImportAttribute attribute = new DllImportAttribute(GetDllName()); attribute.CallingConvention = CallingConvention.Cdecl; attribute.PreserveSig = true; attribute.EntryPoint = "nvapi_QueryInterface"; PInvokeDelegateFactory.CreateDelegate(attribute, out QueryInterface); try { GetDelegate(NvId_Initialize, out UnloadInternal); } catch (DllNotFoundException) { return; } catch (EntryPointNotFoundException) { return; } catch (ArgumentNullException) { return; } if (UnloadInternal() == Status.NVAPI_OK) { GetDelegate(NvId_Unload, out UnloadInternal); GetDelegate(NvId_GetInterfaceVersionString, out GetInterfaceVersionStringInternal); GetDelegate(NvId_GetErrorMessage, out GetErrorMessageInternal); // Display GetDelegate(NvId_EnumNvidiaDisplayHandle, out EnumNvidiaDisplayHandleInternal); GetDelegate(NvId_EnumNvidiaUnAttachedDisplayHandle, out EnumNvidiaUnAttachedDisplayHandleInternal); GetDelegate(NvId_GetAssociatedNvidiaDisplayHandle, out GetAssociatedNvidiaDisplayHandleInternal); GetDelegate(NvId_GetAssociatedUnAttachedNvidiaDisplayHandle, out GetAssociatedUnAttachedNvidiaDisplayHandleInternal); available = true; } AppDomain.CurrentDomain.ProcessExit += Nvapi.OnExit; }
static NVAPI() { DllImportAttribute attribute = new DllImportAttribute(GetDllName()); attribute.CallingConvention = CallingConvention.Cdecl; attribute.PreserveSig = true; attribute.EntryPoint = "nvapi_QueryInterface"; PInvokeDelegateFactory.CreateDelegate(attribute, out nvapi_QueryInterface); try { GetDelegate(0x0150E828, out NvAPI_Initialize); } catch (DllNotFoundException) { return; } catch (EntryPointNotFoundException) { return; } catch (ArgumentNullException) { return; } if (NvAPI_Initialize() == NvStatus.OK) { GetDelegate(0xCEEE8E9F, out _NvAPI_GPU_GetFullName); GetDelegate(0xE5AC921F, out NvAPI_EnumPhysicalGPUs); GetDelegate(0x189A1FDF, out NvAPI_GPU_GetUsages); available = true; } }
internal object[] GetPseudoCustomAttributes() { int count = 0; /* MS.NET doesn't report MethodImplAttribute */ MonoMethodInfo info = MonoMethodInfo.GetMethodInfo(mhandle); if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0) { count++; } if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) { count++; } if (count == 0) { return(null); } object[] attrs = new object [count]; count = 0; if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0) { attrs [count++] = new PreserveSigAttribute(); } if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) { attrs [count++] = DllImportAttribute.GetCustomAttribute(this); } return(attrs); }
static NVAPI() { DllImportAttribute attribute = new DllImportAttribute("nvapi64.dll") { CallingConvention = CallingConvention.Cdecl, PreserveSig = true, EntryPoint = "nvapi_QueryInterface" }; PInvokeDelegateFactory.CreateDelegate(attribute, out nvapi_QueryInterface); try { GetDelegate(0x0150E828, out NvAPI_Initialize); } catch (Exception e) { Helpers.ConsolePrint("NVAPI", e.ToString()); return; } if (NvAPI_Initialize() == NvStatus.OK) { GetDelegate(0x5F608315, out NvAPI_GPU_GetTachReading); GetDelegate(0x60DED2ED, out NvAPI_GPU_GetPStates); GetDelegate(0xE3640A56, out NvAPI_GPU_GetThermalSettings); GetDelegate(0xE5AC921F, out NvAPI_EnumPhysicalGPUs); GetDelegate(0x1BE0B8E5, out NvAPI_GPU_GetBusID); } available = true; }
public unsafe void LibraryNames() { var methods = typeof(LibC).GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); foreach (var method in methods) { DllImportAttribute dllImportAttribute = method.GetCustomAttribute <DllImportAttribute>(); if (dllImportAttribute != null) { string functionName = dllImportAttribute.EntryPoint; string libName = dllImportAttribute.Value; IntPtr libHandle = IntPtr.Zero; lock (s_dlopened) { if (!s_dlopened.TryGetValue(libName, out libHandle)) { fixed(byte *filename = Encoding.UTF8.GetBytes(libName)) { libHandle = new IntPtr(LibC.dlopen(filename, LibC.RTLD_LAZY)); } Assert.True(IntPtr.Zero != libHandle, $"Library not found: {libName}"); s_dlopened.Add(libName, libHandle); } } fixed(byte *symbol = Encoding.UTF8.GetBytes(functionName)) { IntPtr symbolHandle = new IntPtr(LibC.dlsym(libHandle.ToPointer(), symbol)); Assert.True(IntPtr.Zero != symbolHandle, $"Function symbol not found in {libName}: {functionName}"); } } } }
private NVAPIService() { DllImportAttribute attribute = new DllImportAttribute(GetLibraryName()); attribute.CallingConvention = CallingConvention.Cdecl; attribute.PreserveSig = true; attribute.EntryPoint = "nvapi_QueryInterface"; PInvokeDelegateFactory.CreateDelegate(attribute, out nvapi_QueryInterface); GetNvAPIDelegate(0x0150E828, out NvAPI_Initialize); NvAPI_Initialize(); GetNvAPIDelegate(0xE5AC921F, out NvAPI_EnumPhysicalGPUs); GetNvAPIDelegate(0xD9930B07, out NvAPI_EnumTCCPhysicalGPUs); GetNvAPIDelegate(0x9ABDD40D, out NvAPI_EnumNvidiaDisplayHandle); GetNvAPIDelegate(0x34EF9506, out NvAPI_GetPhysicalGPUsFromDisplay); GetNvAPIDelegate(0x1BE0B8E5, out NvAPI_GPU_GetBusId); GetNvAPIDelegate(0xE3640A56, out NvAPI_GPU_GetThermalSettings); GetNvAPIDelegate(0x60DED2ED, out NvAPI_GPU_GetDynamicPstatesInfoEx); GetNvAPIDelegate(0x189A1FDF, out NvAPI_GPU_GetDynamicPstatesInfo); { NvPhysicalGpuHandle[] physicalGpuHandles = new NvPhysicalGpuHandle[MAX_PHYSICAL_GPUS]; if (NvAPI_EnumPhysicalGPUs(physicalGpuHandles, out int physicalGpuCount) == 0) { nvPhysicalGpuHandles.AddRange(physicalGpuHandles.Where((handle, i) => i < physicalGpuCount && handle.IsValid).ToList()); } NvPhysicalGpuHandle[] TCCPhysicalGpuHandles = new NvPhysicalGpuHandle[MAX_PHYSICAL_GPUS]; if (NvAPI_EnumTCCPhysicalGPUs(TCCPhysicalGpuHandles, out int TCCPhysicalGpuCount) == 0) { nvPhysicalGpuHandles.AddRange(TCCPhysicalGpuHandles.Where((handle, i) => i < TCCPhysicalGpuCount && handle.IsValid).ToList()); } } }
static NVAPI() { DllImportAttribute attribute = new DllImportAttribute("nvapi64.dll"); attribute.CallingConvention = CallingConvention.Cdecl; attribute.PreserveSig = true; attribute.EntryPoint = "nvapi_QueryInterface"; PInvokeDelegateFactory.CreateDelegate(attribute, out nvapi_QueryInterface); try { GetDelegate(0x0150E828, out NvAPI_Initialize); } catch (Exception e) { Logger.Info("NVAPI", e.ToString()); return; } if (NvAPI_Initialize() == NvStatus.OK) { GetDelegate(0x5F608315, out NvAPI_GPU_GetTachReading); GetDelegate(0x60DED2ED, out NvAPI_GPU_GetPStates); GetDelegate(0xE3640A56, out NvAPI_GPU_GetThermalSettings); GetDelegate(0xE5AC921F, out NvAPI_EnumPhysicalGPUs); GetDelegate(0x1BE0B8E5, out NvAPI_GPU_GetBusID); GetDelegate(0x34206D86, out NvAPI_DLL_ClientPowerPoliciesGetInfo); GetDelegate(0x70916171, out NvAPI_DLL_ClientPowerPoliciesGetStatus); GetDelegate(0xAD95F5ED, out NvAPI_DLL_ClientPowerPoliciesSetStatus); GetDelegate(0xDA141340, out NvAPI_GPU_GetCoolerLevels); GetDelegate(0x891FA0AE, out NvAPI_GPU_SetCoolerLevels); } available = true; }
public static bool Initialize() { if (_nvApiInitialize != null) { return(true); } var attribute = new DllImportAttribute(DllName) { CallingConvention = CallingConvention.Cdecl, PreserveSig = true, EntryPoint = "nvapi_QueryInterface" }; Native.CreatePInvokeDelegate(attribute, out _nvApiQueryInterface); try { GetDelegate((uint)FunctionId.NvAPI_Initialize, out _nvApiInitialize); _initStatus = _nvApiInitialize(); if (_initStatus == NvStatus.Ok) { SetupDelegates(); } return(true); } catch (DllNotFoundException e) { ModuleGpuNvidia.Logger.Error($"Could not found nvapi dll '{DllName}'", e); } catch (EntryPointNotFoundException e) { ModuleGpuNvidia.Logger.Error($"Could not found entry point in '{DllName}'", e); } catch (ArgumentNullException e) { ModuleGpuNvidia.Logger.Error($"Could not query pointer to NvAPI_Initialize in '{DllName}'", e); } return(false); }
private static void GetDelegate <T>(string entryPoint, out T newDelegate) where T : class { DllImportAttribute attribute = new DllImportAttribute(getLibraryName()); attribute.CallingConvention = CallingConvention.Cdecl; attribute.PreserveSig = true; attribute.EntryPoint = entryPoint; PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate); }
private void CheckMember(Type type, MemberInfo mi, AssemblyCheckInfo report) { DllImportAttribute[] attributes = null; MethodBase[] methods = null; switch (mi.MemberType) { case MemberTypes.Constructor: case MemberTypes.Method: { MethodBase mb = (MethodBase)mi; attributes = new DllImportAttribute[] { GetDllImportInfo(mb) }; methods = new MethodBase[] { mb }; break; } case MemberTypes.Event: { EventInfo ei = (EventInfo)mi; MethodBase add = ei.GetAddMethod(true); MethodBase remove = ei.GetRemoveMethod(true); attributes = new DllImportAttribute[] { GetDllImportInfo(add), GetDllImportInfo(remove) }; methods = new MethodBase[] { add, remove }; break; } case MemberTypes.Property: { PropertyInfo pi = (PropertyInfo)mi; MethodInfo[] accessors = pi.GetAccessors(true); if (accessors == null) { break; } attributes = new DllImportAttribute[accessors.Length]; methods = new MethodBase [accessors.Length]; for (int i = 0; i < accessors.Length; ++i) { attributes [i] = GetDllImportInfo(accessors [i]); methods [i] = accessors [i]; } break; } } if (attributes == null || methods == null) { return; } for (int i = 0; i < attributes.Length; ++i) { if (attributes [i] == null) { continue; } CheckLibrary(methods [i], attributes [i], report); } }
private void Init(DllImportAttribute dllImport) { Type type = typeof(DllImportAttribute); this.m_ctor = type.GetConstructors(BindingFlags.Public | BindingFlags.Instance)[0]; CustomAttributeTypedArgument[] array = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(dllImport.Value) }; this.m_typedCtorArgs = Array.AsReadOnly <CustomAttributeTypedArgument>(array); CustomAttributeNamedArgument[] argumentArray2 = new CustomAttributeNamedArgument[] { new CustomAttributeNamedArgument(type.GetField("EntryPoint"), dllImport.EntryPoint), new CustomAttributeNamedArgument(type.GetField("CharSet"), dllImport.CharSet), new CustomAttributeNamedArgument(type.GetField("ExactSpelling"), dllImport.ExactSpelling), new CustomAttributeNamedArgument(type.GetField("SetLastError"), dllImport.SetLastError), new CustomAttributeNamedArgument(type.GetField("PreserveSig"), dllImport.PreserveSig), new CustomAttributeNamedArgument(type.GetField("CallingConvention"), dllImport.CallingConvention), new CustomAttributeNamedArgument(type.GetField("BestFitMapping"), dllImport.BestFitMapping), new CustomAttributeNamedArgument(type.GetField("ThrowOnUnmappableChar"), dllImport.ThrowOnUnmappableChar) }; this.m_namedArgs = Array.AsReadOnly <CustomAttributeNamedArgument>(argumentArray2); }
internal static bool IsDefined(RuntimeMethodInfo method, Type caType) { int num; bool flag = (caType == typeof(object)) || (caType == typeof(Attribute)); if (!flag && (s_pca[caType] == null)) { return(false); } return(((flag || (caType == typeof(DllImportAttribute))) && DllImportAttribute.IsDefined(method)) || (((flag || (caType == typeof(PreserveSigAttribute))) && PreserveSigAttribute.IsDefined(method)) || ((flag || IsSecurityAttribute(caType)) && (GetCustomAttributes(method, caType, true, out num).Length != 0)))); }
private static void GetDelegate <T>(string entryPoint, out T newDelegate) where T : class { DllImportAttribute attribute = new DllImportAttribute(GetDllName()); attribute.CallingConvention = CallingConvention.Winapi; attribute.PreserveSig = true; attribute.EntryPoint = entryPoint; attribute.CharSet = CharSet.Auto; PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate); }
private static object ApplyDelegate(string functionName, FieldInfo field) { var attribute = new DllImportAttribute(DllName) { CallingConvention = CallingConvention.Cdecl, PreserveSig = true, EntryPoint = functionName }; Native.CreatePInvokeDelegate(attribute, field.FieldType, out var delegateInstance); return(delegateInstance); }
private static void AssertEqual(DllImportAttribute d1, DllImportAttribute d2) { Assert.Equal(d1.BestFitMapping, d2.BestFitMapping); Assert.Equal(d1.CallingConvention, d2.CallingConvention); Assert.Equal(d1.CharSet, d2.CharSet); Assert.Equal(d1.EntryPoint, d2.EntryPoint); Assert.Equal(d1.ExactSpelling, d2.ExactSpelling); Assert.Equal(d1.PreserveSig, d2.PreserveSig); Assert.Equal(d1.SetLastError, d2.SetLastError); Assert.Equal(d1.ThrowOnUnmappableChar, d2.ThrowOnUnmappableChar); Assert.Equal(d1.Value, d2.Value); }
static int Main() { DllImportAttribute dia = Attribute.GetCustomAttribute(typeof(Program).GetMethod("test"), typeof(DllImportAttribute)) as DllImportAttribute; if (dia == null) { return(1); } if (dia.CharSet != CharSet.Unicode) { return(2); } if (!typeof(C).IsUnicodeClass) { return(3); } if (!typeof(C.CC).IsUnicodeClass) { return(4); } if (!typeof(D).IsUnicodeClass) { return(5); } var ufp = typeof(D).GetCustomAttributes(false)[0] as UnmanagedFunctionPointerAttribute; if (ufp.CharSet != CharSet.Unicode) { return(51); } if (!typeof(E).IsUnicodeClass) { return(6); } if (!typeof(foo1).IsUnicodeClass) { return(7); } if (!typeof(foo2).IsAutoClass) { return(8); } return(0); }
private static T CreateDelegate <T>(string entryPoint) where T : Delegate { var attribute = new DllImportAttribute(GetDllName()) { CallingConvention = CallingConvention.Cdecl, PreserveSig = true, EntryPoint = entryPoint }; PInvokeDelegateFactory.CreateDelegate(attribute, out T newDelegate); return(newDelegate); }
private static void GetDelegate <T>(string entryPoint, out T newDelegate) where T : class { DllImportAttribute attribute = new DllImportAttribute(dllName) { CallingConvention = CallingConvention.Cdecl, PreserveSig = true, EntryPoint = entryPoint }; PInvokeDelegateFactory.CreateDelegate(attribute, out newDelegate); }
private static void CollectAllReferencedDlls(Assembly assembly, HashSet <String> collectedDlls) { foreach (AssemblyName assemblyName in assembly.GetReferencedAssemblies()) { var loadedAssembly = Assembly.Load(assemblyName); if (!collectedDlls.Contains(loadedAssembly.Location)) { collectedDlls.Add(loadedAssembly.Location); CollectAllReferencedDlls(loadedAssembly, collectedDlls); foreach (Type type in loadedAssembly.GetTypes()) { foreach (MethodInfo method in type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) { DllImportAttribute attrib = (DllImportAttribute)Attribute.GetCustomAttribute(method, typeof(DllImportAttribute)); if (attrib != null && !collectedDlls.Contains(attrib.Value)) { //var beforeModules = new HashSet<String>(); //ProcessModuleCollection myProcessModuleCollection = Process.GetCurrentProcess().Modules; //for (int i = 0; i < myProcessModuleCollection.Count; ++i) //{ // beforeModules.Add(myProcessModuleCollection[i].FileName); //} IntPtr loadedModuleHandle = LoadLibrary(attrib.Value); if (loadedModuleHandle != null) { StringBuilder path = new StringBuilder(1024); GetModuleFileNameA(loadedModuleHandle, path, path.Capacity); if (path.ToString().EndsWith("dll", StringComparison.CurrentCultureIgnoreCase)) { collectedDlls.Add(path.ToString()); } //var afterModules = new HashSet<String>(); //myProcessModuleCollection = Process.GetCurrentProcess().Modules; //for (int i = 0; i < myProcessModuleCollection.Count; ++i) //{ // afterModules.Add(myProcessModuleCollection[i].FileName); //} FreeLibrary(loadedModuleHandle); //HashSet<string> complemenetSet = new HashSet<string>(afterModules.Except(beforeModules)); //collectedDlls.UnionWith(complemenetSet); } } } } } } }