?
Solved

What is ByVal 0&

Posted on 2006-03-20
17
Medium Priority
?
3,140 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:william007
  • 9
  • 4
  • 4
17 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16234540
byval 0& corresponds to the NULL (vbNull) value as parameter)
0
 
LVL 9

Author Comment

by:william007
ID: 16234596
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
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16234630
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

Author Comment

by:william007
ID: 16234648
My last question,
1)Will it be correct if we put "byval &0" since sometimes we specify hexadecimal as &H15 etc..
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16234671
the prefix & is another meaning the the postfix &, the postfix indicates the data type, while the & indicates that it is a hex value
0
 
LVL 9

Author Comment

by:william007
ID: 16235223
Thanks:-)
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 16245118
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
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 16245123
Oops forgot that last comment.

vbNull is a value in the Enum VbVarType equalling 1. I screwed up on that before as well. :-(
0
 
LVL 9

Author Comment

by:william007
ID: 16245158
Thansk for your additional comment, zzzzzooc:)
But where does Enum VbVarType declare?
0
 
LVL 9

Author Comment

by:william007
ID: 16245240
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
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 16245262
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
 
LVL 9

Author Comment

by:william007
ID: 16245298
Thanks, this is very helpful!
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16245505
I see if posted vbNull (as reveiled by zzzzzzoc), let me correct to:
vbNullChar
0
 
LVL 9

Author Comment

by:william007
ID: 16245534
Hi, angellll,
seems like it is more appropriate to be
"Null" :)
0
 
LVL 9

Author Comment

by:william007
ID: 16245545
as vartype(vbnullchar) = vbstring
but vartype(null) = vbnull
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 16245619
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
 
LVL 9

Author Comment

by:william007
ID: 16269129
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question