# Cfunction pointer back to Function?

**URL:** <https://discourse.julialang.org/t/cfunction-pointer-back-to-function/8488>\
**Category:** General Usage\
**Created:** [January 20, 2018, 3:40am UTC](https://discourse.julialang.org/t/cfunction-pointer-back-to-function/8488 "2018-01-20T03:40:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jayschwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayschwa/32/2640_2.png) [@jayschwa](https://discourse.julialang.org/u/jayschwa)\
**Post date:** [January 20, 2018, 3:40am UTC](https://discourse.julialang.org/t/cfunction-pointer-back-to-function/8488/1 "2018-01-20T03:40:09Z")

</div>

```julia
julia> foo() = Cint(1)
foo (generic function with 1 method)

julia> ptr = cfunction(foo, Cint, Tuple{})
Ptr{Nothing} @0x00007f5dacc48510

```

Is it possible to derive `foo` from `ptr`?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [January 20, 2018, 6:10am UTC](https://discourse.julialang.org/t/cfunction-pointer-back-to-function/8488/2 "2018-01-20T06:10:09Z")

</div>

```julia
julia> foo() = Cint(1);

julia> ptr = cfunction(foo, Cint, Tuple{});

julia> _foo = unsafe_wrap(Array, reinterpret(Ptr{typeof(foo)}, ptr), (), false)[];

julia> _foo === foo
true

julia> foo()
1

julia> _foo()
1

```

---

<div class="post-metadata">

**Author:** ![jayschwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayschwa/32/2640_2.png) [@jayschwa](https://discourse.julialang.org/u/jayschwa)\
**Post date:** [January 20, 2018, 6:24am UTC](https://discourse.julialang.org/t/cfunction-pointer-back-to-function/8488/3 "2018-01-20T06:24:07Z")

</div>

`unsafe_wrap(Array, reinterpret(Ptr{typeof(foo)}, ptr), (), false)[]` uses `foo`, and I’m curious if it can be done without it. Sorry if I was unclear.

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [January 20, 2018, 7:44am UTC](https://discourse.julialang.org/t/cfunction-pointer-back-to-function/8488/4 "2018-01-20T07:44:28Z")

</div>

How about directly call it by `ccall`, but in this way, `foo`’s signature info is still needed :

```julia
julia> foo() = Cint(1)
foo (generic function with 1 method)

julia> ptr = cfunction(foo, Cint, Tuple{})
Ptr{Void} @0x0000000111712e30

julia> _foo() = ccall(ptr, Cint, ())
_foo (generic function with 1 method)

```

---

<div class="post-metadata">

**Author:** ![jayschwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayschwa/32/2640_2.png) [@jayschwa](https://discourse.julialang.org/u/jayschwa)\
**Post date:** [January 20, 2018, 9:55pm UTC](https://discourse.julialang.org/t/cfunction-pointer-back-to-function/8488/5 "2018-01-20T21:55:44Z")

</div>

The use case I had in mind was for a C library’s callback setter. E.g. `void* SetCallback(void* funcPtr)` sets a new callback function, and returns the previous callback function. I was curious if that returned function pointer could be changed back into a Julia `Function` without knowing anything more about it.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [January 20, 2018, 10:22pm UTC](https://discourse.julialang.org/t/cfunction-pointer-back-to-function/8488/6 "2018-01-20T22:22:23Z")

</div>

No you can’t do that. Only with debug info maybe but that shouldn’t be used in normal code.

> [@mohamed82008](#):
>
> unsafe\_wrap(Array, reinterpret(Ptr{typeof(foo)}, ptr), (), false);

This is only a complicated way to spell `typeof(foo).instance`. The `ptr` is not used **AT ALL**.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 20, 2018, 10:38pm UTC](https://discourse.julialang.org/t/cfunction-pointer-back-to-function/8488/7 "2018-01-20T22:38:41Z")

</div>

> [@jayschwa](#):
>
> The use case I had in mind was for a C library’s callback setter. E.g. `void* SetCallback(void* funcPtr)`

Each time you call `SetCallback` from Julia, you could store the `Function` object in a dictionary keyed by the `cfunction`, e.g. `mydict[mycfunction] = myfunction`. Then the next time you call `SetCallback` you could look up the `Function` in your dictionary if you want.
