some of the win API functions contain parameters which are clearly marked as "by val".
But whatever modifications we do to these parameters can be seen in calling function(for example getusername function)
what is this peculiarity?
Printable View
some of the win API functions contain parameters which are clearly marked as "by val".
But whatever modifications we do to these parameters can be seen in calling function(for example getusername function)
what is this peculiarity?
By Val and By Ref are used by APIs. You seldom see a By Ref (By Reference) because that is the default. If you do not explicitly pass something By Val (By Value) it will automatically be passed By Ref.
The difference is that a value passed By Val can not be changed. A value passed By Ref can be changed.
In the context of string data types in the API the keywords ByVal and ByRef have a special meaning.
This is because most API calls expect a C style null terminated string data type but internally VB string variables are stored as BSTR format.
If you see ByVal in a string parameter it means that VB will take a copy of the BSTR as a C string, copy the data in to it, call the API and then copy the result back.
Therefore if the API call definition wants a data type of LPSTR, LPCSTR or the like, use ByVal, if the data type is LPBSTR don't.
See also "The Visual Basic Programmer's Guide to the Win32 API" by Daniel Appleman - he explains this much more lucidly...
HTH,
Duncan