/////////////////////////////////////////////////////////////////////// private static ReturnCode ToClass2( Interpreter interpreter, Type type, /* NOT USED */ string text, OptionDictionary options, /* NOT USED */ CultureInfo cultureInfo, /* NOT USED */ IClientData clientData, /* NOT USED */ ref MarshalFlags marshalFlags, /* NOT USED */ ref object value, /* Sample.Class2 */ ref Result error ) { long token = 0; ICommand command = null; if (interpreter.GetCommand( text, LookupFlags.NoWrapper, ref token, ref command, ref error) == ReturnCode.Ok) { value = command; return(ReturnCode.Ok); } return(ReturnCode.Error); }
/////////////////////////////////////////////////////////////////////// #region Public Constructors public MarshalClientData( object data, OptionDictionary options, MarshalFlags marshalFlags, ReturnCode returnCode, Result result ) : this(data) { this.options = options; this.marshalFlags = marshalFlags; this.returnCode = returnCode; this.result = result; }
/////////////////////////////////////////////////////////////////////// public static bool HasFlags( MarshalFlags flags, MarshalFlags hasFlags, bool all ) { if (all) { return((flags & hasFlags) == hasFlags); } else { return((flags & hasFlags) != MarshalFlags.None); } }
/////////////////////////////////////////////////////////////////////// public bool DoesMatchType( object value, Type type, MarshalFlags marshalFlags ) { CheckDisposed(); if (parentBinder == null) { throw new InvalidOperationException(); } return(parentBinder.DoesMatchType(value, type, marshalFlags)); }
private static void LayoutUnmanagedSizeTest() { Console.WriteLine("running ValueTypeUnmanagedSizeTest..."); foreach (string assemblyString in testAssemblies) { Assembly assembly = Assembly.Load(assemblyString); foreach (Type type in assembly.GetTypes()) { if (!type.IsAutoClass && type != typeof(void) && type != typeof(ArrayWithOffset) && type != typeof(HandleRef)) { int size_reported_by_clr; try { // will fail when CLR thinks the type has no layout size_reported_by_clr = Marshal.SizeOf(type); } catch (Exception) { continue; } MarshalFlags flags = MarshalFlags.AnsiStrings | MarshalFlags.StructField; if (runningOn64Bit) { flags |= MarshalFlags.Platform64Bit; } NativeType native_type = NativeType.FromClrType(type, flags); if (native_type.TypeSize != size_reported_by_clr) { Console.WriteLine("* size mismatch for {0} (CLR {1}, tool {2})", type.FullName, size_reported_by_clr, native_type.TypeSize); } } } } }
public ChangeTypeData( string caller, Type type, object oldValue, OptionDictionary options, CultureInfo cultureInfo, IClientData clientData, MarshalFlags marshalFlags ) { this.caller = caller; this.type = type; this.oldValue = oldValue; this.options = options; this.cultureInfo = cultureInfo; this.clientData = clientData; this.marshalFlags = marshalFlags; /////////////////////////////////////////////////////////////////// Initialize(); }
/////////////////////////////////////////////////////////////////////// public ReturnCode InvokeChangeTypeCallback( ChangeTypeCallback callback, Type type, string text, OptionDictionary options, CultureInfo cultureInfo, IClientData clientData, ref MarshalFlags marshalFlags, ref object value, ref Result error ) { CheckDisposed(); if (parentBinder == null) { throw new InvalidOperationException(); } return(parentBinder.InvokeChangeTypeCallback( callback, type, text, options, cultureInfo, clientData, ref marshalFlags, ref value, ref error)); }
/////////////////////////////////////////////////////////////////////// private static ReturnCode FromClass2( Interpreter interpreter, /* NOT USED */ Type type, /* NOT USED */ object value, OptionDictionary options, /* NOT USED */ CultureInfo cultureInfo, /* NOT USED */ IClientData clientData, /* NOT USED */ ref MarshalFlags marshalFlags, /* NOT USED */ ref string text, ref Result error ) { if (value is Class2) { text = ((Class2)value).Description; return(ReturnCode.Ok); } else { error = "type mismatch, need Class2"; } return(ReturnCode.Error); }
protected override void Initialize(TypeDefKey key) { Type type = key.Type; Debug.Assert(type.IsValueType || Utility.HasLayout(type)); this.name = Utility.GetNameOfType(type); this.isBlittable = Utility.IsStructBlittable(type, (key.Flags & MarshalFlags.AnsiPlatform) == MarshalFlags.AnsiPlatform); // reflect the structure FieldInfo[] fis = type.GetFields(bindingFlags); this.fields = new NativeField[fis.Length]; KeyValuePair <int, NativeField>[] fields_with_tokens = new KeyValuePair <int, NativeField> [fis.Length]; MarshalFlags flags = key.Flags & ~(MarshalFlags.AnsiStrings | MarshalFlags.UnicodeStrings); flags |= Utility.GetCharSetMarshalFlag(type); for (int i = 0; i < fis.Length; i++) { NativeField nf = NativeField.FromClrField(fis[i], flags); this.fields[i] = nf; // check for misaligned reference type fields // (can only be misaligned if layout was specified explicitly) if (nf.Offset.HasValue && nf.ContainsManagedReference) { int ptr_size = TypeName.GetPointerSize((key.Flags & MarshalFlags.Platform64Bit) == MarshalFlags.Platform64Bit); if (nf.Offset.Value % ptr_size != 0) { Log.Add(Errors.ERROR_MisalignedReferenceTypeField, nf.Name); } } if (nf.Type.TypeSize == 0) { // this means that the field type is another structure whose size has not been set up // yet -> there are circular dependencies among structures this.isInvalid = true; Log.Add(Errors.ERROR_RecursiveStructureDeclaration, nf.Name); } if (type.IsExplicitLayout && !nf.Offset.HasValue) { Log.Add(Errors.ERROR_NoFieldOffsetInSequentialLayout, nf.Name); } fields_with_tokens[i] = new KeyValuePair <int, NativeField>(fis[i].MetadataToken, nf); } // sort fields to reflect the layout if (type.IsExplicitLayout) { this.isExplicitLayout = true; // explicit layout - sort according to offsets Array.Sort <KeyValuePair <int, NativeField>, NativeField>( fields_with_tokens, this.fields, ExplicitFieldComparer.Instance); // managed references overlapping with other fields are not checked here - such errors are // reported by the loader and assemblies containing these types are never loaded } else { // sequential layout - sort according to metadata tokens Array.Sort <KeyValuePair <int, NativeField>, NativeField>( fields_with_tokens, this.fields, SequentialFieldComparer.Instance); } SetupSizeAndAlignment(type); SetupIsUnionFlag(); }
public TypeDefKeyWithUnmanagedType(Type type, MarshalFlags flags, UnmanagedType unmanagedType) : base(type, flags) { this.UnmanagedType = unmanagedType; }
private static void LayoutUnmanagedPrintoutTest() { string file_name = "_test.cpp"; Console.WriteLine("running ValueTypeUnmanagedPrintoutTest..."); // we'll generate a C++ source file with all the unmanaged structures // - it should compile // - the C++ sizeof operator should return the intended size // - the C++ offsetof macro should return the same offsets as Marshal.OffsetOf Console.WriteLine(" generating definitions..."); using (TextWriterCodePrinter printer = new TextWriterCodePrinter(new StreamWriter(file_name))) { // prepare a memory printer to print main to CodeMemoryPrinter main_printer = new CodeMemoryPrinter(); main_printer.PrintLn(OutputType.Other, "#define VERIFY(cond) if (!(cond)) printf(\"* \\\"%s\\\" violated\\n\", #cond)"); main_printer.PrintLn(OutputType.Other, "int main()"); main_printer.Print(OutputType.Other, "{"); main_printer.Indent(); main_printer.PrintLn(); LogMemoryPrinter log_printer = new LogMemoryPrinter(); printer.PrintLn(OutputType.Other, "#include <stddef.h>"); printer.PrintLn(OutputType.Other, "#include <stdio.h>"); printer.PrintLn(OutputType.Other, "#include <windows.h>"); printer.PrintLn(OutputType.Other, "#include <oaidl.h>"); printer.PrintLn(); int counter = 0; foreach (string assemblyString in testAssemblies) { Assembly assembly = Assembly.Load(assemblyString); foreach (Type type in assembly.GetTypes()) { if (!type.IsAutoClass) { int size_reported_by_clr; try { // will fail when CLR thinks the type has no layout size_reported_by_clr = Marshal.SizeOf(type); } catch (Exception) { continue; } MarshalFlags flags = MarshalFlags.AnsiStrings | MarshalFlags.StructField; if (runningOn64Bit) { flags |= MarshalFlags.Platform64Bit; } NativeType native_type = NativeType.FromClrType(type, flags); DefinedNativeType def_native_type = native_type as DefinedNativeType; if (def_native_type != null) { // dump the "closure" of this definition to a namespace string ns_name = String.Format("_CLR_{0}", counter++); printer.PrintLn(OutputType.Other, "namespace " + ns_name); printer.Print(OutputType.Other, "{"); printer.Indent(); printer.PrintLn(); NativeTypeDefinitionSet set = new NativeTypeDefinitionSet(); set.Add(def_native_type.Definition); def_native_type.Definition.GetDefinitionsRecursive(set, def_native_type.Definition); // enumerate the closure (will respect dependencies and return definitions // in a correct order introducing forward declarations if necessary) foreach (NativeTypeDefinition def in set) { def.PrintTo(printer, log_printer, //PrintFlags.UsePlainC | PrintFlags.UseDefinedComInterfaces | PrintFlags.MangleEnumFields); printer.PrintLn(); printer.PrintLn(); } printer.Unindent(); printer.PrintLn(); printer.PrintLn(OutputType.Other, "};"); // add tests to main() main_printer.PrintLn(OutputType.Other, String.Format( "VERIFY(sizeof({0}::{1}) == {2});", ns_name, def_native_type.Definition.Name, size_reported_by_clr)); StructureDefinition struct_def = def_native_type.Definition as StructureDefinition; if (struct_def != null) { for (int i = 0; i < struct_def.FieldCount; i++) { NativeField nf = struct_def.GetField(i); int offset; try { offset = (int)Marshal.OffsetOf(type, nf.Name); } catch (Exception) { continue; } // if we think we know the offset of this field, we will verify it main_printer.PrintLn(OutputType.Other, String.Format( "VERIFY(offsetof({0}::{1}, {2}) == {3});", ns_name, def_native_type.Definition.Name, nf.Name, offset)); } } } } } } main_printer.Print(OutputType.Other, "return 0;"); main_printer.Unindent(); main_printer.PrintLn(); main_printer.PrintLn(OutputType.Other, "}"); main_printer.ReplayTo(printer); } Console.WriteLine(" compiling the generated file..."); ProcessStartInfo start_info = new ProcessStartInfo( @"c:\Program Files (x86)\Microsoft Visual Studio 8\VC\bin\cl.exe", "/DUNICODE " + file_name); start_info.WorkingDirectory = Directory.GetCurrentDirectory(); start_info.UseShellExecute = false; start_info.CreateNoWindow = true; start_info.RedirectStandardOutput = true; Process cl_proc = Process.Start(start_info); cl_proc.WaitForExit(); Console.WriteLine(cl_proc.StandardOutput.ReadToEnd()); Console.WriteLine(" running the generated executable..."); start_info.FileName = Path.ChangeExtension(file_name, ".exe"); start_info.Arguments = String.Empty; Process tst_proc = Process.Start(start_info); tst_proc.WaitForExit(); Console.WriteLine(tst_proc.StandardOutput.ReadToEnd()); }
public TypeDefKeyWithUnmanagedType(Type type, MarshalFlags flags, UnmanagedType unmanagedType) : base(type, flags) { this.UnmanagedType = unmanagedType; }