On 2006-11-30, Newbie Coder <ne**********@pleasespamme.comwrote:
Hello Group
I am trying to find a window using the known classname, which I got through
Spy++
The classname is'AfxWnd80s' & when I use FindWindow API to return the INT32
with the window handle it returns 0 (zero)
Example:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As String, ByVal lpWindowName As String) As Int32
Private Declare Auto Function FindWindow Lib "user32" _
(Byval lpClassName As String, ByVal lpWindowName As String) As IntPtr
Don't alias API calls anymore. That is a relic of the bad old days of VB6
when VB could not call unicode api calls directly. Just declare the function
Auto. The .NET marshaller is smart enough to figure out the proper function
to call on the current platform. If you alias to the "A" function on a NT
based platform (NT, 2000, XP, etc) - you are causing the runtime way more work
then is necessary, because it will first convert your string from it's unicode
format to ansi, and then pass that buffer to the api call. The api call will
promptly convert the ansi string it was passed to unicode and delegate the
call to the "W" function. The situation is even worse if the api call
actually modifies the string (which this one doesn't)... Because then that whole process has to be
reversed on top of the fact that because you used a system.string to recieve
the buffer rather then a system.text.stringbuilder, the vb.net marshaler get's
involved to cover your butt from the fact that strings are immutable. In
fact, using system.string as a api buffer will not work correctly in C#
because it doesn't cover for you :)
Dim i As Int32 = FindWindow("AfxWnd80s", VbNullString)
Dim i As IntPtr = FindWindow ("AfxWnd80s", Nothing)
--
Tom Shelton