Open TrebuchKill opened 11 months ago
See rustc src/lib.rs --print native-static-libs --crate-type staticlib
. It should print something like:
note: native-static-libs: advapi32.lib bcrypt.lib kernel32.lib msvcrt.lib ntdll.lib userenv.lib ws2_32.lib
You will need to add these import libraries to your C project. Some will be included by default, others won't. Though the linker errors will only happen if your program actually tries to make use of the missing symbols so not all of these libs will be strictly necessary.
Adding ntdll.lib
made the example link. Thank you @ChrisDenton.
Before posting this issue, one of the docs I checked was the NtCreateFile docs, which specifies no LIB file. At the beginning of this page is a note linking to Calling Internal APIs, which mentions, that no import library is provided. This is why I didn't look elsewhere if such import library exists.
Yeah, the docs are fairly outdated. ntdll.lib
has been distributed with the Windows SDK for over a decade at this point.
Rust itself tends not to use Nt*
functions but there are a few places where it's used to avoid UB or security issues. For example, someone from Azure noted that stdout (or any File
for that matter) may or may not be an overlapped handle and may or may not be accessed concurrently from multiple threads. Therefore they suggested we use NtWriteFile
to make sure we can address any possible scenario. Another example is fixing CVE-2022-21658 where remove_dir_all
following a path may be tricked into following a symlink and deleting arbitrary files. NtCreateFile
was used to address this because it allows specifying a base directory handle, thus avoiding the need to traverse the full path each time it deletes something.
Before posting this issue, one of the docs I checked was the NtCreateFile docs, which specifies no LIB file.
The docs you linked to actually say:
The associated import library, NtDll.lib is available in the WDK [...]
But it's hard to see, I'll submit an edit to add it to the information block at the bottom like all the other APIs.
@riverar Note that ntdll.lib is also available in the SDK too (as of Windows 8/8.1) which kind of undermines the current rationale given in Calling Internal APIs, I guess it could be written to highlight the positive aspects of using the Win32 API vs. NT API (take advantage of new features without changing your code!) but that's a bigger job.
@ChrisDenton Agreed, removing that sentence about the WDK too.
Tracking: https://github.com/MicrosoftDocs/sdk-api/pull/1738
Code
I tried this project:
Cargo.toml:
rust-toolchain.toml:
src/lib.rs:
src/main.cpp:
build.ps1:
build.ps1
is not required, but it was helpful for testing and documents how I'm building the code.I expected to see this happen:
A C++ application linking against
example.lib
links & runs successfully.Instead, this happened:
Linking errors to internal Windows APIs:
Version it worked on
It most recently worked on:
Version with regression
First noticed with:
Lowest version tested which did not work:
Versions not listed in the
rust-toolchain.toml
example above where not tested.