Hi. New to Julia, using the Windows 7 32bit API. The “EnumWindows” API func takes the address of a callback procedure (designated “EnumWindowsProc”) and an arbritrary user defined Int as args. It then iterates through the top level windows and calls EnumWindowsProc with a handle to each window and the user Int. EnumWindowsProc should return non-zero to continue.
The following code is to find a given named window - here “Program Manager” for testing purposes.
const U32="USER32"
const windowname="Program Manager"
hMyWindow=0
function getWindowText(hWnd::Int32)
#wrapper for GetWindowTextA - working, no issues
buf=Array{UInt8}(512)
ccall((:GetWindowTextA,U32),stdcall,Int32,(Int32,Ptr{UInt8},Int32),hWnd,buf,256)
buf[end]=0
return unsafe_string(pointer(buf))
end
function EnumWindowsProc(hWnd::Cint,param::Cint)
if getWindowText(hWnd)==windowname
hMyWindow=hWnd
println("found window: ",hMyWindow)
return 0
end
return 1
end
lpEnumFunc=cfunction(EnumWindowsProc,Cint,(Cint,Cint))
println("EnumWindows returned: ",ccall((:EnumWindows,"USER32"),Cint,(Ptr{Void},Cint),lpEnumFunc,0))
println("handle is: ",hMyWindow)
The EnumWindows call works correctly. When the named window is located in EnumWindowsProc its value is reported by the println("found window: ",hMyWindow) inside that function, however when the EnumWindows call returns hMyWindow is still set to 0
Am I not defining the scope of hMyWindow to include EnumWindowProc so a local hMyWindow is being created? or perhaps is the value being garbage collected on return from EnumWindows?
EnumWindowProc is only called as a callback by the OS and not directly from any Julia code. Another solution could be to use the user param to supply the address of a variable and write it directly there, but this seems a bit overcomplicated. I feel sure there is something simple I am missing here.
Output is:
found window: 65758
EnumWindows returned: 0
handle is: 0
EnumWindows returning 0 is correct as it is being terminated by EnumWindowProc returning 0 when the window is located.
How do I get this handle out of the callback proc back to my julia program?