After you specify a function name, the DLL containing the function you want to call, and the name of the Windows CE function you want call, you must match all parameters, including the data type and return values, associated with your function to those contained in the corresponding Windows CE API.
One of the most complex tasks in using the Declare statement is to match the data types of the parameters. The following table shows six common Visual Basic for Windows CE data types and their corresponding base and Win32 data types.
Declare Type |
Base Type |
Win 32 Type |
Byte | char, unsigned char | BYTE, CHAR |
Boolean | long, boolean | BOOL, BOOLEAN |
Integer | int, unsigned int, short, unsigned short, wchar, tchar | INT, UINT, SHORT, USHORT, WORD, VARIANT_BOOL, VARTYPE, WPARAM, HFILE |
Long | long, unsigned long | BOOL, LONG, DWORD, LPARAM, LRESULT, ULONG, SCODE, HRESULT |
String | char*, wchar* | LPSTR, LPCSTR, LPWSTR, LPTSTR, LPCWSTR, LPCTSTR, BSTR |
Variant | tagVARIANT | VARIANT, VARIANTARG |
All Windows CE API parameters and return values are specified in the Microsoft Windows CE Platform SDK. The following code example shows the syntax provided by the Microsoft Windows CE Platform SDK for the CreateFile function.
HANDLE CreateFile(LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDistribution,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile);
The following code example shows how to match the parameters, data types, and return values in a Declare statement to the Windows CE CreateFile function.
Public Declare Function CreateFile Lib "coredll" Alias "CreateFileW" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As Long
For the LPCTSTR data type, declare the parameter As String. For the DWORD and HANDLE data types, declare the parameters As Long.
The CreateFile function uses the unsupported data type, SECURITY_ATTRIBUTES. The Microsoft Windows CE Platform SDK states that you must pass the Null value to the CreateFile function. When you call the CreateFile function, you can pass the constant vbNullPtr by reference through the Long data type to represent Null. Because the return type in the CreateFile function is HANDLE, you can use As Long for the return type.
In order to use the Declare statement in your code efficiently and accurately, take note of the following requirements: