WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

DllCall Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

DllCall With Callback Example

 Keywords: DllCall Callback EnumWindows EnumWindowsProc DllCallbackCreate DllCallbackDestroy UDF UDS WinItemize ptrGlobalDefine ptrGlobal

Example 1 - Locate and CLose a Specific Window:

; DllCall with Callback example.
; Requires WinBatch 2011B or newer
#DefineFunction udfEnumWindowsProc(hwnd, lparam)
      ; This callback will run until:
      ;   Callback returns FALSE
      ;   There are no more top level windows to enumerate
      maxsize = 256
      lptitle = BinaryAlloc(maxsize)
      BinaryEodSet(lptitle, 0)
      length = DllCall(DirWindows(1):'user32.dll', long:'GetWindowTextA',long:hwnd,lpbinary:lptitle,long:maxsize)
      If length>1
         BinaryEodSet(lptitle, length)
         title = BinaryPeekStr(lptitle, 0, length)
         If StrIndex( title, lparam, 1, @FWDSCAN )
            ret = AskYesNo( lparam:' window exists!', 'Close this window? [':title:']')
            If ret == @YES Then WinClose(title) ;Close Window
         EndIf
      EndIf
      BinaryFree(lptitle)
      Return @TRUE
#EndFunction


; Locates window and closes it
lookfortitle = 'Notepad'
ShellExecute('Notepad.exe', '', '', @NORMAL, '')
TimeDelay(1)
cbhandle = DllCallbackCreate('udfEnumWindowsProc', 'LONG', 'LONG LPSTR')
ret = DllCall( DirWindows(1):'user32.dll', long:'EnumWindows',callback:cbhandle,lpstr:lookfortitle)
DllCallbackDestroy(cbhandle)
Exit


Example 2 - List all Windows:

; DllCall with Callback example.
; Requires WinBatch 2011B or newer
#DefineFunction udfEnumWindowsProc(hwnd, lparam)
      ; This callback will run until:
      ;   Callback returns FALSE
      ;   There are no more top level windows to enumerate
      Ptr_List=PtrGlobal(List)
      maxsize = 256
      lptitle = BinaryAlloc(maxsize)
      BinaryEodSet(lptitle, 0)
      length = DllCall(DirWindows(1):'user32.dll', long:'GetWindowTextA',long:hwnd,lpbinary:lptitle,long:maxsize)
      If length>1
         BinaryEodSet(lptitle, length)
         title = BinaryPeekStr(lptitle, 0, length)
         If *Ptr_List == "" Then *Ptr_List = title
         Else *Ptr_List = *Ptr_List : @TAB : title
      EndIf
      BinaryFree(lptitle)
      Return @TRUE
#EndFunction

; Enumerate All Windows
PtrGlobalDefine( List )
list = ""
cbhandle = DllCallbackCreate('udfEnumWindowsProc', 'LONG', 'LONG LPSTR')
ret = DllCall( DirWindows(1):'user32.dll', long:'EnumWindows',callback:cbhandle,lpstr:"")
DllCallbackDestroy(cbhandle)

AskItemlist("EnumWindows Results", list, @TAB, @SORTED, @SINGLE )
Exit

Article ID:   W17789
Filename:   DllCall With Callback Example.txt
File Created: 2013:03:05:15:41:04
Last Updated: 2013:03:05:15:41:04