Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 47,126 times

Contents

Related Categories

Windows API - Using Windows API

Using Windows API

Once you have declared your Windows API function, you need to be able to use it.   Windows API functions take a number of different formats. The simplest one is a DLL that just does something, and does not return any strings or pointers. An example is provided below:

'// DLL declarations and constants
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_UNDO = &H304 ' Undos the last action (Ctrl+Z)

'// Command Button Event
Sub cmdUndo_Click()
    SendMessage(txtText.hWnd, WM_UNDO, 0, 0& )
End Sub

If you want more information on using the SendMessage api function, see the Sending Messages article.

Another common function is one that returns a string which contains text. The following example shows you how to create a function that will get the Windows directory.

'// DLL declarations and constants
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Const MAX_PATH As Integer = 260

'// Get Windows Directory function
Public Function GetWindowsDir() As String
    Dim strWinDir As String '// String that will contain win path
    Dim lngResult As Long '// Long Variable that will contain the result value (success/fail)
    '// Fill the Windows dir variable with 260 spaces (259 is the maximum path length, with one space for the trailing null)
    '// if you do not give enough space, you will not get anything at all.
    strWinDir = Space(MAX_PATH)
    '// Call the function, and get the result. We pass the strWinDir variable as a parameter,
    '// this way the DLL can fill it with the result. The last parameter specifies the length of the string
    lngResult = GetWindowsDirectory(strWinDir, MAX_PATH)
    '// suceeded?
    If lngResult = 0 Then
        '// failed, return nothing
        GetWindowsDir = vbNull
    Else
        '// successful, trim result string and return it
        GetWindowsDir = Trim(strWinDir)
    End If
End Function

Other Windows API declarations are more complex, and the best way to learn is to download some code from the VB Component section, and examine it yourself.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments