Trouble with button pushes outside of app - vb.net

Using Spy++ I've been trying to control form buttons on an external program with my own program..
The picture shows what control I am attempting to mimic and here's my following code below...
Dim hWnd As IntPtr = FindWindow(vbNullString, ListView4.SelectedItems(0).SubItems(3).Text)
If hWnd.Equals(IntPtr.Zero) Then
Return
End If
Dim hWndButton As IntPtr = _
FindWindowEx(hWnd, IntPtr.Zero, "Button", "Load Settings")
If hWndButton.Equals(IntPtr.Zero) Then
Return
End If
However nothing happens when I run the code.. one possibility is the window name I'm grabbing is wrong, but if that's not the case is my code correct?

Well, as Alex pointed out, I wasn't actually initiating the button press.. now that he pointed that out I was able to fix my code.. Here's what I now use:
Private Declare Auto Function FindWindow Lib "user32.dll" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32.dll" ( _
ByVal hwndParent As IntPtr, _
ByVal hwndChildAfter As IntPtr, _
ByVal lpszClass As String, _
ByVal lpszWindow As String _
) As IntPtr
Declare Auto Function SendMessage Lib "user32" (ByVal hwnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Private Const BM_CLICK = &HF5
Dim hwndParent As Long = FindWindow(vbNullString, ListView4.SelectedItems(0).SubItems(3).Text)
Debug.Print("findwindow: " & hwndParent)
Dim hwndButton As Long = FindWindowEx(hwndParent, IntPtr.Zero, "Button", "Save as")
Debug.Print("OK: " & hwndButton)
hwndButton = SendMessage(hwndButton, BM_CLICK, 0, 0)
Debug.Print("Clicked: " & hwndButton)

Related

How can I convert vba macro with findwindow api into google app script?

I'd like to convert VBA macro to Google Apps script but I think this particular one is impossible to convert as I am totally lost where to begin with this. I couldn't find anyway to do what "findwindow" does on APP script. Does anyone have any idea how to tackle this or should I give up on this?
Plus, is there anyone played with the phone companion? As they don't provide any API, the only way to use the mobile companion to send messages with google spread would be using findwindow, which shows the same problem. Any insight on this would be highly appreciated. Thanks.
Option Explicit
#If VBA7 Then
' https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowa
Private Declare PtrSafe Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
' https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowexa
Public Declare PtrSafe Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" _
(ByVal hwndParent As Long, _
ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, _
ByVal lpszWindow As String) As Long
' https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessagea
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
'https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postmessagea
Private Declare PtrSafe Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
Private Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
#Else
' https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowa
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
' https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowexa
Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" _
(ByVal hwndParent As Long, _
ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, _
ByVal lpszWindow As String) As Long
' https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendmessagea
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
'https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-postmessagea
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
>Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
#End If
Dim Handle, HandleEx As Long
Private Const WM_SETTEXT = &HC: Private Const WM_KEYDOWN = &H100
Private Const VK_RETURN = &HD: Private Const VK_ESCAPE = &H1B
Private Const WM_CLOSE = &H10: Private Const GW_HWNDNEXT = &H2
Sub Send_Kakao(Target As Range, Msg As Range)
Dim SendTo$: SendTo = Target.Value
Dim Message$: Message = Msg.Value
Dim hwnd_KakaoTalk As Long: Dim hwnd_RichEdit As Long
Call Call_ChatRoom(Target)
DoEvents
Sleep 1000
hwnd_KakaoTalk = FindWindow(vbNullString, SendTo)
hwnd_RichEdit = FindWindowEx(hwnd_KakaoTalk, 0, "RichEdit50W", vbNullString)
If hwnd_RichEdit = 0 Then MsgBox SendTo & "This chatroom is not opened.": Exit Sub
Call SendMessage(hwnd_RichEdit, WM_SETTEXT, 0, ByVal Message)
Call PostMessage(hwnd_RichEdit, WM_KEYDOWN, VK_RETURN, 0)
End Sub
Private Sub Call_ChatRoom(Target As Range)
Dim ChatRoom$: ChatRoom = Target.Value
Dim rtnV As Long
Handle = FindWindow("EVA_Window_Dblclk", vbNullString) 'Saave Window Handle info of Kakao on Handle
'//Print error when cannot be located
If Handle = 0 Then
MsgBox "Open kakao first.", 16, "Error"
Else
HandleEx = FindWindowEx(Handle, 0, "EVA_ChildWindow", vbNullString) 'children Window1
HandleEx = FindWindowEx(HandleEx, 0, "EVA_Window", vbNullString) 'children Window2
HandleEx = FindWindowEx(HandleEx, 0, "Edit", vbNullString) 'children Window3
Call SendMessage(HandleEx, WM_SETTEXT, 0, ByVal ChatRoom)
DoEvents
Sleep 1000
Call PostMessage(HandleEx, WM_KEYDOWN, VK_RETURN, 0)
End If
End Sub

Can some one explain what this is actually doing?

I have been searching for a way to close windows explorer using vba, and i have found something that works. However i actually have no idea what it is actually doing, or what any of it means. Could someone please explain what is happening below?
Private Const CLOSE_WIN = &H10
Dim Hwnd As Long
Private Declare Function apiFindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassname As String, _
ByVal lpWindowName As String) _
As Long
Private Declare Function apiPostMessage _
Lib "user32" Alias "PostMessageA" _
(ByVal Hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) _
As Long
Hwnd = apiFindWindow("CabinetWClass", vbNullString)
Dim retval As Long
If (Hwnd) Then
retval = apiPostMessage(Hwnd, CLOSE_WIN, 0, ByVal 0&)
End If
Thank You

