13.1. Call a Function in an Unmanaged DLL

Problem

You need to call a function in a DLL. This function might be part of the Win32 API or your own legacy code.

Solution

Declare a method in your VB .NET code that you will use to access the unmanaged function. Declare this method as Shared, and apply the attribute System.Runtime.InteropServices.DllImportAttribute to specify the DLL file and the name of the unmanaged function.

How It Works

To use a function from an external library (such as one written in C or C++), all you need to do is declare it appropriately. The CLR automatically handles the rest, including loading the DLL into memory when the function is called and marshaling the parameters from .NET data types to C data types (or the data ...

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.