1

I am writing a Little VBA Programm that Needs to wait until a specific windows is open. I want to do this using FindFindow form the user32.dll but i cant get it run. Weird Thing is that even if i set the 2 Parameters of the function to Null, i still get a negative return, although in that case all windows should match. Basically i dont get a result different from 0 for hwnd Independent of how i call FindWindow. I searched Stack OPverflow and i also Googled the Problem but i cant find what i am doing wrong. Any help is appreciated.

Declare Function FindWindow Lib "user32" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Sub Main
    Dim hwnd As Long

    hwnd = FindWindow(vbNullString, vbNullString)

    If (hwnd = 0) Then MsgBox ("failure")

End Sub

The Solutions to similar Problems like How to use FindWindow to find a visible or invisible window with a partial name in VBA dont seem to work either.

Lorenz Kummer
  • 75
  • 3
  • 11

1 Answers1

2

The problem is that vbNullString is a length 0 string, the same as "". When that is marshalled to the unmanaged code a non-null pointer to a null-terminated array of characters is passed. Because the length is 0 then a pointer to a null-terminator is passed. That's different from NULL which is the null pointer.

I'm far from a VBA expert, but I think the solution is like so:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByRef lpClassName As Any, ByRef lpWindowName As Any) As Long

If you want to call this passing NULL for both arguments do so like this:

window = FindWindow(ByVal 0&, ByVal 0&)

Alternatively, if you want to pass a string value do it like this:

window = FindWindow(ByVal 0&, ByVal "Untitled - Notepad")
David Heffernan
  • 587,191
  • 41
  • 1,025
  • 1,442
  • This gives me a Type mismatch error. Also i the Syntax using vbNullString has been reported a working for other users. Example. https://stackoverflow.com/questions/25098263/how-to-use-findwindow-to-find-a-visible-or-invisible-window-with-a-partial-name The solution provided in that thread doesnt work either. – Lorenz Kummer Mar 09 '18 at 09:11
  • It's working here. Although I'm using 64 bit Excel. Note that I excised the `PtrSafe` in my `Declare`. I can't see your exact error message or where it occurs, so I can't comment on that. Details matter. – David Heffernan Mar 09 '18 at 09:19
  • I get the following erros: https://i.imgur.com/l8T7SEQ.png or https://i.imgur.com/IE8eNyi.png and https://i.imgur.com/YI4UCq1.png depending on how i implement the function. – Lorenz Kummer Mar 09 '18 at 09:58
  • Works fine for me in 32 bit Excel 2010. Sounds like you have a less capable VBA implementation. – David Heffernan Mar 09 '18 at 10:01
  • Ok so it 's possibly got to do something with the environment i am running the script in. I am running it in Dragon NaturallySperaking 15. – Lorenz Kummer Mar 09 '18 at 10:06
  • Yeah, I'm pretty sure that's the crucial information. I'm sure that the issue is what I explained already. That `vbNullString` does not marshal as `NULL`. But what the solution in your VBA is I cannot tell. If you always want to pass `NULL` to the same argument, you can declare it as `ByVal lpXXX As Long` and pass `0&`. That would allow you to prove that the issue is what I say. You can use `Alias` to declare a variety of combinations of imports, so I think that should get you home. – David Heffernan Mar 09 '18 at 10:09