private static List <IMethodDefinition> CreateWindowsHelpers( IMetadataHost host, ITypeDefinition typeDef, string prefix, string loadLibraryMethodName, string freeLibraryMethodName, string getProcAddressMethodName, IModuleReference moduleRef, PInvokeCallingConvention callingConvention) { var loadLibrary = CreateLoadLibraryMethod(host, typeDef, moduleRef, prefix, loadLibraryMethodName, callingConvention); var freeLibrary = CreateFreeLibraryMethod(host, typeDef, moduleRef, prefix, freeLibraryMethodName, callingConvention); var getProcAddress = CreateGetProcAddressMethod(host, typeDef, moduleRef, prefix, getProcAddressMethodName, callingConvention); return(new List <IMethodDefinition> { loadLibrary, freeLibrary, getProcAddress }); }
private static List <IMethodDefinition> CreateUnixSpecificHelpers( IMetadataHost host, ITypeDefinition typeDef, string prefix, string loadLibraryMethodName, string freeLibraryMethodName, string getProcAddressMethodName, IModuleReference moduleRef, PInvokeCallingConvention callingConvention) { var loadLibrary = CreateLoadLibraryMethod(host, typeDef, moduleRef, prefix, loadLibraryMethodName, callingConvention); loadLibrary.Parameters.Add(new ParameterDefinition { Index = 1, Type = host.PlatformType.SystemInt32 }); var freeLibrary = CreateFreeLibraryMethod(host, typeDef, moduleRef, prefix, freeLibraryMethodName, callingConvention); var getProcAddress = CreateGetProcAddressMethod(host, typeDef, moduleRef, prefix, getProcAddressMethodName, callingConvention); return(new List <IMethodDefinition> { loadLibrary, freeLibrary, getProcAddress }); }
private static MethodDefinition CreatePInvokeMethod(IMetadataHost host, ITypeDefinition typeDef, List <IParameterDefinition> parameters, ITypeReference returnType, IModuleReference moduleRef, string prefix, string methodName, PInvokeCallingConvention callingConvention) { return(new MethodDefinition { ContainingTypeDefinition = typeDef, Type = returnType, Parameters = parameters, Name = host.NameTable.GetNameFor(prefix + methodName), Visibility = TypeMemberVisibility.Public, IsStatic = true, IsPlatformInvoke = true, PreserveSignature = true, IsHiddenBySignature = true, IsExternal = true, PlatformInvokeData = new PlatformInvokeInformation { PInvokeCallingConvention = callingConvention, ImportModule = moduleRef, ImportName = host.NameTable.GetNameFor(methodName) } }); }
private static MethodDefinition CreateGetProcAddressMethod(IMetadataHost host, ITypeDefinition typeDef, IModuleReference moduleRef, string prefix, string getProcAddressMethodName, PInvokeCallingConvention callingConvention) { return(CreatePInvokeMethod( host, typeDef, new List <IParameterDefinition> { new ParameterDefinition { Index = 0, Type = host.PlatformType.SystemIntPtr }, new ParameterDefinition { Index = 1, Type = host.PlatformType.SystemString } }, host.PlatformType.SystemIntPtr, moduleRef, prefix, getProcAddressMethodName, callingConvention)); }
private static MethodDefinition CreateFreeLibraryMethod(IMetadataHost host, ITypeDefinition typeDef, IModuleReference moduleRef, string prefix, string freeLibraryMethodName, PInvokeCallingConvention callingConvention) { return(CreatePInvokeMethod( host, typeDef, new List <IParameterDefinition> { new ParameterDefinition { Index = 0, Type = host.PlatformType.SystemIntPtr } }, host.PlatformType.SystemInt32, moduleRef, prefix, freeLibraryMethodName, callingConvention)); }
internal PlatformInvokeInformation(ITypeDeclarationMember declaringMember, SourceCustomAttribute dllImportAttribute) { this.importModule = Dummy.ModuleReference; this.importName = declaringMember.Name.Name; this.noMangle = false; this.pinvokeCallingConvention = PInvokeCallingConvention.WinApi; this.stringFormat = StringFormatKind.Unspecified; this.useBestFit = null; this.throwExceptionForUnmappableChar = null; INameTable nameTable = dllImportAttribute.ContainingBlock.NameTable; int bestFitMappingKey = nameTable.GetNameFor("BestFitMapping").UniqueKey; int callingConventionKey = nameTable.GetNameFor("CallingConvention").UniqueKey; int charSetKey = nameTable.GetNameFor("CharSet").UniqueKey; int entryPointKey = nameTable.GetNameFor("EntryPoint").UniqueKey; int exactSpellingKey = nameTable.GetNameFor("ExactSpelling").UniqueKey; int setLastErrorKey = nameTable.GetNameFor("SetLastError").UniqueKey; int throwOnUnmappableCharKey = nameTable.GetNameFor("ThrowOnUnmappableChar").UniqueKey; foreach (Expression expr in dllImportAttribute.Arguments) { CompileTimeConstant cc = expr as CompileTimeConstant; if (cc != null && cc.Value is string) { IName moduleName = expr.NameTable.GetNameFor((string)cc.Value); this.importModule = new Immutable.ModuleReference(dllImportAttribute.ContainingBlock.Compilation.HostEnvironment, new ModuleIdentity(moduleName, string.Empty)); continue; } NamedArgument narg = expr as NamedArgument; if (narg == null) continue; int key = narg.ArgumentName.Name.UniqueKey; if (key == bestFitMappingKey) { if (narg.ArgumentValue.Value is bool) this.useBestFit = (bool)narg.ArgumentValue.Value; continue; } if (key == callingConventionKey) { if (narg.ArgumentValue.Value is int) { switch ((CallingConvention)(int)narg.ArgumentValue.Value) { case CallingConvention.C: this.pinvokeCallingConvention = PInvokeCallingConvention.CDecl; break; case CallingConvention.Standard: this.pinvokeCallingConvention = PInvokeCallingConvention.StdCall; break; case CallingConvention.ExplicitThis: this.pinvokeCallingConvention = PInvokeCallingConvention.ThisCall; break; case CallingConvention.FastCall: this.pinvokeCallingConvention = PInvokeCallingConvention.FastCall; break; } } continue; } if (key == charSetKey) { if (narg.ArgumentValue.Value is int) { switch ((CharSet)(int)narg.ArgumentValue.Value) { case CharSet.Ansi: this.stringFormat = StringFormatKind.Ansi; break; case CharSet.Auto: this.stringFormat = StringFormatKind.AutoChar; break; case CharSet.Unicode: this.stringFormat = StringFormatKind.Unicode; break; } } continue; } if (key == entryPointKey) { string/*?*/ importName = narg.ArgumentValue.Value as string; if (importName != null) { this.importName = nameTable.GetNameFor(importName); } continue; } if (key == exactSpellingKey) { if (narg.ArgumentValue.Value is bool) this.noMangle = (bool)narg.ArgumentValue.Value; continue; } if (key == setLastErrorKey) { if (narg.ArgumentValue.Value is bool) this.supportsLastError = (bool)narg.ArgumentValue.Value; continue; } if (key == throwOnUnmappableCharKey) { if (narg.ArgumentValue.Value is bool) this.throwExceptionForUnmappableChar = (bool)narg.ArgumentValue.Value; continue; } } }