See Also Example
Declares references to external procedures in a dynamic-link library (DLL).
Declare Sub globalname Lib "libname" [Alias "aliasname" ][([argumentlist])]
Declare Function globalname Lib libname [Alias aliasname ] [([argumentlist])] [As type]
The Declare statement has these parts:
Sub Indicates that the procedure doesn't return a value.
Function Indicates that the procedure returns a value and can be used in an expression.
globalname The name of the Sub or Function procedure called. Procedure names follow the same rules used for naming other Visual Basic variables. Function procedure names can include a type-declaration character indicating the data type returned by the procedure. This name cannot appear as the name of any other procedure.
For Function procedures, the data type of the procedure determines the data type it returns. If the function name doesn't use a type-declaration character, you can use an As clause following the argumentlist to specify the return type of the function.
Lib Indicates that a DLL contains the procedure being declared. The Lib clause is required for all declarations.
libname String literal that is the name of the DLL that contains the declared procedure.
Alias 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 Visual Basic reserved word43US84. You can also use Alias when a DLL procedure has the same name as a Global variable or constant or any other procedure in the same scope. Alias is also useful if any characters in the DLL procedure name aren't allowed in Visual Basic names.
aliasname String literal that identifies the name of the procedure in the DLL.
argumentlist List of variables representing arguments that are passed to the Sub or Function procedure when it is called.
As type Declares the data type of the value returned by a Function procedure. The argument type may be Integer, Long, Single, Double, Currency, String (variable-length only), or Variant .
The argument argumentlist has the following syntax:
[ByVal] variable [ As type] [,[ByVal] variable [ As type] ] . . .
The following list describes the parts of argumentlist.
ByVal Indicates that the argument is passed by value rather than by reference. The ByVal reserved word cannot be used with a variable of a user-defined type, an object type, or a variable that is an array. When ByVal precedes a numeric argument variable, the actual argument is converted to the numeric type indicated in the Declare statement before being passed. When ByVal precedes a String argument variable, the address of the null-terminated string data is sent to the procedure. When ByVal is not included, a string descriptor is sent to the called DLL procedure.
variable A Visual Basic variable name. If you use a type-declaration character with variable, don't use the As clause. If there is no As clause, the default data type of variable is used.
As type Declares the data type of variable: Integer, Long, Single, Double, Currency, String, Variant, Any, a user-defined type, or any object type.
Use the Any data type in an As clause only to override type checking for that argument.
Use the Declare statement to declare external procedures (procedures contained in a DLL). A Declare statement for an external procedure can appear only in the Declarations section of a form or module . DLL procedures declared in any module are available to all procedures in all forms and modules, but DLL procedures declared in a form are only available to the procedures in the form.
Empty parentheses indicate that the Sub or Function procedure has no arguments and that arguments should be checked to ensure that none are passed. In the following example, First takes no arguments. If you use arguments in a call to First, an error occurs:
Declare Sub First Lib "MyLib" ()
When an argument list appears, the number and type of arguments are checked each time the procedure is called. In the following example, First takes one Long argument:
Declare Sub First Lib "MyLib" (X&)
Note You can't have fixed-length strings in the parameter list of a Declare statement because 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.
Declare Statement Example
The example uses the Declare statement to declare a reference to an external function (GetProfileString) in the Windows Kernel DLL. To try this example, paste the code into the Declarations section of a form. Make sure that the entire Declare statement appears as a single line in the Declarations section. Then press F5 and click the form.
Declare Function GetProfileString Lib "Kernel" (ByVal SName$, ByVal KName$,ByVal Def$, ByVal Ret$, ByVal Size%) As Integer
Sub Form_Click ()
Dim Msg, Success ' Declare variables
Dim KName$, Ret$, SName$
SName$ = "Intl" ' WIN.INI section name.
KName$ = "sCountry" ' WIN.INI country setting.
Ret$ = String$(255, 0) ' Initialize return string.
' Call Windows Kernel DLL.
Success = GetProfileString(SName$, KName$, "", Ret$, Len(Ret$))
If Success Then ' Evaluate results.
Msg = "Your country setting is: " & Ret$
Msg = "There is no country setting in your WIN.INI file."
MsgBox Msg ' Display message.