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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.