Library tutorials & articles

API Programming Series #2

The Code

First, load VB and create a new standard EXE project. A form should have been added to the project by default. In the General | Declarations section of the form's code window add the following code:

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal sBuffer As String, lSize As Long) As Long

Now add the following code to an appropriate event handler/procedure. Here I've used the Form_Load event

Private Sub Form_Load ()
  Dim strString As String
  'Create a buffer
  strString = String(255, Chr$(0))
  'Get the computer name
  GetComputerName strString, 255
  'Remove the unnecessary Chr$(0)
  strString = Left$(strString, InStr(1, strString, Chr$(0)) - 1)
  'Show the computer name
  MsgBox strString
End Sub

Please examine the Declaration part carefully. Straightaway, we can notice several things. First of all we see that the scope of the Declaration is private. This is because VB does not allow Public Declare statements in object modules. Next we see that this is the declaration for a Function and that the name by which the function is referred in this program is GetComputerName. This is NOT the real name of the function as will become clear when we discuss aliasing in a short while.

The next two words are interesting. Lib "kernel32" means that the specified API function is found in the kernel32.dll file. Strictly speaking, it should be "kernel32.dll" but the extension name can be skipped. Also, the path can be skipped since the file is to be found in the System folder.

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
AddThis

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