# Declare
{: .no_toc }

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

> [!NOTE]
>
> **Declare** statements with the PtrSafe keyword is the recommended syntax. **Declare** statements that include **PtrSafe** work correctly in twinBASIC and VBA version 7 development environment on both 32-bit and 64-bit platforms only after all data types in the **Declare** statement (parameters and return values) that need to store 64-bit quantities are updated to use LongLong for 64-bit integrals or LongPtr for pointers and handles.

To ensure backwards compatibility with VBA version 6 and earlier, use the following construct:

```tb inert=skeleton
#If VBA7 Then 
Declare PtrSafe Sub... 
#Else 
Declare Sub... 
#EndIf
```

> [!NOTE]
>
> For code to run when built for 64-bit targets, all **Declare** statements must include the **PtrSafe** keyword, and all data types in the **Declare** statement (parameters and return values) that need to store 64-bit quantities must be updated to use **LongLong** for 64-bit integrals or **LongPtr** for pointers and handles.

Syntax:

- > [ *attributes* ]  
  > [ **Public** \| **Private** ] **Declare** [ **PtrSafe** ] **Sub** *name* [ *callconv* ] **Lib** "*libname*" [ **(** [ *arglist* ] **)** ]
- > [ *attributes* ]  
  > [ **Public** \| **Private** ] **Declare** [ **PtrSafe** ] **Sub** *name* [ *callconv* ] **Lib** "*libname*" **Alias** "*aliasname*" [ **(** [ *arglist* ] **)** ]
- > [ *attributes* ]  
  > [ **Public** \| **Private** ] **Declare** [ **PtrSafe** ] **Function** *name* [ *callconv* ] **Lib** "*libname*" [ **(** [ *arglist* ] **)** ] [ **As** *type* ]
- > [ *attributes* ]  
  > [ **Public** \| **Private** ] **Declare** [ **PtrSafe** ] **Function** *name* [ *callconv* ] **Lib** "*libname*" **Alias** "*aliasname*" [ **(** [ *arglist* ] **)** ] [ **As** *type* ]


