Enhanced FindWindow for variable window class names: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Updated author links.)
m (Added category links.)
Line 83: Line 83:
See [http://forums.winamp.com/showthread.php?s=&threadid=172415 this topic] for more information about the function itself and alternatives.
See [http://forums.winamp.com/showthread.php?s=&threadid=172415 this topic] for more information about the function itself and alternatives.
See [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/WindowClasses/WindowClassReference/WindowClassFunctions/GetClassName.asp the msdn] for more information about the user32 api call "GetClassName".
See [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/WindowClasses/WindowClassReference/WindowClassFunctions/GetClassName.asp the msdn] for more information about the user32 api call "GetClassName".
[[{{ns:14}}:User Interface Functions]]

Revision as of 22:00, 30 April 2005

Author: Comm@nder21 (talk, contrib)


Introduction

Sometimes, you want to send messages to a window, that you don't know the full window class name (wcn) of, or it contains variables and you know the constant part. So you can't use FindWindow, that doesn't support wildcars. This function doesn't too. But you can get the handle of any window, whith only one known part of the wcn.

Usage

Simply call this function. At the moment, all variables are hardcoded, so you don't have to push anything. Maybe, i'll add this possibility later.

Push  "Application"   # the known part of the wcn
Push  0   # the starting offset of the upper push
Call  EnhancedFindWindow
Pop  $0   # will contain the window's handle
Pop  $1   # will containg the full wcn
          # both will containg "failed", if no matching wcn was found

The Function

Function  EnhancedFindWindow
  ; input, save variables
  Exch  $0   # part of the wcn to search for
  Exch
  Exch  $1   # starting offset
  Push  $2   # length of $0
  Push  $3   # return code
  Push  $4   # window handle
  Push  $5   # returned class name
  Push  $6   # max length of $5
 
  ; set up the variables
  SetPluginUnload  alwaysoff     # recommended, if u're using the \
                                   system plugin
  StrCpy  $4  0                  # FindWindow wouldn't work without
  StrCpy  $2  ${NSIS_MAX_STRLEN} # the max length of string variables
  StrLen  $5  $4                 # it's length
 
 ; loop to search for open windows
 search_loop:
  FindWindow  $4  ""  ""  0  $4
   IntCmp  $4  0  search_failed
    IsWindow  $4  0  search_failed
     System::Call  \
       'user32.dll::GetClassName(i, t, *i) i(r4r4, .r5, r6r6) .r3'
     IntCmp  $3  0  search_loop
      StrCpy  $3  $5
      StrCpy  $5  $5  $2  $1
      StrCmp  $0  $5  search_end  search_loop
 
 ; no matching class-name found, return "failed"
 search_failed:
  StrCpy  $4  "failed"
  StrCpy  $3  "failed"
 
 ; search ended, output and restore variables
 search_end:
  SetPluginUnload  manual  # the system-plugin can now unload itself
  System::Free  0          # free the memory
 
  StrCpy  $1  $3
  StrCpy  $0  $4
  Pop  $6
  Pop  $5
  Pop  $4
  Pop  $3
  Pop  $2
  Exch  $1
  Exch
  Exch  $0
FunctionEnd

Feedback

Any suggestions, feature requests or even bug reports? Feel free to send me an email or a private message.

Please post any other question, e.g. about usage and customization at the NSIS forums. Remember: You're not alone ...

Further Reading

See this topic for more information about the function itself and alternatives. See the msdn for more information about the user32 api call "GetClassName".