Solved

finding a window handle in VB (Visual Studio 2005)

Posted on 2008-06-09
5
594 Views
Last Modified: 2013-11-26
Hi,

I'm trying to figure out how to get the handle of a window that's already open.  I'm using

System.Diagnostics.Process.Start(file)

to load a file into Firefox. Can this give me back a handle?

I've also tried the attached code that I found online, written for VB 6, but I'm in VS 2005 and the line

wtl = GetWindowTextLength(lhWndP) + 1

gives me a runtime error of PInvoke and 'unbalanced stack'

Anybody have code that will find a window and that works in VS 2005?

Thanks!
Gene



Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, _
    ByVal cch As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
 
    Private Const GW_HWNDNEXT = 2
 
    Private Sub Find_Browser_Window()
        Dim lhWndP As Long
        If GetHandleFromPartialCaption(lhWndP, " - Mozilla Firefox") = True Then
            MsgBox("Found Window Handle: " & lhWndP, vbOKOnly + vbInformation)
        Else
            MsgBox("Window ' - Microsoft Outlook' not found!", vbOKOnly + vbExclamation)
        End If
    End Sub
 
    Private Function GetHandleFromPartialCaption(ByRef lWnd As Long, ByVal sCaption As String) As Boolean
        Dim lhWndP As Long
        Dim sStr As String
        Dim wtl As Integer
        GetHandleFromPartialCaption = False
        lhWndP = FindWindow(vbNullString, vbNullString) 'PARENT WINDOW
        Do While lhWndP <> 0
            wtl = GetWindowTextLength(lhWndP) + 1
            Debug.Print("wtl = " & wtl & ", lhWndP = " & lhWndP)
            sStr = (GetWindowTextLength(lhWndP) + 1).ToString '& Chr(0)
            GetWindowText(lhWndP, sStr, Len(sStr))
            sStr = Microsoft.VisualBasic.Left(sStr, Len(sStr) - 1)
            If InStr(1, sStr, sCaption) > 0 Then
                GetHandleFromPartialCaption = True
                lWnd = lhWndP
                Exit Do
            End If
            lhWndP = GetWindow(lhWndP, GW_HWNDNEXT)
        Loop
    End Function

Open in new window

0
Comment
Question by:ugeb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21747382
if you launch the application with Process.Start, you can try with this:

Dim p as Process = System.Diagnostics.Process.Start(file)
Dim h as IntPtr = p.MainWindowHandle

0
 
LVL 11

Author Comment

by:ugeb
ID: 21747442
Hi,

Thanks for the response.  I'm getting the error:

Object reference not set to an instance of an object.

It looks like p is not being set.  Usually the Firefox browser is already running, does that affect it?  How would I get the handle of a process already running?
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 300 total points
ID: 21747503
try with this static method: System.Diagnostics.Process.GetProcessesByName()

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocessesbyname.aspx
0
 
LVL 14

Assisted Solution

by:rachitkohli
rachitkohli earned 200 total points
ID: 21768431
Dim i as long = System.Diagnostics.Process.Start(file).Handle
0
 
LVL 11

Author Closing Comment

by:ugeb
ID: 31465578
Still had problems, but have moved on.  This points me in the right direction.  Thanks!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Asp.Net Session Question 2 49
CSS question 16 63
Visual Studio Start / Browser Emulator menu different 2 40
.NET universe documentation poster 2 26
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question