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.
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
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...
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
charcoal
he's already posted part 4... i've just gotta get around to publishing 'em
i thought he was never gonna come back
This thread is for discussions of API Programming Series #2.