Solved

FindWindow Not Working

Posted on 2004-08-19
2
944 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …

738 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