by Syed Natiq Abbas Kazmi (3 Submissions)
Category: OLE/COM/DCOM/Active-X
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sun 27th July 2003
Date Added: Mon 8th February 2021
Rating: (1 Votes)
How to declare and use a DLL/API procedure
API Declarations
Alias "SetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String) As Long
'procedures in an external DLL or API in Visual Basic.
'•-- Declaring a DLL Procedure
'•-- The first step is to declare the procedure in the Declarations section of
' a module:
' •-- You can find the exact syntax for a procedure by using the API Viewer
' •-- application, or by searching the Win32api.txt file. If you place the Declare
' •-- in a Form or Class module, you must precede it with the Private keyword.
' •-- You declare a DLL procedure only once per project. You can then call it
' •-- any number of times.
' •-- Calling a DLL Procedure:
' •-- After the function is declared, you call it just as you would a standard
' •-- Visual Basic function. Here, the procedure has been attached to the
' •-- Form Load event:
Private Sub Form_Load()
SetWindowText Form1.hWnd, "Welcome to VB"
End Sub
' •-- When this code is run, the function first uses the hWnd property
' •-- to identify the window where you want to change the caption (Form1.hWnd),
' •-- then changes the text of that caption to "Welcome to VB."
' •-- Remember that Visual Basic cannot verify that you are passing correct values
' •-- to a DLL procedure. If you pass incorrect values, the procedure may fail, which
' •-- may cause your Visual Basic application to hang... or worse. :)