# Calling API functions

**URL:** <https://discourse.julialang.org/t/calling-api-functions/7944>\
**Category:** New to Julia\
**Created:** [December 23, 2017, 6:27pm UTC](https://discourse.julialang.org/t/calling-api-functions/7944 "2017-12-23T18:27:45Z")\
**Posts on this page:** 7\
**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 23, 2017, 6:27pm UTC](https://discourse.julialang.org/t/calling-api-functions/7944/1 "2017-12-23T18:27:45Z")

</div>

Hi,

Have never used Julia before. For my friend (who uses Julia), I was attempting to provide an example of using API functions to enum the top level window names, which I have done before in other languages.

I managed to get a valid handle to User32.dll and the entry point of EnumWindows using Base.Libdl, but wasn’t able to successfully call the function.

Having spent a couple of hours investigating Julia I came up with this:

```julia
function EnumWindowsProc(title::Ptr,param::Int)
   #TODO: list the returned window names
   return 1
end

lpEnumFunc=cfunction(EnumWindowsProc,Cint,(Ptr{Cstring},Cint))

println(ccall((:EnumWindows,"USER32"),UInt32,(lpEnumFunc,0)))

```

… which obviously doesn’t work ☹

Would be very grateful if anyone can show me where I’m going wrong. Thanks 🙂

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [December 23, 2017, 6:32pm UTC](https://discourse.julialang.org/t/calling-api-functions/7944/2 "2017-12-23T18:32:53Z")

</div>

Please quote your code 🙂

Looking at [EnumWindowsProc callback function (Windows) | Microsoft Docs](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633498(v=vs.85).aspx) the type of the cfunction should be `lpEnumFunc(EnumWindowsProc, Cint, (Cint, Int))` since `HWND` is defined as a `int` and `LPARAM` is either a ` __int32` or `__ int64`.

What part doesn’t work for you? What kind of error are you getting? I am 🐧 only.

---

<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 23, 2017, 6:56pm UTC](https://discourse.julialang.org/t/calling-api-functions/7944/3 "2017-12-23T18:56:39Z")

</div>

Thanks for replying. I dont know what a (penguin emoji) is but thanks.

Julia is throwing: “ccall: wrong number of arguments to C function”, which confused me as EnumWindows definately has 2 args. I thought perhaps I might have to supply a prototype of some kind IDK.

Yes, you are quite right, EnumWindow passes a hWnd to EnumWindowsProc, which then should to be passed to GetWindowText to get the title - apologies I was just doing this example from memory and skipped that step, however AFAIK it hasn’t yet come into play as I still can’t call EnumWindows.

I also found at stackoverflow post suggesting that the calling convetion (stdcall) should also be an arg to ccall but I assume that syntax has been removed (tried it… didn’t work)

---

<div class="post-metadata">

**Author:** ![vchuravy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vchuravy/32/8_2.png) [@vchuravy](https://discourse.julialang.org/u/vchuravy)\
**Post date:** [December 23, 2017, 7:02pm UTC](https://discourse.julialang.org/t/calling-api-functions/7944/4 "2017-12-23T19:02:11Z")

</div>

The penguin is the mascot of Linux.

Your `ccall`is missing the type signature. It should be something like.

`ccall(..., Cint, (Ptr{Void}, Cint), ptr, 0)`. And once you have that changing the calling convention should also work.

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [December 23, 2017, 7:18pm UTC](https://discourse.julialang.org/t/calling-api-functions/7944/5 "2017-12-23T19:18:51Z")

</div>

> [@Type](#):
>
> I also found at stackoverflow post suggesting that the calling convetion (stdcall) should also be an arg to ccall but I assume that syntax has been removed (tried it… didn’t work)

No, it’s still there and still required for most functions in the windows API.

---

<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 23, 2017, 8:37pm UTC](https://discourse.julialang.org/t/calling-api-functions/7944/6 "2017-12-23T20:37:30Z")

</div>

… oh that penguin ofc silly me. Thanks @vchuravy I had the ccall the syntax wrong, this allowed me to call EnumWindows:

```julia
function EnumWindowsProc(hWnd::Int32,param::Int32)
   return 1
end

lpEnumFunc=cfunction(EnumWindowsProc,Cint,(Int32,Int32))
println(ccall((:EnumWindows,"USER32"),UInt32,(Ptr{Void},Cint),lpEnumFunc,0))
```

it happily returns 1 (success) 🙂

Thanks for the info @Keno it complained about the stdcall earlier but that was no doubt just because the other ccall args were wrong. It seems to be defaulting to stdcall anyway so all is well.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [December 23, 2017, 8:45pm UTC](https://discourse.julialang.org/t/calling-api-functions/7944/7 "2017-12-23T20:45:04Z")

</div>

See also [https://github.com/ihnorton/Win32GUIDemo.jl](https://github.com/ihnorton/Win32GUIDemo.jl) (needs updating for a few changes, but the ‘ccall’s should all be fine).
