Skip to content

Inconsistent #[link] attribute #115721

@ave9858

Description

@ave9858

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-windowsOperating system: Windows

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions