Ask the Expert

How do we call an API in VB.NET?

In VB6, we can use the API by copying it from an API viewer, but how do we call API in VB.NET?

    Requires Free Membership to View

Windows API calls were an important part of Visual Basic programming in the past, but should seldom be necessary with Visual Basic .NET. Whenever possible, you should use managed code from the .NET Framework to perform tasks instead of Windows API calls. Before going through the trouble of and exposing yourself to the risk of calling Win32 APIs directly, make sure that the functionality you require is not already available in .NET. Some system event, for examples, can be accessed through the Microsoft.Win32 Namespace. Other functions are now an integral part of the .NET object model. That said, if you really do need to call the Win32 API, there are a few things to keep in mind:

Windows dynamic-link libraries (DLLs) represent a special category of interoperability. Windows APIs do not use managed code, do not have built-in type libraries, and use data types that are different than those used with Visual Studio .NET. Because of these differences, and because Windows APIs are not COM objects, interoperability with Windows APIs and the .NET Platform is performed using platform invoke, or PInvoke. Platform invoke is a service that enables managed code to call unmanaged functions implemented in DLLs. You can use PInvoke in Visual Basic .NET by using either the Declare statement or applying the DllImport attribute to an empty procedure.

To find a walkthrough of calling the Win32 API, see the topic "Walkthrough: Calling Windows APIs" under COM Interop in the MSDN help.

This was first published in August 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: