When interacting with the Windows API (Win32), you will inevitably face two facts: libraries are named things like user32.dll, and text is almost always expected to be UTF-16 (Wide Characters). In XS, bridging this gap involved tedious calls to MultiByteToWideChar and manual buffer management.

Affix handles the translation for you.

The Recipe

This recipe displays a native Windows message box containing Unicode characters (an emoji). It looks like this:

Screenshot 2025-12-23 152053

Okay, here's the sauce. ...the source:

use v5.40;
use utf8; # Required for literal emojis in source
use Affix;

# Define some standard Windows constants
use constant MB_OK                   => 0x00000000;
use constant MB_DEFAULT_DESKTOP_ONLY => 0x00020000;

# Bind the function
# Library: user32.dll
# Symbol:  MessageBoxW (The Unicode version)
# Alias:   MessageBox  (What we call it in Perl)
affix 'user32', [ MessageBoxW => 'MessageBox' ] =>
    [ Pointer [Void], WString, WString, UInt ] => Int;

# Call it
# undef becomes NULL (No owner window)
MessageBox( undef, 'Keep your stick on the ice.', '🏒', MB_OK | MB_DEFAULT_DESKTOP_ONLY );

How It Works

Kitchen Reminders