Marshaling Data with Platform Invoke 之三 Marshaling Classes, Structures, and Unions(用时查阅)

Marshaling Classes, Structures, and Unions

Classes and structures are similar in the .NET Framework. Both can have fields, properties, and events. They can also have static and nonstatic methods. One notable difference is that structures are value types and classes are reference types.

The following table lists marshaling options for classes, structures, and unions; describes their usage; and provides a link to the corresponding platform invoke sample.

Type

Description

Sample

Class by value.

Passes a class with integer members as an In/Out parameter, like the managed case.

SysTime

Structure by value.

Passes structures as In parameters.

Structures

Structure by reference.

Passes structures as In/Out parameters.

OSInfo

Structure with nested structures (flattened).

Passes a class that represents a structure with nested structures in the unmanaged function. The structure is flattened to one big structure in the managed prototype.

FindFile

Structure with nested structures (not flattened).

Passes a structure with an embedded structure.

Structures

Structure with a pointer to another structure.

Passes a structure that contains a pointer to a second structure as a member.

Structures

Array of structures with integers by value.

Passes an array of structures that contain only integers as an In/Out parameter. Members of the array can be changed.

Arrays

Array of structures with integers and strings by reference.

Passes an array of structures that contain integers and strings as an Out parameter. The called function allocates memory for the array.

OutArrayOfStructs

Unions with value types.

Passes unions with value types (integer and double).

Unions

Unions with mixed types.

Passes unions with mixed types (integer and string).

Unions

Null values in structure.

Passes a null reference (Nothing in Visual Basic) instead of a reference to a value type.

HandleRef

posted on 2010-01-09 16:06  Acor  阅读(209)  评论(0编辑  收藏  举报

导航