Windows API Call - FindWindow Not Working

  1. 7 weeks ago

    Aaron S

    Feb 18 Pre-Release Testers, Xojo Pro San Diego, CA

    I can't seem to figure out this damn problem. All I want to do is bring a 3rd-party application window to the foreground and no matter what I do, this call returns 0. I have an application that interacts with the Word application object and I need to bring a document to the foreground.

    Thank you!

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (strClassName As CString, strWindowName As CString) As Integer
    Var nWinHandle As Integer = FindWindow(nil,"Document1 - Word")
    MessageBox(Str(nWinHandle))

  2. Andrew L

    Feb 18 San Francisco, CA, USA

    It works for me if I change the call from FindWindowExA to FindWindowExW, and use the window title "Document 1 - Microsoft Word".

  3. Aaron S

    Feb 18 Pre-Release Testers, Xojo Pro San Diego, CA

    @Andrew L Unfortunately that didn't work for me...still getting 0. The title in the window is "Document1 - Word" and no matter how I use FindWindow (including the way you described with FindWindowExW), I always get 0. Ugh.

    Thank you for trying to help me.

  4. Aaron S

    Feb 18 Pre-Release Testers, Xojo Pro San Diego, CA

    Ahhhh...finally found a solution. Needed to specify the class for Word. This worked:

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (strClassName As CString, strWindowName As CString) As Integer
    Declare Function SetForegroundWindow Lib "user32" (hWnd As Integer) As Integer
    Var nWinHandle As Integer = FindWindow("opusapp","Document1 - Word")
    Call SetForegroundWindow(nWinHandle)

  5. Arnaud N

    Feb 21 Pre-Release Testers, Xojo Pro

    @Aaron S Ahhhh...finally found a solution. Needed to specify the class for Word. This worked:

    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (strClassName As CString, strWindowName As CString) As Integer
    Declare Function SetForegroundWindow Lib "user32" (hWnd As Integer) As Integer
    Var nWinHandle As Integer = FindWindow("opusapp","Document1 - Word")
    Call SetForegroundWindow(nWinHandle)

    Just to clarify: if your app is meant to be used in other circumstances than the one you're debugging to, hard-coding the document title would likely fail.
    This could occur in these cases (at least):
    •Your user has Office or its OS in a different language;
    •The user has already saved the document;
    •Another document has been opened/created;
    •On Mac, all documents have been closed;
    •Future/earlier versions of Office whose the default title is different than your expectation.

    You may better enumerate available windows title and pick the one you want (in case it's not what you already do).

  6. Aaron S

    Feb 21 Pre-Release Testers, Xojo Pro San Diego, CA

    @Arnaud N Thank you! Yes, this is something I have to consider! In this case I was keeping it as simple as possible...now I have to make it more reliable for the end user. If you have any sample code for enumerating through windows, that would be fantastic. Otherwise I can sift through Microsoft's documentation.

    Thanks!

    ~Aaron

  7. Arnaud N

    Feb 21 Pre-Release Testers, Xojo Pro

    @Aaron S If you have any sample code for enumerating through windows, that would be fantastic. Otherwise I can sift through Microsoft's documentation.

    Thanks!

    ~Aaron

    You're welcome.
    Ok, so here's some code I used for that purpose. Just note it was written in 2017 (but I expect it would still work); since then, I'm using the MBS plugin for enumerating the windows without the declares.

    This code would bring a window to the foreground, given a passed name:

    Soft Declare Function FindWindowW Lib "user32.dll" (lpClassName As integer,lpWindowName As integer) as integer
    Soft Declare Function GetWindow Lib "user32" (hWnd As integer,wCmd As integer) As integer
    Soft Declare Function GetWindowTextW Lib "user32" (hWnd As integer,lpString As ptr,cch As integer) As integer
    Soft Declare Sub ShowWindow Lib "User32" (wnd As Integer, nCmdShow As Integer)
    Soft Declare Function SetForegroundWindow Lib "User32" (hWnd As Integer) As Integer
    
    Dim ret As Integer
    Dim mb as new MemoryBlock(255)
    ret=FindWindowW(0,0)
    While ret>0
      if GetWindowTextW(ret,mb,mb.size)>0 then
        if mb.WString(0)="The window's title" then
          ShowWindow(ret,9)
          Call SetForeGroundWindow(ret)
        end if
      end if
      ret=GetWindow(ret,2)
    Wend

    Please let me know whether it works (I'm not on Windows right now).

or Sign Up to reply!