PDA

Click to See Complete Forum and Search --> : Some API questions


Paca
Feb 28th, 2001, 05:21 PM
Hello,

I'm pretty new to the whole VB scene and I'm interested in using API for a few things. I have a few questions:

1: How can I use API to click a button or run a command on another program? For example, say I wanted to click a button on my VB program, then by doing that it would click a button or run a command on another program currently running.

2: How would I go about finding the hWnd and other info from another program's windows?

3: I've heard of API spy utilities, but I know nothing about them really, if I was going to use one what should I get?

That is it for now, if you have any other useful tips or comments for these question, please post those as well.

-paca

amitabh
Feb 28th, 2001, 08:37 PM
Do you have any experience in programming(especially C/C++)? If yes, then you can go to www.vbapi.com and www.allapi.net for further reference. If no, then first you should become comfrtable with VB and then touch Api programming.

Feb 28th, 2001, 08:39 PM
Q1:


Private Declare Function PostMessage _
Lib "user32" Alias "PostMessageA" (ByVal hwnd As _
Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long

Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const MK_LBUTTON = &H1


Private Sub Command1_Click()
PostMessage Command2.hwnd, WM_LBUTTONDOWN, MK_LBUTTON, 0
PostMessage Command2.hwnd, WM_LBUTTONUP, MK_LBUTTON, 0
End Sub



Q2:


Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long


Private Sub Command1_Click()

Dim hWin As Long
'Find the Calculator
hWin = FindWindow("SciCalc", "Calculator")
'hWin=FindWindow(Class,Caption)

If hWin <> 0 Then

'if Calculator found then...
Msgbox "Calculator hWnd: " & hWin
Else
'else if Calculator not found...
Msgbox "Calculator not found"
End If

End Sub

Mar 1st, 2001, 03:13 PM
Question 1

It's shorter just to send the BM_CLICK message.

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const BM_CLICK = &HF5

Private Sub Command1_Click()
PostMessage command2.hwnd, BM_CLICK, 0, 0
End Sub


Question 2
It's more efficient to use FindWindowEx, because it allows you to search for child windows as well.

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Sub Command1_Click()

Dim hWnd_App As Long
hWnd_App = FindWindowEx(0, 0, "Notepad", vbNullString)

End Sub



Question 3:

You can make your own with a few API calls. Add to a Form with a Timer and 3 Labels.

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type

Private Sub Form_Load()
Timer1.Interval = 1
End Sub

Private Sub Timer1_Timer()
Dim hWnd_Window As Long
Dim sClassName As String * 255
Dim sWindowName As String * 255
Dim iLength As Integer
Dim PT As POINTAPI

GetCursorPos PT
hWnd_Window = WindowFromPoint(PT.x, PT.y)
iLength = GetClassName(hWnd_Window, sClassName, 255)
sClassName = Left(sClassName, InStr(1, sClassName, vbNullChar))
iLength = GetClassName(hWnd_Window, sWindowName, 255)
sWindowName = Left(sWindowName, InStr(1, sWindowName, vbNullChar))

Label1 = hWnd_Window 'Display the hWnd
Label2 = sClassName 'Display the class name
Label3 = sWindowName 'Display the window name
End Sub

PatrickCorgan
Mar 2nd, 2001, 01:23 AM
If you are at all interested in the API check this site out. They have a couple of great tools for using the API.

Paca
Mar 2nd, 2001, 02:22 AM
Originally posted by Megatron

Question 3:

You can make your own with a few API calls. Add to a Form with a Timer and 3 Labels.

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Type POINTAPI
x As Long
y As Long
End Type

Private Sub Form_Load()
Timer1.Interval = 1
End Sub

Private Sub Timer1_Timer()
Dim hWnd_Window As Long
Dim sClassName As String * 255
Dim sWindowName As String * 255
Dim iLength As Integer
Dim PT As POINTAPI

GetCursorPos PT
hWnd_Window = WindowFromPoint(PT.x, PT.y)
iLength = GetClassName(hWnd_Window, sClassName, 255)
sClassName = Left(sClassName, InStr(1, sClassName, vbNullChar))
iLength = GetClassName(hWnd_Window, sWindowName, 255)
sWindowName = Left(sWindowName, InStr(1, sWindowName, vbNullChar))

Label1 = hWnd_Window 'Display the hWnd
Label2 = sClassName 'Display the class name
Label3 = sWindowName 'Display the window name
End Sub
[/B]

I made myself a nice little API spy using that code, I just changed a few things like putting in a start and stop button in and using text boxes instead of labels (that way I can copy and past lol). I would zip the program and put it with this post, but I don't know if you are alowed to post exe(zipped)s on here, can you?

Paca
Mar 2nd, 2001, 06:21 AM
Megatron showed me how to get the hWnd, class name, and window name, but I was wondering if anyone could show me how to get the window text, window ID number, window style, parent window hWnd, parent window class name, and the parent window text?

Mar 2nd, 2001, 06:36 AM
Try this:


Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long


Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long


Public Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Long, ByVal nIndex As Long) As Integer


Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpstring As String, ByVal cch As Long) As Long


Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long


Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long


