Solved

FindWindow Not Working

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

708 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

14 Experts available now in Live!

Get 1:1 Help Now