Skip to content

Win: Add GetHostNameW fallback for win7 using gethostname#150909

Open
Fulgen301 wants to merge 1 commit into
rust-lang:mainfrom
Fulgen301:win7-gethostname
Open

Win: Add GetHostNameW fallback for win7 using gethostname#150909
Fulgen301 wants to merge 1 commit into
rust-lang:mainfrom
Fulgen301:win7-gethostname

Conversation

@Fulgen301
Copy link
Copy Markdown
Contributor

@Fulgen301 Fulgen301 commented Jan 10, 2026

View all comments

GetHostNameW is only available starting with Windows 8, but gethostname has been available since Windows Vista. Use it as a fallback and convert the result from ANSI to Unicode.

Fixes #150896

This also replaces #150905 - I can rebase it onto the other PR should that one get merged first.

@rustbot rustbot added O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 10, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jan 10, 2026

r? @ChrisDenton

rustbot has assigned @ChrisDenton.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Comment thread library/std/src/sys/pal/windows/c.rs Outdated
Comment thread library/std/src/sys/pal/windows/winsock.rs
Comment thread library/std/src/sys/pal/windows/winsock.rs Outdated
Comment thread library/std/src/sys/pal/windows/winsock.rs Outdated
Comment thread library/std/src/sys/pal/windows/winsock.rs Outdated
Comment thread library/std/src/sys/pal/windows/winsock.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 12, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jan 12, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@ChrisDenton
Copy link
Copy Markdown
Member

ChrisDenton commented Jan 12, 2026

cc @roblabla @PaulDance it would seem a bit silly to merge #150905 then immediately merge this PR. Do you have any thoughts on this PR.

@roblabla
Copy link
Copy Markdown
Contributor

cc @roblabla @PaulDance it would seem a bit silly to merge #150905 then immediately merge this PR. Do you have any thoughts on this PR.

Yes, this PR obsoletes #150905, and seems like the right approach to me.

Comment thread library/std/src/sys/pal/windows/winsock.rs
@PaulDance
Copy link
Copy Markdown
Contributor

it would seem a bit silly to merge #150905 then immediately merge this PR

Yes, it is, but I thought doing so would have the advantage of getting a quick and easy fix known to work merged fast while a more comprehensive but complex one would be implemented with ample time concurrently. Considering the above comments, it kind of confirms that. Either way works in the end, though.

Comment thread library/std/src/sys/pal/windows/winsock.rs Outdated
pub static WS2_32: &CStr = c"ws2_32";

#[cfg(target_vendor = "win7")]
pub fn GetHostNameW(name: PWSTR, namelen: i32) -> i32 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it could be nice to link the original issue somewhere around here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left that out for now - none of the other compat functions have an issue linked to them, and the issue in question was "just" a compatibility issue.

@rust-bors

This comment has been minimized.

@PaulDance
Copy link
Copy Markdown
Contributor

This now needs to be rebased on top of #150905. Some its changes need to be integrated or reverted:

  • the one in library/std/src/sys/net/hostname/mod.rs should be reverted;
  • the documentation in library/std/src/net/hostname.rs should be updated in order to indicate the function used under Win7;
  • the fallback logic needs to be integrated;

@SpriteOvO
Copy link
Copy Markdown
Member

@Fulgen301 Hi, ping from triage team. This PR has been inactive for a while. There are still some review comments that need to be addressed. Would you like to proceed with this PR? Thanks.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 15, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Fulgen301
Copy link
Copy Markdown
Contributor Author

@SpriteOvO really sorry it took so long, I implemented the requested changes.

@Fulgen301 Fulgen301 force-pushed the win7-gethostname branch 2 times, most recently from 759dad1 to 68fa86f Compare May 15, 2026 01:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-windows Operating system: Windows S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

std's tests fail to dynamically link under Windows 7 due to GetHostNameW missing

9 participants