?
Solved

FindWindow Not Working

Posted on 2004-08-19
2
Medium Priority
?
950 Views
Last Modified: 2008-02-01
In VB.NET, I have the following that SHOULD work...

In the general declarations section of a form:

    'Finds a Window based on ClassName or WindowName or Both
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

In the form click event I have the following:

        Dim ClassName As String
        Dim Caption As String
        Dim hWnd As Long

        ClassName = "SciCalc"
        Caption = "Calculator"

        hWnd = FindWindow(ClassName, "")

        MessageBox.Show(CStr(hWnd))

The problem is that I get a value returned no matter what !  In the example above, I did NOT have the calc running and I still get a value from FindWindow.  I should have seen it as ZERO ???

Everything I've seen says this should work.  Please help, this is driving me nuts !

Thanks,

John
0
Comment
Question by:trzesniakj
[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 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 11849351
There were two problems with your code.

1) You were using a VB6 declaration of the API.  In .Net it should return an IntPtr, not a Long.
2) You should pass in vbNullString instead of "" for an empty parameter.

Regards,

Idle_Mind

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Declare Auto Function FindWindow Lib "user32" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String) As IntPtr

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ClassName As String
        Dim Caption As String
        Dim hWnd As IntPtr

        ClassName = "SciCalc"
        Caption = "Calculator"

        hWnd = FindWindow(ClassName, vbNullString)
        'hWnd = FindWindow(vbNullString, Caption)

        MessageBox.Show(hWnd.ToString)
    End Sub
End Class
0
 

Author Comment

by:trzesniakj
ID: 11851601
Wonderful !  That was it !  

Thanks !

John
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Make the most of your online learning experience.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Six Sigma Control Plans
Suggested Courses

770 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