Skip to content

Generate QueryInterface-like helper function for callbacks #1835

Description

@MarijnS95

It'd be great to have helper functions for callbacks with nicer signatures in the windows crate, just like "normal function" wrappers. For starters, when doing this to Query (or QueryOptional) callbacks, code can be written as follows:

let lib = unsafe { Library::new(dxcompiler_lib_name()) }.unwrap();
let create: Symbol<DxcCreateInstanceProc> = unsafe { lib.get(b"DxcCreateInstance\0") }.unwrap();

let compiler: IDxcCompiler2 = unsafe { DxcCreateInstanceProc(&create, &CLSID_DxcCompiler) }?;
let library: IDxcLibrary = unsafe { DxcCreateInstanceProc(&create, &CLSID_DxcLibrary) }?;

Without this we'd have to replace the last two lines with this atrocity (using transmute for lack of a non-verbose way to put together all the pointer casts):

let mut compiler = None::<IDxcCompiler2>;
let mut library = None::<IDxcLibrary>;

unsafe { (create.unwrap())(&CLSID_DxcCompiler, &IDxcCompiler2::IID, std::mem::transmute(&mut compiler)).ok()? };
unsafe { (create.unwrap())(&CLSID_DxcLibrary, &IDxcLibrary::IID, std::mem::transmute(&mut library)).ok()? };

let compiler = compiler.unwrap();
let library = library.unwrap();

There are currently 517 hits for pub type PFN_ and there must be some that behave like QueryInterface (i.e. D3D10/11/12 CREATE_DEVICE). I'd like to get #747 reopened for this to make it in, but one major blocker was not having any (test) code that actively practices this system.

Reasons to not do this

  1. There currently doesn't seem to be any use of this "external symbol loading" for Windows, only when loading an external library like DXC (which can supposedly also be linked from "windows" directly if #[link = "windows"] on DxcCreateInstance is to be believed);
  2. Since writing this code/example, windows-rs is now more strict in requiring _COM_Outptr_ SAL before marking functions/callbacks as SignatureKind::Query. This has been worked around for DxcCreateInstance(2) in Add ComOutPtr attribute to additional params win32metadata#890, but not for the *Proc function types (this will be PR'd to DXC soon™).

Originally posted by @MarijnS95 in #747 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions