Visual Basic Code Help |
Author: |
Message: |
DJeX
Veteran Member
Posts: 1138 Reputation: 11
– / / –
Joined: Jul 2003
|
O.P. Visual Basic Code Help
I found this code on planetsourcecode and I need to know how to get it to work.
code: Private Sub Click(Btnhwnd As Integer)
Dim ChildID As Integer
Dim Nul As Integer
Const GWW_ID = (-12)
ChildID = GetWindowWord(Btnhwnd, GWW_ID)
Nul = SendMessageByNum(GetParent(Btnhwnd), WM_COMMAND, ChildID, ByVal CLng(Btnhwnd))
End Sub
Private Sub Command1_Click()
'On Error Resume Next
Dim Btn As Integer, CurHwnd As Integer, T As String
Dim Length As Integer, x As Integer, y As Integer
' Trying to find the handle of the View
' Code Button so that
' by clicking this program's button, we
' can see the code
' window for this form.
CurHwnd = GetDesktopWindow() 'Get Desktop handle
CurHwnd = GetWindow(CurHwnd, GW_Child) 'Find Child Windows of Desktop
Do
If CurHwnd = 0 Then Exit Do 'No (more) matches found
' Find out how Integer the text in this win
' dow is
Length = GetWindowTextLength(CurHwnd)
T = Space$(Length + 1) 'Allocate buffer space
Length = GetWindowText(CurHwnd, T, Length + 1)
If InStr(UCase$(T), "PROJECT") Then
' The word "Project" was found in this W
' indow's text
' so this is likely VB's "Project" windo
' w
CurHwnd = GetWindow(CurHwnd, GW_Child)
' Looking now for the Project Window's c
' hild windows
Do
If CurHwnd = 0 Then Exit Sub 'No (more) matches found
' Find out how Integer the text in this win
' dow is
Length = GetWindowTextLength(CurHwnd)
T = Space$(Length + 1) 'Allocate buffer space
Length = GetWindowText(CurHwnd, T, Length + 1)
If InStr(UCase$(T), "VIEW CODE") Then
' This is the handle we want
Click CurHwnd 'Click the View Code Button
Exit Sub 'Exit the Sub
End If
CurHwnd = GetWindow(CurHwnd, GW_HWNDNEXT) 'Keep looking
Loop
End If
CurHwnd = GetWindow(CurHwnd, GW_HWNDNEXT) 'Keep looking
Loop
End Sub
Module Code:
code: Global Const WM_COMMAND = &H111
Global Const GW_Child = 5
Global Const GW_HWNDFIRST = 0
Global Const GW_HWNDLAST = 1
Global Const GW_HWNDNEXT = 2
Global Const GW_HWNDPREV = 3
Global Const GW_OWNER = 4
Declare Function GetDesktopWindow Lib "User32" () As Integer
Declare Function GetParent Lib "User32" (ByVal hWnd As Integer) As Integer
Declare Function GetWindow Lib "User32" (ByVal hWnd%, ByVal wCmd%) As Integer
Declare Function GetWindowText Lib "User32" (ByVal hWnd%, ByVal lpString$, ByVal nMaxCount%) As Integer
Declare Function GetWindowTextLength Lib "User32" (ByVal hWnd As Integer) As Integer
Declare Function GetWindowWord Lib "User32" (ByVal hWnd As Integer, ByVal nIndex As Integer) As Integer
Declare Function SendMessageByNum& Lib "User32" Alias "SendMessage" (ByVal hWnd%, ByVal wMsg%, ByVal wparam%, ByVal lparam&)
I get this error when I run the code
|
|
11-27-2005 05:05 AM |
|
|
Mnjul
forum super mod
plz wub me
Posts: 5396 Reputation: 58
– / /
Joined: Nov 2002
Status: Away
|
RE: Visual Basic Code Help
Since GetWindowTextLength has something to do with Strings, it may have two versions (GetWindowTextLengthA and GetWindowTextLengthW) in User32. In fact, it does. This is what VB's API Viewer gives me:
code: Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Try it and report if it doesn't work
By the way, you also need to add "Alias GetWindowTextA" for GetWindowText, otherwise VB will complain about it too.
Another by the way is that you should use Long to store hWnd values in VB (I mean VB6), I guess...
This post was edited on 11-27-2005 at 05:29 AM by Mnjul.
|
|
11-27-2005 05:15 AM |
|
|
DJeX
Veteran Member
Posts: 1138 Reputation: 11
– / / –
Joined: Jul 2003
|
O.P. RE: Visual Basic Code Help
Ok I did what you said and now I get this error
My Code With Changes:
code: Private Sub Click(Btnhwnd As Long)
Dim ChildID As Long
Dim Nul As Long
Const GWW_ID = (-12)
ChildID = GetWindowWord(Btnhwnd, GWW_ID)
Nul = SendMessageByNum(GetParent(Btnhwnd), WM_COMMAND, ChildID, ByVal CLng(Btnhwnd))
End Sub
Private Sub Command1_Click()
'On Error Resume Next
Dim Btn As Long, CurHwnd As Long, T As String
Dim Length As Long, x As Long, y As Long
' Trying to find the handle of the View
' Code Button so that
' by clicking this program's button, we
' can see the code
' window for this form.
CurHwnd = GetDesktopWindow() 'Get Desktop handle
CurHwnd = GetWindow(CurHwnd, GW_Child) 'Find Child Windows of Desktop
Do
If CurHwnd = 0 Then Exit Do 'No (more) matches found
' Find out how Long the text in this win
' dow is
Length = GetWindowTextLength(CurHwnd)
T = Space$(Length + 1) 'Allocate buffer space
Length = GetWindowText(CurHwnd, T, Length + 1)
If InStr(UCase$(T), "PROJECT") Then
' The word "Project" was found in this W
' indow's text
' so this is likely VB's "Project" windo
' w
CurHwnd = GetWindow(CurHwnd, GW_Child)
' Looking now for the Project Window's c
' hild windows
Do
If CurHwnd = 0 Then Exit Sub 'No (more) matches found
' Find out how Long the text in this win
' dow is
Length = GetWindowTextLength(CurHwnd)
T = Space$(Length + 1) 'Allocate buffer space
Length = GetWindowText(CurHwnd, T, Length + 1)
If InStr(UCase$(T), "VIEW CODE") Then
' This is the handle we want
Click CurHwnd 'Click the View Code Button
Exit Sub 'Exit the Sub
End If
CurHwnd = GetWindow(CurHwnd, GW_HWNDNEXT) 'Keep looking
Loop
End If
CurHwnd = GetWindow(CurHwnd, GW_HWNDNEXT) 'Keep looking
Loop
End Sub
Module
code: Global Const WM_COMMAND = &H111
Global Const GW_Child = 5
Global Const GW_HWNDFIRST = 0
Global Const GW_HWNDLAST = 1
Global Const GW_HWNDNEXT = 2
Global Const GW_HWNDPREV = 3
Global Const GW_OWNER = 4
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd%, ByVal wCmd%) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd%, ByVal lpString$, ByVal nMaxCount%) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Integer, ByVal nIndex As Integer) As Long
Declare Function SendMessageByNum& Lib "user32" Alias "SendMessage" (ByVal hwnd%, ByVal wMsg%, ByVal wparam%, ByVal lparam&)
|
|
11-27-2005 05:38 AM |
|
|
Mnjul
forum super mod
plz wub me
Posts: 5396 Reputation: 58
– / /
Joined: Nov 2002
Status: Away
|
RE: Visual Basic Code Help
quote: Originally posted by Mnjul
Another by the way is that you should use Long to store hWnd values in VB (I mean VB6), I guess...
Try that
By the way, where did you get these codes from?
|
|
11-27-2005 05:56 AM |
|
|
DJeX
Veteran Member
Posts: 1138 Reputation: 11
– / / –
Joined: Jul 2003
|
O.P. RE: Visual Basic Code Help
Ok I did try it and still the same error. And I got the code from www.planetsourcecode.com
|
|
11-27-2005 07:15 AM |
|
|
Mike
Elite Member
Meet the Spam Family!
Posts: 2795 Reputation: 48
32 / /
Joined: Mar 2003
|
RE: Visual Basic Code Help
Try pasting this into your module, removing the previous code:
code: Option Explicit
Global Const WM_COMMAND = &H111
Global Const GW_Child = 5
Global Const GW_HWNDFIRST = 0
Global Const GW_HWNDLAST = 1
Global Const GW_HWNDNEXT = 2
Global Const GW_HWNDPREV = 3
Global Const GW_OWNER = 4
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetParent Lib "user32" (ByVal hwnd As Integer) As Long
Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Declare Function GetWindowWord Lib "user32" (ByVal hwnd As Integer, ByVal nIndex As Integer) As Long
Declare Function SendMessageByNum& Lib "user32" Alias "SendMessage" (ByVal hwnd%, ByVal wMsg%, ByVal wparam%, ByVal lparam&)
I dont get any overflow errors anymore...
|
|
11-27-2005 08:07 AM |
|
|
IKillThings
New Member
Posts: 2
Joined: Nov 2005
|
RE: Visual Basic Code Help
Change 2 a double?
|
|
11-27-2005 08:13 AM |
|
|
Mike
Elite Member
Meet the Spam Family!
Posts: 2795 Reputation: 48
32 / /
Joined: Mar 2003
|
RE: Visual Basic Code Help
quote: Originally posted by IKillThings
Change 2 a double?
For which line of code are you talking?
|
|
11-27-2005 08:22 AM |
|
|
CookieRevised
Elite Member
Posts: 15517 Reputation: 173
– / /
Joined: Jul 2003
Status: Away
|
RE: Visual Basic Code Help
DJeX,
In future, can you say what purpose this code has? Or at least link to the exact source from where you got it? It's a bitch to find out what a program does by only looking at the (buggy) code.
Anyways...
- What's the use of a seperate module with just simple declarations?
- What's the use of global variables if you only have 1 form?
- Always use "Option Explicit" in ALL modules, forms, etc at the beginning of your code.
In short: the code you've downloaded is made by someone who don't know how to properly code stuff, and has all the markings of copy/pasting... The code is altered from the original by some stupid kid (he did a "replace all" of all "Long"'s with "Integer"'s, proof: see the many Integer declarations and especially the comment: ' Find out how Integer the text in this window is'). Not to mention the original code was most likely made for VB3.0 or 4.0...
After all these years, that crappy code has spread all over the place (as such code usually do):
http://www.vbexplorer.com/VBExplorer/tips/src09.htm
http://www.andreavb.com/forum/viewtopic.php?TopicID=1014
http://spazioinwind.libero.it/vbprogzone/articles/art006.html
etc...
etc...
Instead of copy/pasting, try to understand what the code does (or tries to do) and rewrite the whole thing from scratch.
quote: Originally posted by IKillThings
Change 2 a double?
If you get overflow problems, changing to Double is not the proper thing to do, rather fix the bug itself instead of applying crappy workarounds (which wouldn't work anyways).
This is the proper code:
- Note that everything is put in the form, you don't need any module.
- Note that the code works, but if you're using VB6, you will most likely not see anything happening as VB6 doesn't have a button with "View Code" in it's main parent window.
In other words, this code of yours will not work afterall, but the method behind it will work. Thus the code needs serious adaptations or you need to be specific in what you want.
code: 'C' Always use "Option Explicit" to avoid undeclared variable errors
Option Explicit
Private Declare Function GetDesktopWindow Lib "User32" () As Long
Private Declare Function GetParent Lib "User32" (ByVal hWnd As Long) As Long
Private Declare Function GetWindow Lib "User32" (ByVal hWnd As Long, ByVal uCmd As Long) As Long
Private Declare Function GetWindowText Lib "User32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowTextLength Lib "User32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
'C' Note that the GetWindowWord() function is obsolete and should be replaced
'C' with GetWindowLong() function. See the MSDN Library for more details.
'C' EDIT: I've replaced it for you...
Private Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SendMessageByNum Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wparam As Long, ByVal lparam As Long) As Long
Private Const WM_COMMAND As Long = &H111
Private Const GW_HWNDFIRST As Long = 0
Private Const GW_HWNDLAST As Long = 1
Private Const GW_HWNDNEXT As Long = 2
Private Const GW_HWNDPREV As Long = 3
Private Const GW_OWNER As Long = 4
Private Const GW_CHILD As Long = 5
Private Const GWL_ID As Long = -12
Private Sub Click(BtnhWnd As Long)
'C' This will send the command id associated with the button to the parent
'C' of the button. In other words: it 'clicks' that button...
SendMessageByNum GetParent(BtnhWnd), WM_COMMAND, GetWindowLong(BtnhWnd, GWL_ID), BtnhWnd
MsgBox "done"
End Sub
Private Sub Command1_Click()
'C' The method used here to find the handle to a button works (almost),
'C' but can greatly be optimized using the proper API's.
'C'
'C' Using the method depict here, has his flaws. eg: if a (child) window
'C' is moved in the Z-order (which can happen all the time!!) it will not
'C' be properly enumerated and even some errors may occur.
'C'
'C' To properly enumerate all (child) windows use the proper API's instead
'C' of a half-baked method. eg: use the EnumWindows() and especially
'C' EnumChildWindows() functions. See the MSDN Library for more details.
Dim T As String
Dim Length As Long
Dim CurHwnd As Long
' Trying to find the handle of the View Code Button so that
' by clicking this program's button, we can see the code
' window for this form.
CurHwnd = GetDesktopWindow() 'Get Desktop handle
CurHwnd = GetWindow(CurHwnd, GW_CHILD) 'Find Child Windows of Desktop
Do Until CurHwnd = 0
' Find out how long the text in this window is
Length = GetWindowTextLength(CurHwnd) 'Get buffer length
T = Space$(Length) 'Allocate buffer space
GetWindowText CurHwnd, T, Length + 1 'Fill buffer
If InStr(UCase$(T), "PROJECT") Then
' The word "Project" was found in this Window's text
' so this is likely VB's "Project" window
' Looking now for the Project Window's child windows/controls
CurHwnd = GetWindow(CurHwnd, GW_CHILD)
Do Until CurHwnd = 0
' Find out how long the text in this window is
Length = GetWindowTextLength(CurHwnd)
T = Space$(Length)
GetWindowText CurHwnd, T, Length + 1
If InStr(UCase$(T), "VIEW CODE") Then
' This is the handle we want
Click CurHwnd 'Click the View Code Button
Exit Sub 'Exit the Sub
End If
CurHwnd = GetWindow(CurHwnd, GW_HWNDNEXT) 'Keep looking
Loop
End If
CurHwnd = GetWindow(CurHwnd, GW_HWNDNEXT) 'Keep looking
Loop
End Sub
Lines starting with 'C' are my added comments
Thus, the important thing about this is to understand the method behind this and write your own code based on that (and using the proper API's instead). The important thing is not to get this code to work (as it is only a dodgy example anyways)...
If you want to press on a button from another program automatically:
- You need to find the control ID of that button
- Send that control ID to the parent of the button
To do this:
- You need to enumerate all child windows of the parent window
- In most cases (but which is not done in this code) you need to enumerate all child windows of all the child windows also, etc...
- Find the button on the current (child) window you're at (the method used here is to search for its caption text, but there are many methods and ways and all depend on what you exactly want)
But, judging from your questions, you might to program/try much easier and basic stuff first before attempting something like this as you don't seem to understand how such stuff works (no offense though! everything needs to be learned and we all need to start somewhere. But starting with something like this, without understanding it isn't the proper way of learning something... "don't run, if you can't walk")
----------------
As a "bonus", I've added a module to this post which shows a better way of doing this, but the principle stays the same. All you need is have MSN Messenger 6.x English running an execute the module (no form needed). It will click the "Add a contact" button.
Attachment: Module1.zip (1.13 KB)
This file has been downloaded 141 time(s).
This post was edited on 11-27-2005 at 09:22 PM by CookieRevised.
.-= A 'frrrrrrrituurrr' for Wacky =-.
|
|
11-27-2005 10:21 AM |
|
|
DJeX
Veteran Member
Posts: 1138 Reputation: 11
– / / –
Joined: Jul 2003
|
O.P. RE: Visual Basic Code Help
Thank you CookieRevised for your reply. I tryed your module but it didn't work.
I tryed the code you posted and that aswell did not work. This is what I did:
I made a test program called Notepad with a button called Test on it. I replaced the correct words in the code above (PROJECT and VIEW CODE) with NOTEPAD and TEST. Then I ran my test program then run the code above but still didn't work.
I just need a simple code that will find a button on another program that is running and press it. Like for example how that Hit Man Pro anti-spyware program works, it controls the other programs that it downloads.
I guess like you said I should learn it but I dont know where to start. I'll study your code some more.
|
|
11-27-2005 07:41 PM |
|
|
Pages: (2):
« First
[ 1 ]
2
»
Last »
|
|
|