If you do API programming for any length of time, it is impossible to avoid these two terms. They refer to two methods used to pass arguments to functions. ByVal means that the argument is passed by value. I.e. a copy of the argument is made and this copy is passed to the function. Any changes that the function makes to its own copy of the data are not reflected to the original value. ByRef means that the address of the variable is passed as argument to the function. So any changes the function makes to the argument is immediately reflected in the original variable. In passing arguments to API functions, ByRef is the default, whereas if you need to pass an argument ByVal, it needs to be explicitly specified.
If you want a more detailed discussion on this, you can read this article
Now let us analyse the rest of the code. In the Form_Load event procedure we first create a string buffer, strString. Then we pre-size (or stuff) it with enough Chr$(0) (space) so that it is larger than the maximum length that the computer name can have.
Warning: If you skip the previous step, you may get back an empty string as the result. And though it has never occurred to me, Microsoft warns that you may even crash the program if you fail to pre-size the buffer.
After that, we pass the pointer to the buffer and an integer value, which denotes the length of the buffer to the GetComputerName function. The strString buffer now contains the computer name and trailing spaces. In the final step we lop off the spaces to get the computer name, which we display, using the MsgBox function.
That is all there is to it! And I guess the time has come for me to say goodbye. If you have any doubts, comments or questions, do feel free to mail me. Happy coding!
Comments