# Call Win32 API function?

**URL:** <https://forum.crystal-lang.org/t/call-win32-api-function/5658>\
**Category:** Help & Support\
**Created:** [May 13, 2023, 9:11am UTC](https://forum.crystal-lang.org/t/call-win32-api-function/5658 "2023-05-13T09:11:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zura](https://avatars.discourse-cdn.com/v4/letter/z/91b2a8/32.png) [@zura](https://forum.crystal-lang.org/u/zura)\
**Post date:** [May 13, 2023, 9:11am UTC](https://forum.crystal-lang.org/t/call-win32-api-function/5658/1 "2023-05-13T09:11:02Z")

</div>

Hello!

Is it possible to call a Win32 API function on Windows? (native, not wsl). Should I use FFI? e.g. how would you call Win32 API `MessageBox`?

Thanks!

---

<div class="post-metadata">

**Author:** ![MistressRemilia](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/mistressremilia/32/2585_2.png) [@MistressRemilia](https://forum.crystal-lang.org/u/MistressRemilia)\
**Post date:** [May 13, 2023, 10:01am UTC](https://forum.crystal-lang.org/t/call-win32-api-function/5658/2 "2023-05-13T10:01:38Z")

</div>

I’m not very familiar with Windows programming, but I’m pretty sure the Win32 API is a C++ API. You’d need to either have a C wrapper for it, or maybe Microsoft already supplies this (or maybe it’s a C API already? Again, I’m not a Windows programmer ^\_^)

When you have the C wrapper, you can use the usual `lib` keyword [to make C bindings](https://crystal-lang.org/reference/1.8/syntax_and_semantics/c_bindings/lib.html) so Crystal can access it. Something similar to, but not exactly like, this:

```crystal
@[Link("windows")]
lib LibWin32
  alias HWND = Void*
  fun messageBox = "MessageBox"(handle : HWND, text : UInt8*, caption : UInt8*, typ : LibC::UInt) : LibC::Int
end

LibWin32.messageBox(nil, "Hello, world!", "Hello", 0)

```

This code won’t work if you copy-paste it, but it might give you a starting point.

EDIT: Actually, I think Win32 already is a C API.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [May 13, 2023, 1:10pm UTC](https://forum.crystal-lang.org/t/call-win32-api-function/5658/3 "2023-05-13T13:10:27Z")

</div>

It’s almost there. This works:

```cr
@[Link("User32")]
lib LibUser32
  alias HWND = Void*
  fun messageBoxExA = "MessageBoxExA"(handle : HWND, text : UInt8*, caption : UInt8*, typ : LibC::UInt, languageId : LibC::DWORD) : LibC::Int
end

LibUser32.messageBoxExA(nil, "Hello, world!", "Hello", 0, 0)

```

`MessageBox` seems to be unavailable in my library (maybe it’s only available in the dll?), but `MessageBoxExA` or `MessageBoxExW` work.

---

<div class="post-metadata">

**Author:** ![HertzDevil](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hertzdevil/32/1023_2.png) [@HertzDevil](https://forum.crystal-lang.org/u/HertzDevil)\
**Post date:** [May 13, 2023, 2:42pm UTC](https://forum.crystal-lang.org/t/call-win32-api-function/5658/4 "2023-05-13T14:42:20Z")

</div>

If `A` and `W` variants exist then the one without either is always a C macro that expands to one of them depending on `#ifdef UNICODE`.
