Give me Standard Step By Step Programming Method (Logic) TO
call API From VB.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Calling an API from VB is pretty easy.  There is an "API Text Viewer" that comes with VB.  All of the windows APIs defined can be copied to the clipboard from it and pasted into your program from it.

When you run it, open the file "Win32api.txt".  This file has all of the definitions in it.

To call an API you need to put a DEFINE statement in your program... Either in the DECLARATION SECTION of a form or in a MODULE.

If you use a form, you must make it a PRIVATE DECLARATION.

An example of an API is the GetCursorPos API.  With it, you can get the position of the mouse pointer on the screen.

For example, put the following in a module:

        x As Long
        y As Long
End Type
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Notice that the function uses a type of POINTAPI and the type is defined above.  This type declaration is also found in the API viewer... After you load the "Win32api.txt" file into the viewer, just select "types" in the dropdown box...

Now to use the GetCursorPos function:

Put the following code in a timer event on a form and run the program:

Dim MouseLoc as POINTAPI
dim Retcode as Long
debug.print MouseLoc.x, MouseLoc.y

the mouse's physical X/Y location will be displayed in the Immediate Window...


Now whats's happening...

The GetCursorPos takes the argument MouseLoc and loads the X/Y items of it and when the API terminates, Retcode is loaded with the return value of the API.

Simple huh?


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
'more the merrier..here's another.
'it's Christmas

'Move a file using API
'put this in the gen declarations

Private Declare Function MoveFile Lib "kernel32.dll" Alias _
"MoveFileA" (ByVal lpExistingFileName As String, ByVal _
lpNewFileName As String) As Long
'put this in the command button

' The original file will no longer exist.  Note how this example both changes
' the filename and moves the file into a different directory simultaneously.

Dim retval As Long  ' return value

retval = MoveFile("C:\download\file.txt", "c:\my documents\file.txt")

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.