Creating a Procedure and Specifying a DLL

If your procedure returns a value, write the declaration as a function. If it does not return a value, write the declaration as a subprocedure. However, procedures almost invariably are functions, and for this reason the remainder of these sections on accessing the Windows CE API focus on the process of declaring a new function.

The first step is to open the Declarations section of a module and add the Declare keyword, the function name you want to declare, the Lib keyword, and the file name of the DLL where the function is located. The following code example shows a declaration for the GetLastError function.

Declare Function GetLastError Lib "coredll.dll" () As Long

After you enter the Declare keyword, the toolkit automatically loads Pvbdecl.dll, the file that supports the Declare statement, into the compiler. This file must be present on a device for the Declare statement to work correctly.

In the code example, the Declare statement associates the Win32 GetLastError function with the Windows CE GetLastError function in Coredll.dll. Because the .dll file extension is optional, you can specify either “Coredll.dll” or simply “coredll”; however, regardless of which name you use, always enclose the library name in quotation marks in your code.

When you call the Win32 GetLastError function, the compiler calls the corresponding function in Coredll.dll and receives the return value. The compiler then returns the value to your code.

The Lib clause points to the .dll file that contains the procedure. The majority of Windows CE functions are contained in the file Coredll.dll. In addition, you can find common Windows CE functions in User32.dll and Kernel32.dll.

In the previous code example, the name of the declared function, GetLastError, matches the name of the GetLastError function in the Windows CE API. However, you can declare a function with a unique name. If the name of your function differs from the function contained in the Windows CE API, you must add the Alias keyword to your declaration.

In addition, when you work with Windows CE functions that contain strings, you must add the Alias keyword followed by the name of the Unicode version of the Windows CE function you want to call. The following code example shows a declaration for the Windows CE function CreateFile. The “CreateFileW” after the Alias keyword designates that the function is the Unicode version of the Win32 CreateFile function. Because Windows CE supports only Unicode, there is no corresponding ASCII version of the function.

Public Declare Function CreateFile Lib "coredll" Alias "CreateFileW" 

Note that the code example includes the Public keyword. DLL procedures declared in standard modules are public by default; however, if you want your application to be compatible with Visual Basic 6.0, you can add the Public keyword to your declaration, although this keyword has no effect in Windows CE-based applications.

Windows CE does not support the Private keyword. Thus, procedures declared in a form can be used only within that form. However, procedures declared in modules can be called from anywhere in your application.

For more information about the syntax of the Declare statement, see Declare.