How to change combo box selected value of another application? Visual Basic

I know how to click a button of another application using the codes below. But now I need to know how to change the selected value of a combo box.
'Declaration
Private Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Declare Auto Function PostMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
Private Const BM_CLICK = &HF5
'Usage
Dim ButtonHandle As IntPtr
Dim MainWindowHandle As IntPtr
MainWindowHandle = FindWindow(FormClass, FormCaption)
ButtonHandle = FindWindowEx(MainWindowHandle, IntPtr.Zero, TargetClass, TargetCaption)
If ButtonHandle <> 0 Then
PostMessage(ButtonHandle, BM_CLICK, 0, 0)
End If
All I need to do is to change the combobox selected value of a different application from its default value of Off to a value of On. Any help is very much appreciated. Thank You.
Finally I now know how to do this. The code that I used is shown below.
'Declaration
Private Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Auto Function FindWindowEx Lib "user32.dll" (ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Declare Auto Function PostMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
Private Const BM_CLICK = &HF5
Private Const CB_SETCURSEL = &H14E
'Usage
Dim MainWindowHandle As IntPtr
Dim ChildAfter As IntPtr
Dim ComboBoxHandle As IntPtr
'Get the Handle
MainWindowHandle = FindWindow(FormClass, FormCaption)
'Get the ChildAfter of the Combo Box
ChildAfter = FindWindowEx(MainWindowHandle, IntPtr.Zero, ChildClass, ChildCaption)
'Get the handle of the combobox dropdown
ComboBoxHandle = FindWindowEx(MainWindowHandle, ChildAfter, "ComboBox", vbNullString)
'Select combo box index(1)
PostMessage(ComboBoxHandle, CB_SETCURSEL, 1, 0)
From the MSDN documentation of FindWindowEx:
hwndChildAfter [in, optional]
Type: HWND
A handle to a child window. The search begins with the next child window in the Z order. The child window must be a direct child window of hwndParent, not just a descendant window.
If hwndChildAfter is NULL, the search begins with the first child window of hwndParent.
#RemyLebeau Thank you so much for helping me do this.

Visual Basic Excel Messages Box

Hi I want to ask if is it possible to have MsgBox without any buttons only with my message? Maybe is another way to display messages to user without using MsgBox?
I usually do not answer question which do not show enough research but this is way beyond a normal user.
Is it possible to have MsgBox without any buttons only with my message?
Msgbox doesn't give you an option to hide it. But we can bypass by subclassing the Excel Application and the Message Box.
Is another way to display messages to user without using MsgBox?
Yes you have two alternatives
Use a customized userform OR
Subclassing as I have shown below
ScreenShot
Code
Paste this code in a module and run the procedure Sample
Option Explicit
Private Declare Function SetWindowsHookEx Lib "user32" _
Alias "SetWindowsHookExA" (ByVal idHook As Long, _
ByVal lpfn As Long, ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, ByVal ncode As Long, _
ByVal WParam As Long, lparam As Any) As Long
Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
ByVal hwndParent As Long, ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, ByVal lpszCaption As String) As Long
Private Declare Function ShowWindow Lib "user32" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function EnableWindow Lib "user32" _
(ByVal hwnd As Long, ByVal fEnable As Long) As Long
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 SetWindowLong Lib "user32" _
Alias "SetWindowLongA" (ByVal hwnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" _
Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function CallWindowProc Lib "user32" _
Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, ByVal MSG As Long, ByVal WParam As Long, _
ByVal lparam As Long) As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private hwndXLApp As Long
Private hwndMsgBox As Long
Private hwndMsgBoxBtn As Long
Private HookIt As Long
Private OldAppWinProc As Long
Private OldMBoxWinProc As Long
Private Const WH_CBT As Long = 5
Private Const HCBT_CREATEWND As Long = 3
Private Const GWL_STYLE As Long = -16
Private Const DS_NOIDLEMSG As Long = &H100&
Private Const GWL_WNDPROC As Long = (-4)
Private Const WM_ENTERIDLE As Long = &H121
Private Const WM_COMMAND As Long = &H111
Private Const WM_NCDESTROY As Long = &H82
Sub Sample()
hwndXLApp = FindWindow("XLMAIN", Application.Caption)
'~> Setup the hook to catch creation of messagebox
HookIt = SetWindowsHookEx(WH_CBT, AddressOf HookProc, 0, GetCurrentThreadId)
MsgBox ("Look Mommy, My button is missing!!!")
End Sub
Private Function HookProc(ByVal idHook As Long, ByVal WParam As Long, ByVal lparam As Long) As Long
Dim strBuffer As String
Dim RetVal As Long, curStyle As Long, NewStyle As Long
'~~> Check if a window is being created
If idHook = HCBT_CREATEWND Then
strBuffer = Space(256)
'~~> Check if it is a MSGBOX
RetVal = GetClassName(WParam, strBuffer, 256)
If Left(strBuffer, RetVal) = "#32770" Then
'~~> Handle of Msgbox
hwndMsgBox = WParam
'~~> We make the Msgbox Modeless so that we can use
'~~> ShowWindow API to hide the button
curStyle = GetWindowLong(WParam, GWL_STYLE)
NewStyle = curStyle And Not DS_NOIDLEMSG
SetWindowLong WParam, GWL_STYLE, NewStyle
'~~> Subclass Excel app to catch the WM_ENTERIDLE message and
OldAppWinProc = SetWindowLong(hwndXLApp, GWL_WNDPROC, AddressOf NewAppWindowProc)
'~~> Sub class the msgbox to catch the WM_NCDESTROY message to cleanup
OldMBoxWinProc = SetWindowLong(WParam, GWL_WNDPROC, AddressOf NewMsgBxWindowProc)
'~~> UnHook
UnhookWindowsHookEx HookIt
End If
End If
'~~> Call next hook
HookProc = CallNextHookEx(HookIt, idHook, ByVal WParam, ByVal lparam)
End Function
Private Function NewAppWindowProc(ByVal hwnd As Long, ByVal MSG _
As Long, ByVal WParam As Long, ByVal lparam As Long) As Long
On Error Resume Next
Select Case MSG
Case WM_ENTERIDLE
EnableWindow hwnd, 1
hwndMsgBoxBtn = FindWindowEx(hwndMsgBox, ByVal 0&, "Button", vbNullString)
ShowWindow hwndMsgBoxBtn, 0
'~~> Un SubClass Excel
SetWindowLong hwnd, GWL_WNDPROC, OldAppWinProc
End Select
'~~> Pass Intercepted Messages To The Original WinProc
NewAppWindowProc = CallWindowProc(OldAppWinProc, hwnd, MSG, WParam, lparam)
End Function
Private Function NewMsgBxWindowProc(ByVal hwnd As Long, ByVal MSG _
As Long, ByVal WParam As Long, ByVal lparam As Long) As Long
On Error Resume Next
Select Case MSG
Case WM_NCDESTROY, WM_COMMAND
SetWindowLong hwnd, GWL_WNDPROC, OldMBoxWinProc
End Select
NewMsgBxWindowProc = CallWindowProc(OldMBoxWinProc, hwnd, MSG, WParam, lparam)
End Function

