Unable to generate bindings using 'windows-binding' for a project around the Windows API & the 'windows' crate

Hello everyone,

I am having trouble building a simple program using some Windows API calls using the "windows" crate.

Issue : Bindgen fails to generate valid bindings.rs

I am looking to generate static bindings to resolve symbols within my Helix IDE which is not possible for the moment using the lastest version of the "windows" crate. So far, I downgraded the version to 0.52 which comes with imports out of the box.

The set-up is the following :

// Cargo.toml
[dependencies]
windows = "0.62.2"

[build-dependencies]
windows-bindgen = "0.65.0"
// bindings.rs
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {

    let args = [
        "--out",
        "src/bindings.rs", // Path where bindings are stored,
        "--filter",        // Windows namespaces you want to generate
        "Windows.Win32.System.Threading",
        "Windows.Win32.Foundation",
        "Windows.Win32.System.Console",
        "Windows.Win32.System.Com",
    ];

    // Generate bindings
    windows_bindgen::bindgen(&args);

    // Tell Cargo to re-run build.rs if you change the list
    println!("cargo:rerun-if-changed=build.rs");

    Ok(())
}

During the building phase, the rust compiler throws an large number of ' use of unresolved module or unlinked crate `windows_*'. Having skimmed though the changelog on the release section of the crate, I did notice that a lot reworking has been done between the 0.5X version to the 0.62 version, though I am not expirimented enough to fully understand that changes I need to apply to even make a single windows pop.

Here is an excerpt of the errors thrown :


error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16426:21
      |
16426 |                     windows_core::link!("ole32.dll" "system" fn CoTestCancel() -> windows_core::HRESULT);
      |                     ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16434:21
      |
16434 |                     windows_core::link!("ole32.dll" "system" fn CoTreatAsClass(clsidold : *const windows_core::GUID, clsidnew : *const windows_core::GUID) -> windows_core::HRESULT);
      |                     ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16439:21
      |
16439 |                     windows_core::link!("ole32.dll" "system" fn CoUninitialize());
      |                     ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16448:21
      |
