Calling Windows API Functions

See AlsovbprocWinAPISee

Information on the Microsoft Windows Application Programming Interface (API) is available in two Help files located in the WINAPI subdirectory of your Visual Basic directory.  The WIN31API.HLP file contains function declarations, Type declarations, and the values for global constants used in the functions for Visual Basic programs.  The WIN31WH.HLP file documents each function in terms of the C programming language.  This is the same Help file included in the Microsoft Windows Software Development Kit (SDK).

To open a WinAPI Help file from Help

  1. From the File menu in Help, choose Open.

  2. Select the WINAPI directory.

  3. Double-click a file to open it.

      If you have installed the Professional Edition, you can click the file names:

      WIN31API.HLP!JumpContents("win31api.hlp") and WIN31WH.HLP!JumpContents("win31wh.hlp").

To use a Windows API function with the Professional Edition

  1. Select the function you want from the information in WIN31WH.HLP, which gives a description of the function's purpose and requirements (in terms of the C programming language, not Basic).  For example, a useful function is GetPrivateProfileInt, which reads a specific .INI file and returns a number from a specified setting.  Be sure to find whether the function requires a STRUCT (or structure) requiring a Type declaration in Visual Basic.

  2. In WIN31API.HLP, find the desired function, such as GetPrivateProfileInt, in the Declare Statements list and click it.

  3. Click the Copy button and select the entire declaration text.  Click the Copy button, then the Close button.

  4. Paste the declaration text into the Declarations section of a module.

  5. If your function requires a Type declaration or any constant declarations, go back to WIN31API.HLP and click the Contents button, then copy the needed Type declaration and constant declarations.


For example, you can use this code to get the keyboard type from your SYSTEM.INI file:

  ' The Declarations section of your module:
  Declare Function GetPrivateProfileInt Lib "Kernel" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer

  Sub Form_Load ()
    Dim KeyboardType As Integer
    KeyboardType = GetPrivateProfileInt("Keyboard", "type", 2, "C:\WINDOWS\SYSTEM.INI")
    MsgBox "Your keyboard type is "; KeyboardType
  End Sub


Warning   Windows functions can require long lines of code and very long declaration statements.  You must be sure you have the entire declaration or function call on one line.  You must also be very careful to provide arguments of the correct data type.  The wrong data type may cause a General Protection Fault at run time.