[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1563
  • Last Modified:

FindWindow() in VB.net

Hello everyone,

I am working on one program in which i need to open another application and do some functionality with that application. I have come to know that we can do it with findwindow(). but I can just open the first window and then I am not able to find the way to press button to go to next window.

How can we find out the Ipname of the window of the application and I also want to find how can we locate the button and press it using function.

0
dualtech
Asked:
dualtech
  • 11
  • 6
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can use FindWindowEx() to get handles to CHILD windows within the main window.  If a window is nested several layers deep then you have to get each parent window in order.  Use tools such as Spy++ (comes with some versions of Visual Studio) or the free WinSpector  to determine the hierarchical relationship of the controls in the target app.

Once you have a handle to the button you can use the SendMessage() API with the BM_CLICK message to click it.
0
 
dualtechAuthor Commented:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(408, 288)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(104, 24)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(520, 318)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim hwndLogin As Integer
        Dim hwndBranch As Integer
        Dim hwndLoginUser As Integer
        Dim hwndLoginPW As Integer
        Dim hwndLoginOK As Integer
        Dim hwndBranchOK As Integer
        Dim hwndCMS As Integer

        Dim MenuHandle As Integer
        Dim SubMenuHandle As Integer
        Dim MenuID As Integer
        Dim lret As Long

        Shell("H:\Haw\cms.exe /cfg:H:\hawk.ini", AppWinStyle.NormalFocus)

        hwndLogin = FindWindow("#32770", "Client Management System Login")

        Do Until hwndLogin
            Application.DoEvents()
            hwndLogin = FindWindow("#32770", "Client Management System Login")
        Loop

        hwndLoginUser = FindWindowEx(hwndLogin, Nothing, "Edit", Nothing)
        SendMessageByString(hwndLoginUser, WM_SETTEXT, 0, "RVG")
        hwndLoginPW = FindWindowEx(hwndLogin, hwndLoginUser, "Edit", Nothing)
        SendMessageByString(hwndLoginPW, WM_SETTEXT, 0, "tech2")
        hwndLoginOK = FindWindowEx(hwndLogin, Nothing, "Button", Nothing)
        Call PostMessage(hwndLoginOK, WM_LBUTTONDOWN, 0&, 0&)
        Call PostMessage(hwndLoginOK, WM_LBUTTONUP, 0&, 0&)


        hwndBranch = FindWindow(Nothing, "Choose The Office You are in Today")
        Do Until hwndBranch
            Application.DoEvents()
            hwndBranch = FindWindow(Nothing, "Choose The Office You are in Today")
        Loop


        hwndBranchOK = FindWindowEx(hwndBranch, Nothing, "Button", Nothing)
        Call PostMessage(hwndBranchOK, WM_LBUTTONDOWN, 0&, 0&)
        Call PostMessage(hwndBranchOK, WM_LBUTTONUP, 0&, 0&)


        hwndCMS = FindWindow(Nothing, "Hawthrone - [Start Page]")

        Do Until hwndCMS
            Application.DoEvents()
            hwndCMS = FindWindow(Nothing, "Hawthrone - [Start Page]")
        Loop

        MenuHandle = GetMenu(hwndCMS)

        SubMenuHandle = GetSubMenu(MenuHandle, 2)

        MenuID = GetMenuItemID(SubMenuHandle, 4)

        lret = PostMessage(hwndCMS, WM_COMMAND, MenuID, 0&)
    End Sub
End Class

This is the code I have made till now.. This program was made to work in the previous version of the application . now in new version they have made one new window between login window and  main form. I am not able to access that window. Please let me know if there is some thing wrong with the code.
I want to press only OK button in that window which is used to select the branch.
0
 
dualtechAuthor Commented:
I can get through the login window using this code. I am stuck up in the next window after login window.
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
dualtechAuthor Commented:
Now with the same code I just change the name of the middle window some spelling changes and if I put a break point at the post message point then It works and get me into next menu. but otherwise its not letting me in there.

I am not able to understand what am I doing wrong.

Please somebody help.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I can't see anything overtly wrong with the code.  It's next to impossible to say why it isn't working, though, without the target app sitting in front of me.  =\

0
 
dualtechAuthor Commented:
I can get into the target app in the debugging mode but if I am using the normal mode then its not working.
I am very much confused .

Please let me know how can I make it work.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure why it would work in the IDE and not as a "release" EXE.  Do you have any Try/Catch blocks that you can turn off?  Perhaps there is an error occuring that you haven't anticipated...
0
 
dualtechAuthor Commented:
I used sleep function and now I can open both the windows. But now the Problem is I cant access the menu. Can somebody please help me with this.

The code is same I have just added two sleep function in between the windows.

Please some one help me out with the menu access.
0
 
dualtechAuthor Commented:
I have wrote copied all the code which i wrote. I have not used try and catch block at any place. I want to automatically generate report every day. so Its in the 3rd menu at 5th position the position at above position is wrong I have changed it. But still I am getting values for the menu id and submenu id but they are not being accessed.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
So are you doing checks like?

    MenuHandle = GetMenu(hwndCMS)
    If MenuHandle <> 0 Then
        ' ...move on  to submenu...
    Else
        MessageBox.Show("Main Menu NOT Found")
    End If
0
 
dualtechAuthor Commented:
I have copied all the code which i have written. I have not used try and catch block at any place. I want to automatically generate report every day. so Its at the 3rd menu at 5th position in the sub menu I have written wrong position in above code. I am getting values for the menu id and submenu id but they are not being accessed.

Is there is any thing wrong with the command which I have written for the menu access
0
 
dualtechAuthor Commented:
hwndCMS = FindWindow(Nothing, "Hawthorne - [Start Page]")

        Do Until hwndCMS
            Application.DoEvents()
            hwndCMS = FindWindow(Nothing, "Hawthorne - [Start Page]")
        Loop

        MenuHandle = GetMenu(hwndCMS)

        SubMenuHandle = GetSubMenu(MenuHandle, 3)

        MenuID = GetMenuItemID(SubMenuHandle, 5)

        lret = PostMessage(hwndCMS, WM_COMMAND, MenuID, 0&)
0
 
dualtechAuthor Commented:
Above is the code which I am using to access the menus. I am getting values for MenuHandle, and SubMenuHandle. but at the point of MenuID it is giving me 0 for MenuID.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I assume that the "sub menu" has SIX items on it?  You're asking for item #5 and GetMenuItemID is zero based.

Other than that, it's possible that you've declared the APIs incorrectly?
0
 
dualtechAuthor Commented:
I didnt get what exectly do you want to say. Could you please tell me if I am making any mistakes in writing any syntex.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
As I said before, I don't see anything wrong with your POSTED code.

You didn't post the API declarations so it's possible something is wrong with them.

Other than that, this has become a troubleshooting problem with respect to an app that we (the experts) CANNOT see.  Therefore we don't know if your LOGIC with respect to this app is correct.

Can you not see that correct SYNTAX means nothing if you're telling the computer to do something that doesn't make sense for your particular situation?

It's next to impossible for us to "fix" your code without being able to "play" with the target app...  =\
0
 
dualtechAuthor Commented:
As I am not able to find appropriate answer from any one I would like to close this question and get my points refund back to me.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now