Solved

FindWindow Not Working

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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This is an explanation of a simple data model to help parse a JSON feed
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 …
Simple Linear Regression

728 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