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. |
|
Structure by value. |
Passes structures as In parameters. |
|
Structure by reference. |
Passes structures as In/Out parameters. |
|
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. |
|
Structure with nested structures (not flattened). |
Passes a structure with an embedded structure. |
|
Structure with a pointer to another structure. |
Passes a structure that contains a pointer to a second structure as a member. |
|
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. |
|
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. |
|
Unions with value types. |
Passes unions with value types (integer and double). |
|
Unions with mixed types. |
Passes unions with mixed types (integer and string). |
|
Null values in structure. |
Passes a null reference (Nothing in Visual Basic) instead of a reference to a value type. |