What is ByVal 0&

I have seen the code below, but what is the meaning of "ByVal 0&"?

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long

Function InstanceToWnd(ByVal target_pid As Long) As Long
    Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
    test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
End Function
LVL 9
william007Asked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
byval 0& corresponds to the NULL (vbNull) value as parameter)
0
 
william007Author Commented:
Thanks,
1. Will it be correct if I just put "0&" instead of "byval 0&"? What is that byval for?
2. What is the meaning of the symbol "&"?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
1) no. this is a "special" convention by vb for DLL function calls to pass a empty value for "pointer" values.
2) the & is also needed, it indicates that the data type of the 0 should be "long" and not integer or byte.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
william007Author Commented:
My last question,
1)Will it be correct if we put "byval &0" since sometimes we specify hexadecimal as &H15 etc..
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the prefix & is another meaning the the postfix &, the postfix indicates the data type, while the & indicates that it is a hex value
0
 
william007Author Commented:
Thanks:-)
0
 
zzzzzoocCommented:
angelIII is correct (except for one portion I'll mention at the end) but as for more specifically why it's used in API functions..

Take this function for example:
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

The last parameter is declared ByRef (default when ByVal isn't mentioned) and as Any. Normally, it's used to pass certain structures (UDTs) ByRef but it's also meant to pass values ByVal. If you try to pass a Long value to that function, the DLL will attempt to write to protected memory (because it's ByRef) and cause your application to crash, most-likely. To overcome this, you'd have to pass it ByVal and type-cast it, such as..

1.)
    Call SendMessage(hWnd, WM_TEST, 0, ByVal 6&)
2.)
    Call SendMessage(hWnd, WM_TEST, 0, ByVal CLng(6))
3.)
    Dim val As Long
    val = 6
    Call SendMessage(hWnd, WM_TEST, 0, ByVal val)

This is also why you'll see the same functions, but named different with different declarations such as:

Private Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

0
 
zzzzzoocCommented:
Oops forgot that last comment.

vbNull is a value in the Enum VbVarType equalling 1. I screwed up on that before as well. :-(
0
 
william007Author Commented:
Thansk for your additional comment, zzzzzooc:)
But where does Enum VbVarType declare?
0
 
william007Author Commented:
Thanks, I knew what you mean,
VbNull is vb internal Enum value, has a constant value 1 and we should compare it using
vartype(i)=vbnull,
Thanks again for this precious info:)
0
 
zzzzzoocCommented:
It's not very relevant to this question but it's already declared within VB and it used by the VarType() function.

    Dim s As String
    Select Case VarType(s)
        Case VbVarType.vbString
            MsgBox "this is a string!"
    End Select

As for Null (VB keyword), I'm not sure of any specific use for it except for Variants to signify it contains nothing. angelIII was correct in saying 0& corresponds to a "null" value as 0 usually represents that in C/C++ but don't confuse that with the Null keyword. There's also vbNullChar (Chr(0)) (and vbNullString = "") and null-characters are generally used by other programming languages to terminate strings because they don't store their lengths (c/c++ for example actually have to loop through the entire string and find a null-char to know when the string ends!). VB uses BSTRs though, which store the strings' lengths, but that's a different topic.

    Dim v As Variant
    v = Null
    Select Case VarType(v)
        Case VbVarType.vbNull
            MsgBox "this is null!"
    End Select
0
 
william007Author Commented:
Thanks, this is very helpful!
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
I see if posted vbNull (as reveiled by zzzzzzoc), let me correct to:
vbNullChar
0
 
william007Author Commented:
Hi, angellll,
seems like it is more appropriate to be
"Null" :)
0
 
william007Author Commented:
as vartype(vbnullchar) = vbstring
but vartype(null) = vbnull
0
 
zzzzzoocCommented:
Too many things called null!

william is correct that vbNullChar is a string (Chr(0)) and wouldn't correspond with 0& but neither will Null, I don't think. 0& (or 0) still represents a "null", but not the Null keyword, a null-character or vbNull. It's just 0... which is probably just referred to as "null".

So.. There's a null term, a Null keyword, a Null character and a Null string. Let's not forget Empty. :)~ All have their proper uses and I'm not sure of every one of them.
0
 
william007Author Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.