Public Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long


Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long


Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long


Public Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal yPoint As Long) As Long


Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long


Type POINTAPI
x As Long
y As Long
End Type



Public Sub hWndSpy(WinHdl As TextBox, WinClass As TextBox, WinTxT As TextBox, WinStyle As TextBox, WinIDNum As TextBox, WinPHandle As TextBox, WinPText As TextBox, WinPClass As TextBox, WinModule As TextBox)
Dim pt32 As POINTAPI, ptx As Long, pty As Long, sWindowText As String * 100
Dim sClassName As String * 100, hWndOver As Long, hWndParent As Long
Dim sParentClassName As String * 100, wID As Long, lWindowStyle As Long
Dim hInstance As Long, sParentWindowText As String * 100
Dim sModuleFileName As String * 100, r As Long
Static hWndLast As Long
Call GetCursorPos(pt32)
ptx = pt32.X
pty = pt32.Y
hWndOver = WindowFromPointXY(ptx, pty)


If hWndOver <> hWndLast Then
hWndLast = hWndOver
WinHdl.Text = "Window Handle: " & hWndOver
r = GetWindowText(hWndOver, sWindowText, 100)
WinTxT.Text = "Window Text: " & Left(sWindowText, r)
r = GetClassName(hWndOver, sClassName, 100)
WinClass.Text = "Window Class Name: " & Left(sClassName, r)
lWindowStyle = GetWindowLong(hWndOver, GWL_STYLE)
WinStyle.Text = "Window Style: " & lWindowStyle
hWndParent = GetParent(hWndOver)


If hWndParent <> 0 Then
wID = GetWindowWord(hWndOver, GWW_ID)
WinIDNum.Text = "Window ID Number: " & wID
WinPHandle.Text = "Parent Window Handle: " & hWndParent
r = GetWindowText(hWndParent, sParentWindowText, 100)
WinPText.Text = "Parent Window Text: " & Left(sParentWindowText, r)
r = GetClassName(hWndParent, sParentClassName, 100)
WinPClass.Text = "Parent Window Class Name: " & Left(sParentClassName, r)
Else
WinIDNum.Text = "Window ID Number: N/A"
WinPHandle.Text = "Parent Window Handle: N/A"
WinPText.Text = "Parent Window Text : N/A"
WinPClass.Text = "Parent Window Class Name: N/A"
End If
hInstance = GetWindowWord(hWndOver, GWW_HINSTANCE)
r = GetModuleFileName(hInstance, sModuleFileName, 100)
WinModule.Text = "Module: " & Left(sModuleFileName, r)
End If
End Sub


Usage

Call hWndSpy(text1,text2,text3,text4,text5,text6,text7,text8,text9)

Paca
Mar 2nd, 2001, 08:38 AM
That worked, just had to add some stuff because I used a module, thanks Matthew Gates.