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

Inconsistent #[link] attribute #115721

Closed
ave9858 opened this issue Sep 9, 2023 · 2 comments
Closed

Inconsistent #[link] attribute #115721

ave9858 opened this issue Sep 9, 2023 · 2 comments
Labels
C-bug Category: This is a bug. O-windows Operating system: Windows

Comments

@ave9858
Copy link

ave9858 commented Sep 9, 2023

I tried this code:

lib.rs

#[link(name = "kernel32.dll", kind = "raw-dylib")]
extern "system" {
    pub fn GetStdHandle(nStdHandle: u32) -> *mut u8;
}

main.rs

use issue::GetStdHandle;

fn main() {
    unsafe {
        GetStdHandle(4294967286);
    }
}

I expected to see this happen: main.rs using lib.rs should compile and run without any issues.

Instead, this happened:

error: process didn't exit successfully: `target\debug\issue.exe` (exit code: 0xc0000135, STATUS_DLL_NOT_FOUND)

The following code works correctly:

main.rs

#[link(name = "kernel32.dll", kind = "raw-dylib")]
extern "system" {
    pub fn GetStdHandle(nStdHandle: u32) -> *mut u8;
}

fn main() {
    unsafe {
        GetStdHandle(4294967286);
    }
}

I expected the extern block to work the same in main.rs and lib.rs, but dumpbin shows that in the non-working code GetStdHandle is imported from kernel32.dll.dll:

Microsoft (R) COFF/PE Dumper Version 14.37.32822.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file .\issue.exe

File Type: EXECUTABLE IMAGE

  Section contains the following imports:

    kernel32.dll.dll
             14001C298 Import Address Table
             140024070 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                           0 GetStdHandle


rustc --version --verbose:

rustc 1.72.0 (5680fa18f 2023-08-23)
binary: rustc
commit-hash: 5680fa18feaa87f3ff04063800aec256c3d4b4be
commit-date: 2023-08-23
host: x86_64-pc-windows-msvc
release: 1.72.0
LLVM version: 16.0.5
@ave9858 ave9858 added the C-bug Category: This is a bug. label Sep 9, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Sep 9, 2023
@ave9858 ave9858 changed the title Title: Inconsistent #[link] attribute Inconsistent #[link] attribute Sep 9, 2023
@saethlin saethlin added O-windows Operating system: Windows and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Sep 10, 2023
@klensy
Copy link
Contributor

klensy commented Sep 10, 2023

You should use name = "kernel32" without dll part.
But RFC shows that with raw-dylib it should use full name with dll, hmm.

Found related microsoft/windows-rs#2431, should use *.dll with , modifiers = "+verbatim"?

@ChrisDenton
Copy link
Member

Yep, the name should be without the .dll extension unless modifiers = "+verbatim" is used.

@ChrisDenton ChrisDenton closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. O-windows Operating system: Windows
Projects
None yet
Development

No branches or pull requests

5 participants