16448 | ...   windows_core::link!("ole32.dll" "system" fn CoWaitForMultipleHandles(dwflags : u32, dwtimeout : u32, chandles : u32, phandles : *const super::super::Foundation:: HANDLE, lpdwindex : *mut u32) -...
      |       ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16467:21
      |
16467 | ...   windows_core::link!("ole32.dll" "system" fn CoWaitForMultipleObjects(dwflags : u32, dwtimeout : u32, chandles : u32, phandles : *const super::super::Foundation:: HANDLE, lpdwindex : *mut u32) -...
      |       ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16482:21
      |
16482 |                     windows_core::link!("ole32.dll" "system" fn CreateAntiMoniker(ppmk : *mut * mut core::ffi::c_void) -> windows_core::HRESULT);
      |                     ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16491:21
      |
16491 |                     windows_core::link!("ole32.dll" "system" fn CreateBindCtx(reserved : u32, ppbc : *mut * mut core::ffi::c_void) -> windows_core::HRESULT);
      |                     ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16502:21
      |
16502 |                     windows_core::link!("ole32.dll" "system" fn CreateClassMoniker(rclsid : *const windows_core::GUID, ppmk : *mut * mut core::ffi::c_void) -> windows_core::HRESULT);
      |                     ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16511:21
      |
16511 |                     windows_core::link!("ole32.dll" "system" fn CreateDataAdviseHolder(ppdaholder : *mut * mut core::ffi::c_void) -> windows_core::HRESULT);
      |                     ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16527:21
      |
16527 | ...   windows_core::link!("ole32.dll" "system" fn CreateDataCache(punkouter : * mut core::ffi::c_void, rclsid : *const windows_core::GUID, iid : *const windows_core::GUID, ppv : *mut *mut core::ffi::...
      |       ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16541:21
      |
16541 |                     windows_core::link!("ole32.dll" "system" fn CreateFileMoniker(lpszpathname : windows_core::PCWSTR, ppmk : *mut * mut core::ffi::c_void) -> windows_core::HRESULT);
      |                     ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16557:21
      |
16557 | ...   windows_core::link!("ole32.dll" "system" fn CreateGenericComposite(pmkfirst : * mut core::ffi::c_void, pmkrest : * mut core::ffi::c_void, ppmkcomposite : *mut * mut core::ffi::c_void) -> window...
      |       ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `windows_core`
     --> foo/src/bindings.rs:16577:21
      |
16577 | ...   windows_core::link!("urlmon.dll" "system" fn CreateIUriBuilder(piuri : * mut core::ffi::c_void, dwflags : u32, dwreserved : usize, ppiuribuilder : *mut * mut core::ffi::c_void) -> windows_core:...
      |       ^^^^^^^^^^^^ use of unresolved module or unlinked crate `windows_core`

Any advice is much appreciated for I am lost in limbo ...

Please post the complete text of at least one of these errors (starting from error: and ending at the blank line). rustc errors contain many pieces of information which are valuable in identifying what the problem is, especially for us who are not operating your computer directly.

rust-analyzer will not be able to work until the build of your dependencies succeeds.

Hello, thanks for the reply. The issue I am facing is during the binding generation using "windows-bindgen" to produce import symbols of the latest version of the "windows" crate for my IDE. It appears I have completely omitted this part. I will rewrite this topic with a better overview of the issue I am facing.

I’m not familiar with how windows or windows-bindgen works, but this error seems straightforward enough: the generated code requires windows-core, but you haven’t declared that dependency, only a dependency on windows.

That said, the documentation for windows-bindgen says that you should be adding a dependency on windows-link, not windows-core, which is inconsistent with your result. Maybe the documentation is out of date, or for a slightly different situation?


Also, this is not part of your build failure, but your build script is misbehaving:

    let args = [
        "--out",
        "src/bindings.rs", // Path where bindings are stored,

Build scripts should not write files anywhere outside of $OUT_DIR. Trying to write elsewhere, particularly into src/, may result in conflicts between different projects (because sources are shared between different build configurations) or errors (if the source directory is set up to be read-only, which a user or Cargo would be within their rights to do). It’s not especially harmful when you are developing the code itself, but it must not go into a package that anyone else is going to depend on, whether via crates.io or git.

Write your generated code into $OUT_DIR, then use #[path = concat!(env!("OUT_DIR"), "bindings.rs")] mod bindings; to load it from there.

You shouldn't need windows-bindgen. The windows crate alrady contains all the bindings generated.

You only need to find the features to activate using the Feature search

From what I understand, the bindings are generated at compile time. Which would make any code using the windows crate functional if written correctly.

The thing is, I really would like some kind of LSP support for my IDE during coding. Otherwise it looks quite painful.

It hasn’t worked like this for a few years. As R081n said, you should just use the windows or windows-sys crate as a dependency with the right features selected, both of which have all the normal APIs pregenerated in different styles.

There’s an up to date Getting Started guide with more details.

1 Like

Ok I'll look into it thanks a lot.

I have followed the guide to generate standalone bindings as described here to get some autocomplete in Helix, it doesn't work otherwise : Standalone code generation - Kenny Kerr

So far, I have written a build scripts that generate bindings based on the activated features of the windows crate present in the Cargo.toml file.

What I am struggling with right now is the type resolution in my IDE for common Windows API types such as PWSTR, LPTR, etc that should be present in windows_core::* or windows_core::*

Thank you for your guidance.

I’m not sure why autocomplete and type resolution wouldn’t be working for you. I’ve turned an old example into a Cargo project, available from carey/flushdns, and everything seems to work properly with Helix on Windows without any code generation.

The only thing that needed fixing was to install rust-analyzer first:

rustup component add rust-analyzer