Visual Basic Language Reference  

Declare Statement

Used at module level to declare references to external procedures in a dynamic-link library (DLL).

[ <attrlist> ] [ Public | Private | Protected | Friend | Protected Friend ] [ Shadows ] _
Declare [ Ansi | Unicode | Auto ] [ Sub ] name Lib "libname" _
[ Alias "aliasname" ] [([ arglist ])]

-or-

[ <attrlist> ] [ Public | Private | Protected | Friend | Protected Friend ] [ Shadows ] _
Declare [ Ansi | Unicode | Auto ] [ Function ] name Lib "libname" _
[ Alias "aliasname" ] [(arglist ])] [ As type ]

Parts

attrlist
Optional. List of attributes that apply to this procedure. Multiple attributes are separated by commas.
Public
Optional. Entities declared with the Public modifier have public access. There are no restrictions on the use of public entities.
Private
Optional. Entities declared with the Private modifier have private access. A private entity is accessible only within its declaration context, including any nested entities.
Protected
Optional. Entities declared with the Protected keyword have protected access. They are accessible only from within their own class or from a derived class. Protected access can be specified only on members of classes. It is not a superset of friend access.
Friend
Optional. Entities declared with the Friend modifier have friend access. An entity with friend access is accessible only within the program that contains the entity declaration.
Protected Friend
Optional. Entities declared with the Protected Friend modifiers have the union of protected and friend accessibility.
Shadows
Optional. Indicates that this procedure shadows an identically named programming element in a base class. You can shadow any kind of declared element with any other kind. A shadowed element is unavailable in the derived class that shadows it
Ansi
Optional. Converts all strings to ANSI values. If no modifier is specified, Ansi is the default.
Unicode
Optional. Converts all strings to Unicode values.
Auto
Optional. Converts the strings according to common language runtime rules based on the name of the method (or the alias name, if specified).
Sub
Optional (either Sub or Function must appear). Indicates that the procedure does not return a value.
Function
Optional (either Sub or Function must appear). Indicates that the procedure returns a value that can be used in an expression.
name
Required. Any valid procedure name. Note that DLL entry points are case sensitive.
Lib
Required. Indicates that a DLL or code resource contains the procedure being declared. The Lib clause is required for all declarations.
libname
Required. Name of the DLL or code resource that contains the declared procedure.
Alias
Optional. Indicates that the procedure being called has another name in the DLL. This is useful when the external procedure name is the same as a keyword. You can also use Alias when a DLL procedure has the same name as a public variable, constant, or any other procedure in the same scope. Alias is also useful if any characters in the DLL procedure name are not allowed by the DLL naming convention.
aliasname
Optional. Name of the procedure in the DLL or code resource. If the first character is not a number sign (#), aliasname is the name of the procedure's entry point in the DLL. If (#) is the first character, all characters that follow must indicate the ordinal number of the procedure's entry point.
arglist
Optional. List of variables representing arguments that are passed to the procedure when it is called.
type
Optional. Data type of the value returned by a Function procedure; may be Byte, Boolean, Char, Short, Integer, Long, Single, Double, Decimal, String (variable length only), Object, or structure. Arrays of any type cannot be returned, but an object containing an array can.
Note   Functions declared with the Declare statement cannot return dates.

Each attribute in the attrlist part has the following syntax and parts:

attrname [({ attrargs | attrinit })]

attrlist Parts

attrname
Required. Name of the attribute. Must be a valid Visual Basic identifier.
attrargs
Optional. List of positional arguments for this attribute. Multiple arguments are separated by commas.
attrinit
Optional. List of field or property initializers for this attribute. Multiple initializers are separated by commas.

The arglist argument has the following syntax and parts:

[ <attrlist> ] [ Optional ] [ ByVal | ByRef ] [ ParamArray ] varname[( )] [ As type ]

arglist Parts

attrlist
Optional. List of attributes that apply to this argument. Multiple attributes are separated by commas.
Optional
Optional. Indicates that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. Optional can't be used for any argument if ParamArray is used.
ByVal
Optional. Indicates that the argument is passed by value. ByVal is the default in Visual Basic.
ByRef
Optional. Indicates that the argument is passed by reference.
ParamArray
Optional. Used only as the last argument in arglist to indicate that the final argument is an Optional array of Object elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. The ParamArray keyword can't be used with ByVal, ByRef, or Optional.
varname
Required. Name of the variable representing the argument being passed to the procedure; follows standard variable naming conventions.
( )
Required for array variables. Indicates that varname is an array.
type
Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, Char, Short, Integer, Long, Single, Double, Decimal, String (variable length only), Object, a user-defined type, or an object type.

Remarks

For Function procedures, the data type of the procedure determines the data type it returns. You can use an As clause following arglist to specify the return type of the function. Within arglist, you can use an As clause to specify the data type of any of the arguments passed to the procedure.

Note   If the external procedure was created on a platform other than Visual Basic .NET, you must take care that the data types correspond. For example, if you declare a reference to a Visual Basic 6.0 procedure with an Integer argument (two bytes in Visual Basic 6.0), you must identify that argument as Short in the Declare statement, because that is the two-byte integer type in Visual Basic .NET.

Example

The following example declares a function that returns the current user name.

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

The DllImport attribute 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 or module
' where the DllImport attribute will be used.
Imports System.Runtime.InteropServices
'...
<DllImport("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 function empty - DLLImport attribute forces calls 
   ' to MoveFile to be forwarded to MoveFileW in KERNEL32.DLL.
End Function

See Also

DllImportAttribute Class | AddressOf Operator | Call Statement | Function Statement | LastDLLError Property | Sub Statement | Integer Data Type Changes in Visual Basic | Walkthrough: Calling Windows APIs