Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grabbing the handle of a class within a process...

Status
Not open for further replies.

Zoplax

Technical User
Oct 2, 2002
58
0
0
US
I have a couple of VB.NET functions as follows:

Private Function EnumParentProc(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean

Dim sb As New StringBuilder(256)

GetWindowText(hWnd, sb, sb.Capacity)

Dim windowText As String = sb.ToString()

'If (windowText.ToString = "<main program>") Then
If (windowText.ToString = "<sub program within main>") Then

MsgBox(windowText.ToString)


EnumChildWindows(hWnd, ecp, IntPtr.Zero)

End If

Return True

End Function



Private Function EnumChildProc(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean

Dim className As New StringBuilder(256)

GetClassName(hWnd, className, className.Capacity)

If className.ToString.StartsWith("<class name within sub program>") Then

MsgBox(className.ToString)
MsgBox(hWnd.ToInt32)
MsgBox(lParam.ToInt32)

Dim textLength As Integer = SendMessage(hWnd, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)
Dim windowText As New StringBuilder(textLength + 1)

SendMessage(hWnd, WM_GETTEXT, windowText.Capacity, windowText)

ComboBox1.Items.Add(windowText)

MsgBox(windowText.ToString)

End If
Return True

End Function



Basically, this tries to grab a handle to a class within a program. However, the issue is that there happen to be two objects with the same class name within the program, and I only want the handle of the first of these.

I'm using WinspectorSpy to spy on the objects, and according to it the one class I want has an "ID" of 100, while the one I do NOT want has an "ID" of 101.

Is there some way to not only use the ClassName, but also use the ID, so that I can pick the one of these two which I want to send messages to?

Forgive me if I'm mangling the terminology, I'm in the process of teaching myself about the Win32 API and I haven't got all the terms straight in my mind yet. I can post the entire source code I have so far if that'll help; it currently enumerates both like-named classes and shows msgboxes for each with the class name and I guess the handle (integer).

Thanks in advance for any help, I would greatly appreciate it. :)
 
>>I'm in the process of teaching myself about the Win32 API

If you want to use the Win32 API then why using .NET? Wasn't .NET supposed to be OS independent?

Greetings,
Rick
 
Good question; I've been reading up on the Win32 API and .NET recently and it seems like .NET has evolved quite a bit for purposes of event and message handling between two .NET apps, injecting messages and such into foreign apps like I'm trying to do seems still the realm of Win32 API calls in (for lack of a better term) the "old" VB6 style.

I've managed to figure out what my problem was above, for this particular program I was trying to insert messages into, I was referencing the wrong control within the program.

There are two Edit controls and I was sending messages to the wrong one! Once I updated my code to reference the other it worked like a charm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top