Windows Handle

HJB417 said:
The former (until the window is destroyed).

Damn, well he is my current thought process, I haveny been able to test this but. Im thinking that if I can find the program in the task manager get its PID number I should be able to find the window and any child windows. Since Im not at the computer with VB.Net on it I cant try anything but I dont see why that wouldnt be possible, Ill be doing some research on it. I know i reads something about get process ID or something of that nature. Ill hunt through the MNSD site tonight. Plaease feel free to comment on my thought process above and it you think it would work or if you have a better direction to go.

Thanks again

ZeroEffect
 
Ok well here is what I found and I think if I incorporate this into the other code itll work just fine.

Code:
Imports System.Diagnostics

    Dim returnValue() As Diagnostics.Process

    Dim Handle As IntPtr

    returnValue = Process.GetProcessesByName("notepad")

    Handle = returnValue(0).Handle

Any thoughts about doing it this way?

Thanks

ZeroEffect
 
Opps I had to change this line.

Code:
Handle = returnValue(0).Handle

to

Handle = returnValue(0).MainWindowHandle

Again Thanks for you help

ZeroEffect
 
New Problem

Ok Eveything is great but, The window I need to grab is 1, a child window and 2, it has no caption. I have been lookinig into this,

Code:
C# Code

[DllImport("User32.dll")]
public static extern Boolean EnumChildWindows(int hWndParent,Delegate lpEnumFunc,int lParam);

but I cant seem to get it to work while trying to convert it to VB.NET

Code:
    <DllImport("user32.dll", SetLastError:=True)> _
    Shared Function EnumChildWindows(ByVal hWndParent As IntPtr, ByVal lpEnumFunc As ?Not Sure?, ByVal lParam As Integer) As IntPtr
    End Function

I have seen some examples but nothing it working for me. I am still reading up on it to learn more but any help is welcome.

Thanks

ZeroEffect
 
Will do here is my import code. Ill be trying the function shortly.

Code:
    <DllImport("user32.dll", SetLastError:=True)> _
    Shared Function GetClassNameA(ByVal hwnd As IntPtr, ByVal lpClassName As String, ByVal nMaxCount As Integer) As Integer
    End Function

Thanks Again

ZeroEffect
 
[VB]
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function GetClassName(ByVal hwnd As IntPtr, <MarshalAs(UnmanagedType.LPStr)> ByVal buf As StringBuilder, ByVal nMaxCount As Integer) As Integer
End Function


Public Function get_ClassName() As String
Dim builder1 As New StringBuilder(256)
Dim num1 As Integer = NativeMethods.GetClassName(Me.Handle, builder1, 256)
Return builder1.ToString(0, num1)
End Function
[/VB]
 
With the code above and this line,
Code:
            Dim sClassName As String 
            Dim r As Integer
            r = GetClassNameA(hwnd, sClassName, 100)

I was able to return the number 6 but Im not dure what to do with that ro if it is right.
according to information on the support website they had this.
Code:
vb6 code

Dim sClassName As String * 100

but .NET doesnt like that. So that is my next step. Any thoughts are welcome.

Thanks again

ZeroEffect
 
found the import

Code:
imports system.text

Here is the error I am getting too.

"System.Drawing.NativeMethods is not accessible in this context because it is private"

Im looking for it now.

Thanks

ZeroEffect
 
Removing "NativeMethods" from the code eliminated the problem and I am now returning the class name. now that I have the class name where am I to use it. I have tried placing it here,

Code:
 Dim PTools_hWnd As IntPtr = FindWindowExA(hwnd, 0, strWinClass, vbNullString)

but all this does is return a zero, Ill have to try some more things.

Here is my code so far

