[ Team LiB ] |
17.8 Predefined Interop Support AttributesThe FCL provides a set of attributes you can use to mark up your objects with information that is used by the CLR marshaling services to alter their default marshaling behavior. This section describes the most common attributes you need when interoperating with native Win32 DLLs. These attributes all exist in the System.Runtime.InteropServices namespace. 17.8.1 The DllImport Attribute[DllImport (dll-name [, EntryPoint=function-name]? [, CharSet=charset-enum]? [, SetLastError=true|false]? [, ExactSpelling=true|false]? [, PreserveSig=true|false]? [, CallingConvention=callconv-enum?)] (for methods) The DllImport attribute annotates an external function that defines a DLL entry point. The parameters for this attribute are as follows:
17.8.2 The StructLayout Attribute[StructLayout(layout-enum [, Pack=packing-size]? [, CharSet=charset-enum]? [, Size=absolute-size?)] (for classes, structs) The StructLayout attribute specifies how the data members of a class or struct should be laid out in memory. Although this attribute is commonly used when declaring structures that are passed to or returned from native DLLs, it can also define data structures suited to file and network I/O. The parameters for this attribute are as follows:
17.8.3 The FieldOffset Attribute[FieldOffset (byte-offset)] (for fields) The FieldOffset attribute is used within a class or struct that has explicit field layout. This attribute can be applied to a field and specifies the field offset in bytes from the start of the class or struct. Note that these offsets don't have to be strictly increasing and can overlap, thus creating a union data structure. 17.8.4 The MarshalAs Attribute[MarshalAs(unmanaged-type) [, named-parameters]?] (for fields, parameters, return values) The MarshalAs attribute overrides the default marshaling behavior the marshaler applies to a parameter or field. The unmanaged-type value is taken from the UnmanagedType enum; see the following list for the permissible values:
For a detailed description of how and when to use each of these enum values, as well as other legal named parameters, see the .NET Framework SDK documentation. 17.8.5 The In Attribute[In] (for parameters) The In attribute specifies that data should be marshaled into the caller and can be combined with the Out attribute. 17.8.6 The Out Attribute[Out] (for parameters) The Out attribute specifies that data should be marshaled out from the called method to the caller and can be combined with the In attribute. |
[ Team LiB ] |