    /// <summary>
    /// {{ doc }}
    /// </summary>
    public int {{ method_name }}({% if signature_params %}{{ signature_params }}, {% endif %}Delegate handler) {
        var handle = GCHandle.Alloc(handler, GCHandleType.Normal);
        IntPtr ctx = GCHandle.ToIntPtr(handle);

        int result = NativeMethods.{{ native_method }}(
            _handle,
            _handlerCallback,
            ctx{{ arg_lines }}
        );

        if (result == 0) {
            lock (_registeredCallbacks) {
                _registeredCallbacks[ctx] = handle;
            }
        } else {
            handle.Free();
        }
        return result;
    }