*attributes*
: *optional* One or more of:  
[Description](/tB/Core/Attributes.md#description), [DLLStackCheck](/tB/Core/Attributes.md#dllstackcheck), [PreserveSig](/tB/Core/Attributes.md#preservesig), [SetDllDirectory](/tB/Core/Attributes.md#setdlldirectory), [UseGetLastError](/tB/Core/Attributes.md#usegetlasterror)

**Public**
: *optional* Used to declare procedures that are available to all other procedures in all modules.

**Private**
: *optional* Used to declare procedures that are available only within the module where the declaration is made.

**PtrSafe**
: *required in 64-bits* Asserts that the declaration has been reviewed for pointer-width correctness and is safe to use in a 64-bit process. It has no effect on a 32-bit build, so it is good practice to use it everywhere.

**Sub / Function**
: Indicates whether the procedure returns a value (**Function**) or not (**Sub**).

*name*
: Any valid procedure name. Note that DLL entry points are case-sensitive.

*callconv*
: *optional* The calling convention of the DLL procedure: **CDecl**, **ThisCall** or **FastCall**. The default is stdcall, the convention of nearly all Windows API functions. A twinBASIC extension; see [Calling Conventions](/Features/Advanced/API-Declarations.md#calling-conventions).

*libname*
: Name of the DLL or code resource that contains the declared procedure. In twinBASIC it can also be, without quotes, the namespace of a statically linked library that an **Import Library** statement names; see [Static Linking](/Features/Advanced/Static-Linking.md).

**Alias** *aliasname*
: *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. Alias also applies 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 when any characters in the DLL procedure name aren't allowed by the DLL naming convention.  
*aliasname* names 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, Integer, Long, LongLong, LongPtr, Currency, Single, Double, Decimal, Date, String (variable length only), Variant, a user-defined type (UDT), or an object type.

### arglist

The *arglist* argument has the following syntax and parts:

Syntax: [ **Optional** ] [ **ByVal** \| **ByRef** ] [ **ParamArray** ] *varname* [ **( )** ] [ **As** *type* ]

**Optional**
: *optional* Indicates that an argument is not required. If used, all subsequent arguments in *arglist* must also be optional and declared by 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.

**ByRef**
: *optional* Indicates that the argument is passed by reference. **ByRef** is the default unlike in Visual Basic .NET.

**ParamArray**
: *optional* Used only as the last argument in arglist to indicate that the final argument is an **Optional** array of **Variant** elements. The **ParamArray** keyword permits passing an arbitrary number of arguments. The ParamArray keyword can't be used with **ByVal**, **ByRef**, or **Optional**.

*varname*
: 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**, **Integer**, **Long**, **LongLong**, **LongPtr**, **Currency**, **Single**, **Double**, **Decimal**, **Date**, **String** (variable length only), **Object**, **Variant**, a user-defined type (UDT), or an object type.

When an argument list is included, the number and type of arguments are checked each time the procedure is called. The First sub in the following example takes one **Long** argument, whereas the Second sub takes no arguments:

```tb check_build
Declare Sub First Lib "MyLib" (X As Long)
Declare Sub Second Lib "MyLib" ()
```

> [!NOTE]
>
> - Fixed-length strings cannot appear in the argument list of a **Declare** statement; only variable-length strings can be passed to procedures. Fixed-length strings can appear as procedure arguments, but they are converted to variable-length strings before being passed.
> - The **vbNullString** constant is used when calling external procedures, where the external procedure requires a string whose value is zero. This is not the same thing as a zero-length string ("").

> [!WARNING] BETA 1045
> In a 64-bit build with [LLVM](/LLVM/Getting-Started.md), a call that passes `ByVal 0&` for the eighth parameter, declared `ByRef` with a 32-bit type such as **Long**, to give the procedure a NULL pointer, can end with an access violation (0xC0000005) and no message box. The eighth parameter of **WideCharToMultiByte** is one such case. Whether the call fails depends on what the stack held before it. The same override for the first to seventh parameters is right. Declare the parameter `ByVal ... As LongPtr` instead. A 32-bit build and a build without LLVM are not affected.

> [!WARNING] BETA 1045
> In a 32-bit build, a call of a DLL procedure whose **Declare** names the wrong calling convention, or the wrong number of parameters, raises no error. The call returns, with a wrong value when a parameter is missing, and the procedure that made the call ends with an access violation as it returns; in an IDE run the compiler's process ends with it and restarts. VB6 raises error 49, *Bad DLL calling convention*, at the call in its IDE and in a P-Code exe. A call of a statically linked function raises an error instead; see [Calling Conventions](/Features/Advanced/API-Declarations.md#calling-conventions). [**[DLLStackCheck(False)]**](/tB/Core/Attributes.md#dllstackcheck) on the **Declare** turns the same check on for its calls.

### Example

This example shows how the **Declare** statement is used at the module level of a standard module to declare a reference to an external procedure in a dynamic-link library (DLL). **Declare** statements can be placed in class modules when they are **Private**.

```tb check_build
' In 32-bit Microsoft Windows systems, specify the library USER32.DLL.
Declare Sub MessageBeep Lib "User32" (ByVal N As Long)
 
' 64-bit Declare statement example:
Declare PtrSafe Function GetActiveWindow Lib "User32" () As LongPtr
 
' Conditional Compilation Example 
#If Vba7 Then
     ' Code is running in 32-bit or 64-bit twinBASIC or VBA7
     #If Win64 Then
          ' Code is running in 64-bit twinBASIC or VBA7.
     #Else
          ' Code is not running in 64-bit twinBASIC or VBA7.
     #End If
#Else
     ' Code is NOT running in 32-bit or 64-bit twinBASIC or VBA7.
#End If 
```