VB.net SendMessage waiting problem

I am using the following code to click a button for displaying another form to click on:
Dim hwnd As Integer = FindWindow(vbNullString, "Virtual CDRom Control Panel")
Dim x As Integer = FindWindowEx(hwnd, 0, vbNullString, "Driver Control ...")
SendMessage(x, BM_CLICK, 0&, 0&)
Thread.Sleep(200)
hwnd = FindWindow(vbNullString, "Virtual CD-ROM Driver Control")
Debug.Print(hwnd)
Problems is that when it gets to
SendMessage(x, BM_CLICK, 0&, 0&)
to click the button, it stops the code there until i exit out of the box that pops up. I want to be able to continue without having to exit the box since the next line
hwnd = FindWindow(vbNullString, "Virtual CD-ROM Driver Control")
finds the pop up window and will click on a button inside that box.
Any help would be great! :o)
David
SOLVED
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWndParent As IntPtr, ByVal hWndChildAfter As Integer, ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As IntPtr
Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As IntPtr) As IntPtr
Dim hwnd As IntPtr = FindWindow(vbNullString, "Virtual CDRom Control Panel")
Dim x As IntPtr = FindWindowEx(hwnd, 0, vbNullString, "Driver Control ...")
PostMessage(x, BM_CLICK, 0&, 0&)
Thread.Sleep(200)
hwnd = FindWindow(vbNullString, "Virtual CD-ROM Driver Control")
Debug.Print(hwnd)
Try changing SendMessage to PostMessage.
Also please note all HWNDs must be declared as IntPtr.
Also wParam and lParam for SendMessage and PostMessage are IntPtrs.
This will make you code compatible with x64 environment.