Solved

FindWindow Not Working

Posted on 2004-08-19
2
937 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
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

813 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now