Code:
    Private Sub PT_Command(ByVal strCMD As String)
        Try
            Dim ret As Integer
            Dim Rec1 As Rectangle = New Rectangle
            Find Window

            Dim returnValue() As Diagnostics.Process

            returnValue = Process.GetProcessesByName(prgWindow)

            Dim hwnd As IntPtr = returnValue(0).MainWindowHandle

            strWinClass = get_ClassName(hwnd)

           Dim PTools_hWnd As IntPtr = FindWindowExA(hwnd, 0, strWinClass, vbNullString) finds child windows

            Get Left, Right, Top and Bottom of Form1
            Dim success As Boolean = GetWindowRect(hwnd, Rec1)
            Debug.WriteLine(String.Format("Rec1={0}", Rec1))

            If Not success Then
                Throw New Win32Exception
            End If

            GetWindowRect returning zero values
            Tried both hwnd & PTools_hWnd
            If UCase(strCMD) = "PLAY" Then
                MoveCursor((Rec1.Left + x1), (Rec1.Top + y1))
            ElseIf UCase(strCMD) = "STOP" Then
                MoveCursor((Rec1.Left + x2), (Rec1.Top + y2))
            End If

            If Not SetForegroundWindow(hwnd) Then
                Throw New Win32Exception
            End If

            If newPoint.Equals(Cursor.Position) Then
                mouse_event(2, 0, 0, 0, IntPtr.Zero)
                mouse_event(4, 0, 0, 0, IntPtr.Zero)
            Else
                SendKeys.Send(" ")
            End If

        Catch ex As Exception
            Dim tempData() As String
            Dim I As Integer

            SendKeys.Send(" ")

            tempData = Split(ex.ToString, vbCrLf)
            For I = LBound(tempData) To UBound(tempData)
                TextBox1.AppendText(Now & vbTab & tempData(I) & vbCrLf)
            Next

        End Try
    End Sub

    Private Sub MoveCursor(ByVal Fx As Integer, ByVal Fy As Integer)
        newPoint = New Point(Fx, Fy)
        Debug.WriteLine(String.Format("Old mouse position = {0}.", Cursor.Position))
        Debug.WriteLine(String.Format("Moving mouse to {0}.", newPoint))
        Cursor.Position = newPoint
    End Sub

    Public Function get_ClassName(ByVal intHandle As IntPtr) As String
        Dim builder1 As New StringBuilder(256)
        Dim num1 As Integer = GetClassName(intHandle, builder1, 256)
        Return builder1.ToString(0, num1)
    End Function

Thanks again for your help

ZeroEffect
 
I am Still trying to get the handle of the child window from class name. but I was able to get more info about the window using a freeware program called WinDowse I think it give me more info than spy++.

*** Window Information ***************************************

Text Playback 1 (1)
Process ID 0000032C
App instance 00400000
Handle 000105B8
Parent handle 000204AA
Control ID 00000000
Function 00000000
Menu handle 00000000
Coords in parent left:82, top:93, right:469, bottom:527
Coords in screen left:82, top:112, right:469, bottom:546
Window size width:387, height:434
Client area size width:379, height:407
Style 94CC0044
Extended style 00010100
ws_overlapped*| ws_popup*| ws_visible*| ws_clipsiblings*| ws_border*|
ws_dlgframe*| ws_sysmenu*| ws_thickframe*| ws_ex_windowedge*| ws_ex_left*|
ws_ex_ltrreading*| ws_ex_rightscrollbar*| ws_ex_controlparent


*** Class Information ****************************************

Name #32770
Function 77D6E54F
Icon 00000000
Small icon 00000000
Cursor 00010013
Bkg brush 00000000
Module handle 77D40000
Style 00004808
cs_dblclks*| cs_savebits*| cs_globalclass

This is from the test application I am using that I know has child windows that way I can move it to the primary application I want to use this on.

Thanks

ZeroEffect
 
Update

I have been playing with this for the last while and I cant seem to get it to return the correct window. It is returning a child window but not the one I want.

Code:
Private Declare Function GetWindow Lib "user32" Alias "GetWindow" _
(ByVal hwnd As IntPtr, ByVal wCmd As Integer) As IntPtr

    Private Const GW_CHILD = 5

 in a sub after the main window is found
Testtest = getChildWindow(hwnd)

the function that is called

    Function getChildWindow(ByVal hwnd As IntPtr) As IntPtr

        Dim cHandle As IntPtr

        getChildWindow = GetWindow(hwnd, GW_CHILD)

    End Function

More as events arise, any thoughts?

Thanks

ZeroEffect
 
HJB417 said:
whats the name of the program youre trying to send mouse clicks to?

