# Writing to a var from a julia func that is called externally

**URL:** <https://discourse.julialang.org/t/writing-to-a-var-from-a-julia-func-that-is-called-externally/7978>\
**Category:** New to Julia\
**Created:** [December 25, 2017, 4:01am UTC](https://discourse.julialang.org/t/writing-to-a-var-from-a-julia-func-that-is-called-externally/7978 "2017-12-25T04:01:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Type](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@Type](https://discourse.julialang.org/u/Type)\
**Post date:** [December 25, 2017, 4:01am UTC](https://discourse.julialang.org/t/writing-to-a-var-from-a-julia-func-that-is-called-externally/7978/1 "2017-12-25T04:01:08Z")

</div>

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.

```julia
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:

```julia
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?

---

<div class="post-metadata">

**Author:** ![anon67531922](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anon67531922](https://discourse.julialang.org/u/anon67531922)\
**Post date:** [December 25, 2017, 4:53am UTC](https://discourse.julialang.org/t/writing-to-a-var-from-a-julia-func-that-is-called-externally/7978/2 "2017-12-25T04:53:24Z")

</div>

Here you go 🙂

```julia
help?> global
search: global GlobalRef cglobal

  global x makes x in the current scope and its inner scopes refer to the global variable of that name. In the example
  below, global is needed so the function can modify the global variable z:

  z=3
  function foo()
      global z=6
  end

  julia> foo()
  6
  julia> z
  6

  Without the global declaration in foo(), a new local variable would have been created inside foo(), and the z in the
  global scope would have remained equal to 3.

```

---

<div class="post-metadata">

**Author:** ![Type](https://avatars.discourse-cdn.com/v4/letter/t/57b2e6/32.png) [@Type](https://discourse.julialang.org/u/Type)\
**Post date:** [December 25, 2017, 5:21am UTC](https://discourse.julialang.org/t/writing-to-a-var-from-a-julia-func-that-is-called-externally/7978/3 "2017-12-25T05:21:49Z")

</div>

That’s it 🙂 thanks @anon67531922. I think I actually tried global in the initial def at one stage, but not when setting the value inside the func. Well spotted.
