FindWindow returning handles of non existant programs

B

Bob

Hi,
Trying to determine if a program is running or not.
I implemented the example code below but I am getting handles returned
regardless of whether a program exists or not. i.e. You can feed in a
rubbish string for the lpClassName parameter and still get back a long > 0.
What am I doing wrong?
thanks
Bob
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
_

lpClassName As String, ByVal lpWindowName As String) As Long

Public Function GetHandleByClass(ByVal strTargetProgram As String) As Long

Try

Return FindWindow(strTargetProgram, vbNullString) ' Always returns a large
arbitary number

Catch ex As Exception

MsgBox("Gethandle error " & ex.Message)

End Try

End Function
 
T

Tom Shelton

Hi,
Trying to determine if a program is running or not.
I implemented the example code below but I am getting handles returned
regardless of whether a program exists or not. i.e. You can feed in a
rubbish string for the lpClassName parameter and still get back a long > 0.
What am I doing wrong?
thanks
Bob
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal
_

lpClassName As String, ByVal lpWindowName As String) As Long

This declaration should be:

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

A long is 64-bits in VB.NET.
 
B

Bob

Hi Tom, Thanks for that,
I also found
<DllImport("user32.dll")> Public Shared Function _

FindWindow(ByVal strClassName As String, ByVal strWindowName _

As String) As Integer

Which works.

Is there any .net reference for the WINAPI calls? Guess there must be but I
haven't been able to find it. All the examples I have tried have been from
trolling through the help etc.
regards
bob
 
D

Dragon

Hi Bob
Is there any .net reference for the WINAPI calls? Guess there must be but I
haven't been able to find it. All the examples I have tried have been from
trolling through the help etc.

Try ApiViewer 2004:

http://www.activevb.de/rubriken/apiviewer/index-apiviewereng.html

It provides declarations of many API functions, constants, and structures in
VB, VB .NET, C# and many more.

For descriptions and samples see API-Guide:

http://www.allapi.net/cgi-bin/redirect.cgi?place=apiguide

Most samples there are in VB6, but there's some .NET replacement code as
well.

HTH

Roman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top