Calling Windows API Functions

One of the major features of .NET is that it is platform independent. However, there are many powerful functions available in the Windows API, so there is little doubt that some developers will want to a call Windows API function from time to time. Fortunately, VB .NET includes the Declare statement, which allows you to call Windows API functions.

The Declare statement in VB .NET is very similar to the one from VB6, with some minor modifications. You might recall from your VB6 work that functions have both an ANSI and a Unicode implementation in the API: an A suffix means the ANSI version, whereas a W suffix indicates Unicode. Examine the following sample Declare statement:

Declare Function GetComputerName Lib "kernel32.dll" _
  Alias "GetComputerNameA" (ByVal lpBuffer As String, _
  ByRef nSize As Long) As Long

You see here that you are using a Declare to create a function that points to the ANSI implementation of GetComputerName. By default, the Declare statement assumes the ANSI version is what you will be calling; no modifier is needed. If you choose to call the Unicode version, you add a modifier to the Declare statement to force strings to be passed as Unicode.

Declare Unicode Function GetComputerName Lib "kernel32.dll" _
  Alias "GetComputerNameW" (ByVal lpBuffer As String, _
  ByRef nSize As Long) As Long

Whether you choose ANSI or Unicode is immaterial in most VB .NET applications.

Calling a Windows API Function

You can call a Windows API function using the Declare function and then calling the function as you would any other function. Create a new Windows application named ApiTest. Add a button to the application, and make your code look like this (with the Windows Form Designer generated code hidden, of course):

Public Class Form1
   Inherits System.Windows.Forms.Form
   Declare Function GetComputerName Lib "kernel32.dll" _
     Alias "GetComputerNameA" (ByVal lpBuffer As String, _
     ByRef nSize As Long) As Long

#Region " Windows Form Designer generated code "

   Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
      Dim sMachineName As String = Space(50)
      Dim lLength As Long = 50
      Dim lRetVal As Long

      lRetVal = GetComputerName(sMachineName, lLength)

      MsgBox("Current Machine: " & sMachineName)
   End Sub
End Class

In this code, you are using the Windows API to get the name of the current computer. You declare a function called GetComputerName that references the GetComputerNameA in the kernel32.dll.

Inside the click event sub, you create a string variable and fill it to a size of 50. You then create a long that you also initialize to 50. If you have done much with API calls, you know that you often have to pass in a string that has already been initialized to a large enough size to hold the value that will be returned. What is strange is that even though the lpBuffer parameter is defined with the ByVal keyword, the return value is actually passed back to the string. The nSize parameter is ByRef, and the size of the return is passed back through this parameter.

If you run this code, the message box will display the name of the computer on which the code is running. Congratulations! You have just called a Windows API function from within a .NET application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.165.2