Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCWSTR arguments/fields are generated as PWSTR in in Rust bindings #777

Closed
randomPoison opened this issue May 7, 2021 · 6 comments
Closed
Labels
enhancement New feature or request

Comments

@randomPoison
Copy link

I'm currently using the windows crate to generate bindings the the Projected Filesystem API, and I noticed that parts of the API that use PCWSTR (both function arguments and struct fields) are being treated as PWSTR (i.e. they mutable wide strings instead of const wide strings).

A simple example of this is the PRJ_GET_DIRECTORY_ENUMERATION_CB type, which is generated as follows with windows 0.9:

pub type PRJ_GET_DIRECTORY_ENUMERATION_CB =
    unsafe extern "system" fn(
        callbackdata: *const PRJ_CALLBACK_DATA,
        enumerationid: *const ::windows::Guid,
        searchexpression: super::SystemServices::PWSTR,
        direntrybufferhandle: PRJ_DIR_ENTRY_BUFFER_HANDLE,
    ) -> ::windows::HRESULT;

Note that the searchExpression argument is declared as a PWSTR in the generated Rust bindings, but the original definition declares it as a PCWSTR.

To reproduce this, you can create a build script that generates the ProjFS bindings:

windows::build!(
    Windows::Win32::ProjectedFileSystem::*,
);
@ennis
Copy link

ennis commented May 9, 2021

Bindings to DirectWrite have the same issue: for example, IDWriteFactory::CreateTextLayout, which takes a const WCHAR*:

HRESULT CreateTextLayout(
  WCHAR const       *string,
  UINT32            stringLength,
  IDWriteTextFormat *textFormat,
  FLOAT             maxWidth,
  FLOAT             maxHeight,
  IDWriteTextLayout **textLayout
);

gets translated into a function that takes a PWSTR (*mut u16):

pub unsafe fn CreateTextLayout<'a>(
                    &self,
                    string: impl ::windows::IntoParam<'a, super::SystemServices::PWSTR>,
                    stringlength: u32,
                    textformat: impl ::windows::IntoParam<'a, IDWriteTextFormat>,
                    maxwidth: f32,
                    maxheight: f32,
                    textlayout: *mut ::std::option::Option<IDWriteTextLayout>,
                ) -> ::windows::HRESULT { ... }

@kennykerr kennykerr added the question Further information is requested label May 10, 2021
@kennykerr
Copy link
Collaborator

Sorry for the delay in responding. The metadata that drives the Windows crate only defined PSTR and PWSTR - there's no const version. The const is translated into an attribute, either on the parameter or the field, that languages can then interpret as needed. The Windows crate already takes into account the const attribute on function parameters, but not structs or callbacks. That still remains to be mapped in some sensible way. I have some work coming that will deal with this more naturally, both const and optional (#816) attributes.

@kennykerr kennykerr added enhancement New feature or request and removed question Further information is requested labels May 24, 2021
@lzybkr
Copy link
Member

lzybkr commented Dec 10, 2021

@kennykerr - I found at least 1 parameter that should be const but is not: lpmultibytestr on MultiByteToWideChar:

    pub fn MultiByteToWideChar(codepage: u32, dwflags: MULTI_BYTE_TO_WIDE_CHAR_FLAGS, lpmultibytestr: super::Foundation::PSTR, cbmultibyte: i32, lpwidecharstr: super::Foundation::PWSTR, cchwidechar: i32) -> i32;

Version:

[dependencies.windows-sys]
version = "0.27.0"
default-features = false
features = ["Win32_Globalization", "Win32_Foundation"]

@kennykerr
Copy link
Collaborator

@lzybkr thanks for the reminder - I should be able to generate const in such cases.

@kennykerr
Copy link
Collaborator

This was recently fixed with #1468 and #1470

elmarco pushed a commit to elmarco/windows-rs that referenced this issue Feb 2, 2022
@kennykerr
Copy link
Collaborator

#1550 adds first-class types for PCSTR and PCWSTR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants