Declare Statement - Visual Basic (original) (raw)

Declares a reference to a procedure implemented in an external file.

Syntax

[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ Overloads ] _
Declare [ charsetmodifier ] [ Sub ] name Lib "libname" _
[ Alias "aliasname" ] [ ([ parameterlist ]) ]
' -or-
[ <attributelist> ] [ accessmodifier ] [ Shadows ] [ Overloads ] _
Declare [ charsetmodifier ] [ Function ] name Lib "libname" _
[ Alias "aliasname" ] [ ([ parameterlist ]) ] [ As returntype ]

Parts

Term Definition
attributelist Optional. See Attribute List.
accessmodifier Optional. Can be one of the following: - Public- Protected- Friend- Private- Protected Friend- Private Protected See Access levels in Visual Basic.
Shadows Optional. See Shadows.
charsetmodifier Optional. Specifies character set and file search information. Can be one of the following: - Ansi (default)- Unicode- Auto
Sub Optional, but either Sub or Function must appear. Indicates that the external procedure does not return a value.
Function Optional, but either Sub or Function must appear. Indicates that the external procedure returns a value.
name Required. Name of this external reference. For more information, see Declared Element Names.
Lib Required. Introduces a Lib clause, which identifies the external file (DLL or code resource) that contains an external procedure.
libname Required. Name of the file that contains the declared procedure.
Alias Optional. Indicates that the procedure being declared cannot be identified within its file by the name specified in name. You specify its identification in aliasname.
aliasname Required if you use the Alias keyword. String that identifies the procedure in one of two ways: The entry point name of the procedure within its file, within quotes ("") -or- A number sign (#) followed by an integer specifying the ordinal number of the procedure's entry point within its file
parameterlist Required if the procedure takes parameters. See Parameter List.
returntype Required if Function is specified and Option Strict is On. Data type of the value returned by the procedure.

Sometimes you need to call a procedure defined in a file (such as a DLL or code resource) outside your project. When you do this, the Visual Basic compiler does not have access to the information it needs to call the procedure correctly, such as where the procedure is located, how it is identified, its calling sequence and return type, and the string character set it uses. The Declare statement creates a reference to an external procedure and supplies this necessary information.

You can use Declare only at module level. This means the declaration context for an external reference must be a class, structure, or module, and cannot be a source file, namespace, interface, procedure, or block. For more information, see Declaration Contexts and Default Access Levels.

External references default to Public access. You can adjust their access levels with the access modifiers.

Rules

Data Type Rules

Behavior

Important

If the external procedure runs outside the common language runtime (CLR), it is unmanaged code. When you call such a procedure, for example a Windows API function or a COM method, you might expose your application to security risks. For more information, see Secure Coding Guidelines for Unmanaged Code.

Example 1

The following example declares an external reference to a Function procedure that returns the current user name. It then calls the external procedure GetUserNameA as part of the getUser procedure.

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (
    ByVal lpBuffer As String, ByRef nSize As Integer) As Integer
Sub GetUser()
    Dim buffer As String = New String(CChar(" "), 25)
    Dim retVal As Integer = GetUserName(buffer, 25)
    Dim userName As String = Strings.Left(buffer, InStr(buffer, Chr(0)) - 1)
    MsgBox(userName)
End Sub

Example 2

The DllImportAttribute provides an alternative way of using functions in unmanaged code. The following example declares an imported function without using a Declare statement.

' Add an Imports statement at the top of the class, structure, or
' module that uses the DllImport attribute.
Imports System.Runtime.InteropServices
<DllImportAttribute("kernel32.dll", EntryPoint:="MoveFileW",
    SetLastError:=True, CharSet:=CharSet.Unicode,
    ExactSpelling:=True,
    CallingConvention:=CallingConvention.StdCall)>
Public Shared Function MoveFile(ByVal src As String,
  ByVal dst As String) As Boolean
    ' This function copies a file from the path src to the path dst.
    ' Leave this function empty. The DLLImport attribute forces calls
    ' to MoveFile to be forwarded to MoveFileW in KERNEL32.DLL.
End Function

See also