13.3. Call an Unmanaged Function That Uses a Structure

Problem

You need to call an unmanaged function, such as GetVersionEx, that accepts a structure as a parameter.

Solution

Define the structure in your VB .NET code. Use the attribute System.Runtime.InteropServices.StructLayoutAttribute to configure how the structure fields are laid out in memory. Use the Shared SizeOf method of the System.Runtime.InteropServices.Marshal class if you need to determine the size of the unmanaged structure in bytes.

How It Works

In VB .NET code, you are not able to directly control how type fields are laid out once the memory is allocated. Instead, the CLR is free to arrange fields to optimize performance, especially in the context of moving memory around during ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.