In VB6, we can use the API by copying it from an API viewer, but how do we call API in VB.NET? 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.
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.