Library tutorials & articles

API Programming Series #2

ByVal and ByRef

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

  1. 29 May 2003 at 17:00

    The current release of the ApiViewer can now be found here:
    http://www.activevb.de/rubriken/apiviewer/index-apiviewereng.html


    or (German):
    http://www.ApiViewer.de

  2. 17 Dec 2002 at 12:44
    Nice job!! I really like the resource links and continued learning suggestions. I can't wait til my site is ready, so I can give back to the community. Stay Tuned!!
    charcoal
  3. 20 Nov 2002 at 02:25

    i'm faithfully following his series.

  4. 19 Nov 2002 at 11:46

    he's already posted part 4... i've just gotta get around to publishing 'em

  5. 19 Nov 2002 at 07:10

    i thought he was never gonna come back

  6. 01 Jan 1999 at 00:00

    This thread is for discussions of API Programming Series #2.

Leave a comment

Sign in or Join us (it's free).

Sreejath S. Warrier

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback