manged alternative to get the complete text from each open window

G

Guest

i have a solution to gather the text in each window that is open, but would
rather use only managed code rather than unmanaged code my current solution
relies on. The purpose is to get the text in each open window
even/especially for child windows in other non managed mdi apps - if excel
has 3 books open or any other mdi app has several windows open, I'd like the
complete text from each window.

The current solution gives me more info than i need (quite a bit), as i'm
getting not just the text from each open window - i'm getting other stuff
that i am not familiar with with my desired info. heres the code i'm using
currently:

# Windows Form Designer generated code #

Private Delegate Function EnumWindowsProc( _
ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32

Private Declare Function EnumWindows Lib "user32.dll" _
(ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32

Private Declare Function GetWindowTextLength Lib _
"user32.dll" Alias "GetWindowTextLengthA" _
(ByVal hwnd As IntPtr) As Int32

Private Declare Function GetWindowText Lib "user32.dll" Alias
"GetWindowTextA" _
(ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32

Private Function EnumWindowsCallBack(ByVal hwnd As IntPtr, ByVal lParam As
Int32) As Int32

Dim sSave As String

sSave = Space(GetWindowTextLength(hwnd) + 1)

GetWindowText(hwnd, sSave, Len(sSave))

sSave = Microsoft.VisualBasic.Left(sSave, Len(sSave) - 1)

If sSave.Trim <> "" Then
lv.Items.Add(sSave)
End If

Return 1
End Function

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click

lv.Items.Clear()
EnumWindows(AddressOf EnumWindowsCallBack, 0)

End Sub

Either a manged only alternative(perfect), a change to make to get only the
window text and not everythign else (a close second) or direction to go look
further (always appreciated) would be nice.

gabe
 
G

Guest

Cor, darn i'm glad you responded, you've been invaluable in the past. yeah,
i spent the last 45 minutes staring at that exact page, checking
documentation for the classes and looking for samples (google, .net2themax,
et al) and while i see that the Win32 function GetWindowText is mapped to
System.Windows.Forms.Form.Text, I didnt see how that class could be used to
gather the window text for other/external programs and thier the window text
for mdiChildren. are you looking at another class or do i (again)
misunderstand that class, or are you using more than just that class?

gabe
 
H

Herfried K. Wagner [MVP]

gabe said:
while i see that the Win32 function GetWindowText is mapped to
System.Windows.Forms.Form.Text, I didnt see how that class could be used
to
gather the window text for other/external programs and thier the window
text
for mdiChildren. are you looking at another class or do i (again)
misunderstand that class, or are you using more than just that class?

The 'Form' class is not a replacement of the Win32 API calls you mentioned
in your post. Some of the functionality you want to archieve can be
archieved using the 'System.Diagnostics.Process' class and its
'GetProcesses' method and 'MainWindowTitle' property, however, I doubt that
a fully managed solution is possible.
 
G

Guest

bummer - really wish someone i respected less was telling that they doubted a
fully managed solution was possible, i saw the MainWindowTitle property and
thought maybe i had skipped over ChildWindowTitles() or ChildWindow() on the
page, but no - just dont seem to exist. sadly, MainWindowTitle doesnt
provide what i'm looking for.

the solution is to run a db query based on some text found in another
process' childWindowTitle, then start an additional process based on the
results of the query.
looks like i'm leaving in the win32api calls. oh well.

if you run my sample code you'll see that i get a bunch of entries that
arent visable windows?
can i somehow remove the entires that arent windows captions?

Aside from www.pinvoke.net, where do you go for vb.net win32api reference?


thanks tons
gabe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top