william007
asked on
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
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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
My last question,
1)Will it be correct if we put "byval &0" since sometimes we specify hexadecimal as &H15 etc..
1)Will it be correct if we put "byval &0" since sometimes we specify hexadecimal as &H15 etc..
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks:-)
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
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
Oops forgot that last comment.
vbNull is a value in the Enum VbVarType equalling 1. I screwed up on that before as well. :-(
vbNull is a value in the Enum VbVarType equalling 1. I screwed up on that before as well. :-(
ASKER
Thansk for your additional comment, zzzzzooc:)
But where does Enum VbVarType declare?
But where does Enum VbVarType declare?
ASKER
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:)
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:)
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
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
ASKER
Thanks, this is very helpful!
I see if posted vbNull (as reveiled by zzzzzzoc), let me correct to:
vbNullChar
vbNullChar
ASKER
Hi, angellll,
seems like it is more appropriate to be
"Null" :)
seems like it is more appropriate to be
"Null" :)
ASKER
as vartype(vbnullchar) = vbstring
but vartype(null) = vbnull
but vartype(null) = vbnull
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.
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.
ASKER
Hi, there is a related question here,
https://www.experts-exchange.com/questions/21785748/Passing-Byval-to-ByRef.html
https://www.experts-exchange.com/questions/21785748/Passing-Byval-to-ByRef.html
ASKER
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 "&"?