ProgrammingDesktop .NET Developer

How to organize work with external libraries (DLLs) and COM objects in Visual Basic. What is the difference between Add Reference and Declare, and what nuances exist for inter-language interaction?

Pass interviews with Hintsage AI assistant

Answer.

Working with external libraries expands the capabilities of Visual Basic, allowing the use of not only standard classes but also third-party components — both .NET libraries (DLLs) and classic COM objects.

Background

Originally, VB was primarily focused on interacting with COM components (ActiveX, OCX), allowing such libraries to be connected through "Project – References". With the emergence of VB.NET, the ability to reference .NET assemblies was added, making it more convenient to use external methods via P/Invoke (Declare).

Problem

The main difficulty is the need to properly choose the integration method to avoid runtime errors, compatibility issues, and resource leaks when interacting with external code.

Solution

In .NET, to use .NET libraries or registered COM components, Add Reference is used, which adds a reference to the assembly; to call procedures from native DLLs, Declare is applied.

Example using a .NET library (Add Reference):

' After adding the .NET reference Imports System.IO Dim reader As New StreamReader("file.txt") Dim line As String = reader.ReadLine() reader.Close()

Example calling a function from WinAPI (Declare):

' Calling a function from user32.dll (WinAPI) Declare Function MessageBeep Lib "user32" (ByVal wType As Integer) As Boolean Sub TestBeep() MessageBeep(0) End Sub

Key features:

  • Add Reference for .NET/COM, Declare for stdcall functions from DLLs.
  • The necessity of proper resource management when working with external code.
  • Differences in syntax and the required platform and compatibility setup.

Tricky Questions.

Can the Imports statement be used to load functions from external DLLs?

No, the Imports statement only simplifies accessing namespaces within linked assemblies but cannot be used for dynamically linking functions from native DLLs.

Is it necessary to register .NET assemblies for use through Add Reference in VB.NET?

No, .NET libraries are connected by simply adding a reference, while registration through regsvr32 is only required for COM libraries.

Can .NET methods be connected through Declare, not just native Win32 functions?

No, Declare is only applied to functions from regular (non-.NET) DLLs that export stdcall/FARPROC functions. .NET functions are called through Add Reference/Imports.

Common Mistakes and Anti-Patterns

  • Unconscious mixing of the Add Reference and Declare approaches, attempting to call .NET functions via Declare.
  • Ignoring the necessity of correctly releasing COM objects (Marshal.ReleaseComObject).
  • Errors with pointers and type incompatibility when calling external code.

Real-life Examples

Negative Case

A developer decided to use a third-party .NET library via Declare without studying the documentation and encountered a compilation error.

Pros:

  • Quick implementation if the component is a standard DLL.

Cons:

  • Failure at the compilation stage.
  • Does not work for .NET assemblies.

Positive Case

Correctly adding a .NET reference through Add Reference, then using Imports and creating instances of the required classes.

Pros:

  • Reliable integration with .NET code.
  • Automatic memory management.

Cons:

  • Registration is required for some COM components.