{% if is_async %}
        return await Task.Run(() =>
        {
            var rc = NativeMethods.{{ native_method_name }}(
{{ args_block }}                out var outPtr,
                out var outLen,
                out var outCap
            );
            if (rc != 0)
            {
                var ec = NativeMethods.LastErrorCode();
                var ctxPtr = NativeMethods.LastErrorContext();
                var msg = System.Runtime.InteropServices.Marshal.PtrToStringUTF8(ctxPtr) ?? "Unknown error";
                throw new {{ exception_name }}(ec, msg);
            }
            var result = new byte[(int)outLen];
            System.Runtime.InteropServices.Marshal.Copy(outPtr, result, 0, (int)outLen);
            NativeMethods.FreeBytes(outPtr, outLen, outCap);
            return result;
        });
{% else %}
        var rc = NativeMethods.{{ native_method_name }}(
{{ args_block }}            out var outPtr,
            out var outLen,
            out var outCap
        );
        if (rc != 0)
        {
            var ec = NativeMethods.LastErrorCode();
            var ctxPtr = NativeMethods.LastErrorContext();
            var msg = System.Runtime.InteropServices.Marshal.PtrToStringUTF8(ctxPtr) ?? "Unknown error";
            throw new {{ exception_name }}(ec, msg);
        }
        var result = new byte[(int)outLen];
        System.Runtime.InteropServices.Marshal.Copy(outPtr, result, 0, (int)outLen);
        NativeMethods.FreeBytes(outPtr, outLen, outCap);
        return result;
{% endif %}
