Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

InAttribute Class

Indicates that data should be marshaled from the caller to the callee, but not back to the caller.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

[AttributeUsageAttribute(AttributeTargets.Parameter, Inherited=false)] 
[ComVisibleAttribute(true)] 
public sealed class InAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Parameter, Inherited=false) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class InAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Parameter, Inherited=false) 
ComVisibleAttribute(true) 
public final class InAttribute extends Attribute

You can apply this attribute to parameters.

The InAttribute is optional. The attribute is supported for COM interop and platform invoke only. In the absence of explicit settings, the interop marshaler assumes rules based on the parameter type, whether the parameter is passed by reference or by value, and whether the type is blittable or non-blittable. For example, the StringBuilder class is always assumed to be In/Out and an array of strings passed by value is assumed to be In.

You cannot apply the InAttribute to a parameter modified with the C#-styled out keyword. To avoid confusing the In keyword in Visual Basic with the InAttribute, minus Attribute, use the <[In]> form with brackets around the attribute.

Combining the InAttribute and OutAttribute is particularly useful when applied to arrays and formatted, non-blittable types. Callers see the changes a callee makes to these types only when you apply both attributes. Since these types require copying during marshaling, you can use InAttribute and OutAttribute to reduce unnecessary copies.

For more information on the effect of InAttribute on marshaling behavior, see Directional Attributes.

The following example shows how to apply the InAttribute and OutAttribute to a platform invoke prototype that passes an array as a parameter. The combination of directional attributes allows the caller to see the changes made by the callee.

// Declare a class member for each structure element.
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Auto )]  
public class OpenFileName 
{
    public int       structSize = 0;
    public string    filter = null;
    public string    file = null;
    // ...
}

public class LibWrap
{
   // Declare a managed prototype for the unmanaged function.
   [ DllImport( "Comdlg32.dll", CharSet=CharSet.Auto )]
   public static extern bool GetOpenFileName([ In, Out ] OpenFileName ofn );  
}

// Declare a class member for each structure element.
/** @attribute StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)
 */
public class OpenFileName
{
    public int structSize = 0;
    public String filter = null;
    public String file = null;
    // ...
} //OpenFileName

public class LibWrap
{
    // Declare a managed prototype for the unmanaged function.
    /** @attribute DllImport("Comdlg32.dll", CharSet = CharSet.Auto)
     */
    public static native boolean GetOpenFileName(
        /** @attribute In()
            @attribute Out()
         */
        OpenFileName ofn);
} //LibWrap

System.Object
   System.Attribute
    System.Runtime.InteropServices.InAttribute
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.