For testing it is dadpro32.exe by enco it is a broadcast audio system. The one for the final product is ProTools LE (ProToolsLE.exe) an audio editing system. ProTools will only run if the hardware is attached to the computer so I am extremely limited on testing with that application. I want to fire Protools remotely. and this is how I can do it. The spacebar is the shortcut for playing and stopping the editor but if I turn off the switch and the audio has ended itll start to play again. So that is what I am trying to accomplish. Does this help you out?

Thanks

ZeroEffect
 
Heres code for EnumChildWindows. I converted it to vb using reflector, its not compilable as is under vb.

But the idea is, you need to declare a delegate for EnumChildWindows
to use. When you call EnumChildWindows, it will invoke the delegate each time it reads/finds a new child window. In the code below, it adds it to an ArrayList.
[VB]
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function EnumChildWindows(ByVal window As IntPtr, ByVal callback As EnumWindowsProc, ByVal i As Integer) As Boolean
End Function

<Serializable> _
Public Delegate Function EnumWindowsProc(ByVal handle As IntPtr, ByVal i As Integer) As Boolean

Private ReadOnly ChildWindows As ArrayList


Public Function GetChildWindows() As IntPtr()
Dim windowArray2 As IntPtr()
SyncLock Me.ChildWindows
NativeMethods.EnumChildWindows(Me.Handle, New EnumWindowsProc(AddressOf Me.EnumerateChildProc), 0)
Dim windowArray1 As IntPtr() = CType(Me.ChildWindows.ToArray(GetType(IntPtr)), IntPtr())
Me.ChildWindows.Clear
windowArray2 = windowArray1
End SyncLock
Return windowArray2
End Function


Private Function EnumerateChildProc(ByVal Handle As IntPtr, ByVal i As Integer) As Boolean
Me.ChildWindows.Add(Handle)
Return True
End Function
[/VB]
 
HJB417 said:
Code:
Private Function EnumerateChildProc(ByVal Handle As IntPtr, ByVal i As Integer) As Boolean
      Me.ChildWindows.Add(Handle)
      Return True
End Function
[/VB][/QUOTE]

I am working with the code now, I am running into this error when the code above is run.

System.NullReferenceException: Object reference not set to an instance of an object

So I am a hunting.

Side note I know that you have given me alot of help with this and I am slowly catching on. This is my first attempt at a program of this type so it is taking me a little bit longer to digest the code. Thank you for the help you have supplied. 

Thanks

ZeroEffect
 
Last edited by a moderator:
Console app where 5310006 is a handle of one of the instances of vs.net I had open.

[VB]
Imports System
Imports System.Runtime.InteropServices

Module Module1

<DllImport("user32.dll", SetLastError:=True)> _
Function EnumChildWindows(ByVal window As IntPtr, ByVal callback As EnumWindowsProc, ByVal i As Integer) As Boolean
End Function

<Serializable()> _
Delegate Function EnumWindowsProc(ByVal handle As IntPtr, ByVal i As Integer) As Boolean


Sub Main()
Dim parentHandle As IntPtr = New IntPtr(5310006)
EnumChildWindows(parentHandle, New EnumWindowsProc(AddressOf EnumWindowsCallBackFunction), 0)
End Sub

Function EnumWindowsCallBackFunction(ByVal handle As IntPtr, ByVal i As Integer) As Boolean
Console.WriteLine("EnumChildWindows Found {0}.", handle)
Return True
End Function

End Module
[/VB]
 
Progress

Well I am able to now return all Child windows very cool. What I am doing is as one is found I grab it heigth and width and compare it to the window I need heigth and width (This window cant be resized) to get its handle. Sounds great right... Well It grabs and goes through the child windows but its not finding the one I want. Now using the software I mentention above I looked through the child windows it grabbed and the window I want again is not there but If I look at the window I want, it shows that the window handle for the main program as its parent. I am not sure what I am Missing here. I do know that the child window I am looking for can exceed the bounds of the parent, its not trapped in the main window. Is this a different kind of child window? this is on the test app and the app I want to use the program on. I am going through the msdn site now looking for some answers.

Thanks

ZeroEffect
 
Back
Top