Some messages
contain parameters declared as Any.
It means this parameter can be a variety of types (you may pass
an integer, a string, or a user-defined type, or else). So, here
is an example of a function (SendMessage)
which contains a parameter of type Any:
Public
Declare Function SendMessage Lib "User32" Alias "SendMessageA"
(ByVal Hwnd as Long, ByVal wMsg as Long, ByVal wParam as Long,
lParam as Any) as Long
lParam
is declared ByRef (default) and As
Any. Now, here are some rules to follow when passing different
type of values to this function as lParam.
If
the value is |
Pass
it As |
numeric |
ByVal
(As Long, or As Any) |
Null |
ByVal
(As Long, or As Any) |
String |
ByRef
(As String, or As Any) |
Type |
ByRef
(As Any) |
array
of Type |
ByRef
(As Any) |
If your function
declaration looks like the one above, and you need to pass a Long,
write something like:
Call
SendMessage(Me.Hwnd, WM_XXXX, 0&, ByVal LongValue)
Note that
there is nothing in front of the first three parameter although
they are numeric values. This is so, because in the function declaration
they are declared as ByVal. The fourth parameter, though, is declared
ByRef (VB doesn't know what kind of values you are going to pass)
and you must explicitly specify ByVal in front of it.
Sometimes
it's much simpler to just declare several versions of one function
and use a different one for different calls. You may declare something
like:
Public
Declare Function SendMessageLng Lib "User32" Alias "SendMessageA"
(ByVal Hwnd as Long, ByVal wMsg as Long, ByVal wParam as Long,
ByVal lParam as Long) as Long
or
Public
Declare Function SendMessageStr Lib "User32" Alias "SendMessageA"
(ByVal Hwnd as Long, ByVal wMsg as Long, ByVal wParam as Long,
lParam as String) as Long
Notice that
the parameter type does not change for API. The fourth parameter
is always a 4-byte Long value. When
you pass a Long or Null ByVal, a the 4-byte value is passed directly
to the function. If you pass a String or else, you pass it ByRef
and so VB actually passes the address of you variable, and it
is a 4-byte value again.