flow/src/win32/win32ext.zig
Jonathan Marler 99aefc8d22 remove direct2d dependency and update zigwin32
Since we removed the direct2d renderer we no longer need to reference
the direct2d-zig repository.  Instead we now directly reference the
zigwin32 repository.  I've also updated that repository with a few fixes
and additions which allowed us to remove some code from flow.
2025-01-21 08:40:44 +01:00

21 lines
1 KiB
Zig

const std = @import("std");
const win32 = @import("win32").everything;
// TODO: update zigwin32 with a way to get the corresponding IID for any COM interface
pub fn queryInterface(obj: anytype, comptime Interface: type) *Interface {
const obj_basename_start: usize = comptime if (std.mem.lastIndexOfScalar(u8, @typeName(@TypeOf(obj)), '.')) |i| (i + 1) else 0;
const obj_basename = @typeName(@TypeOf(obj))[obj_basename_start..];
const iface_basename_start: usize = comptime if (std.mem.lastIndexOfScalar(u8, @typeName(Interface), '.')) |i| (i + 1) else 0;
const iface_basename = @typeName(Interface)[iface_basename_start..];
const iid_name = "IID_" ++ iface_basename;
const iid = @field(win32, iid_name);
var iface: *Interface = undefined;
const hr = obj.IUnknown.QueryInterface(iid, @ptrCast(&iface));
if (hr < 0) std.debug.panic(
"QueryInferface on " ++ obj_basename ++ " as " ++ iface_basename ++ " failed, hresult=0x{x}",
.{@as(u32, @bitCast(hr))},
);
return iface;
}