Home arrow Visual Basic arrow Page 5 - Accessing the Windows API in Visual Basic

Accessing the Windows API in Visual Basic

The Windows Application Programming Interface, or API, is a complex set of functions for interacting with Windows. In this article, Jason attempts to shed some light on this topic by explaining what an API is, and providing an example of how to access one through Visual Basic.

Author Info:
By: Jason Brimblecombe
Rating: 4 stars4 stars4 stars4 stars4 stars / 127
December 12, 2001
  1. · Accessing the Windows API in Visual Basic
  2. · What is the Windows API?
  3. · Exploring the API function list
  4. · Your first API call
  5. · Calling the API
  6. · Conclusion

print this article

Accessing the Windows API in Visual Basic - Calling the API
(Page 5 of 6 )

Place a command Button on your form and set the caption to 'Show Windows Directory'. I am going to assume that the name of this command button has been left as Command1. We will be attaching code to the Click event of this button, so open up the Command1_Click() sub-routine and enter the code below:

Private Sub Command1_Click()

Dim TheResult

Dim TheWindowsDirectory As String

TheWindowsDirectory = Space(144)

'Fill 144 spaces in TheWindowsDirectory string

TheResult = GetWindowsDirectory(TheWindowsDirectory, 144)

'Get path and place it in TheResult string

If TheResult = 0 Then

MsgBox "Cannot get the Windows Directory"


'Prepare the String for preview, and then display in a Message Box

TheWindowsDirectory = Trim(TheWindowsDirectory)

MsgBox "The Windows Path: " & TheWindowsDirectory

End If

End Sub

Run the program and click on the command button. A messagebox similar to the one shown below should be displayed:

The text of the message box is the windows director

This message box shows that Windows has been installed into the directory (or folder) with the path 'C:\Windows\'. Your box may vary to mine, depending on where Windows has been installed on your computer.

You may be asking yourself why an API function like this would be used. Well, if your project was built to perform a task that has a use for the Windows directory, then you are going to need to know the location of it, and using the API is the easiest way.

If you hard coded the windows directory into your application. then an error may occur, because not all people choose to install Windows to the default c:\windows directory.

There are other API functions available through the same DLL that perform a similar task to the one that we have just coded. For example, 'GetSystemDirectory' will display the full path to the Windows System Directory. In my case, the result would be 'C:\Windows\System', but it may say something different on other systems, depending on where Windows is installed.
blog comments powered by Disqus

- Developing an XML Web Service Using Visual S...
- Creating an HTML File List with VB
- Fun with Email: VB6, CDO, MAPI, and a Remote...
- Extranet/Intranet Dictionary Cracker in VB
- Finding Default App Icons With Visual Basic
- Registry Fever With Visual Basic
- Implementing An ADO Data Control With VB6
- Printing With Visual Basic
- MSMQ Part 1/2: Architecture and Simple Imple...
- Magnifying The Desktop With Visual Basic
- Sending Email With MAPI Components in Visual...
- Two Person Chat With The Winsock Control And...
- A Real-Time ActiveX News Control
- Accessing the Windows API in Visual Basic

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials