Skip to content

Fix incorrect character encoding in GetLastErrorAsString on Windows#323

Closed
soutong wants to merge 2 commits intoduckdb:mainfrom
soutong:fix/windows-error-message-utf8
Closed

Fix incorrect character encoding in GetLastErrorAsString on Windows#323
soutong wants to merge 2 commits intoduckdb:mainfrom
soutong:fix/windows-error-message-utf8

Conversation

@soutong
Copy link

@soutong soutong commented Jul 28, 2025

Summary

This PR fixes a character encoding issue on Windows where error messages retrieved using GetLastError() and FormatMessageA may be garbled when non-ASCII characters are involved.

Changes

  • Replaced FormatMessageA with FormatMessageW to retrieve wide-character error messages.
  • Added conversion from std::wstring to UTF-8 using WindowsUtil::UnicodeToUTF8.
  • Updated return logic to handle empty messages safely.

Why

On localized Windows systems, FormatMessageA can return garbled messages when using non-ASCII locale (e.g., Chinese). This patch ensures proper Unicode support.

@staticlibs
Copy link
Collaborator

Hi, thanks for the fix! It looks good to me. Just to be integrated it needs to be submitted to the https://github.com/duckdb/duckdb repo. src/duckdb directory in JDBC is imported from the main repo periodically. If you submit it to duckdb/duckdb - I can review it promptly. Or, if that is not convenient, I can submit it there myself?

@soutong
Copy link
Author

soutong commented Jul 29, 2025

Hi, thanks for the heads-up! I've already opened a PR to duckdb:main — feel free to take a look whenever you get a chance. Appreciate your help! duckdb/duckdb#18431

@staticlibs
Copy link
Collaborator

@soutong

Thanks for moving it to the main repo! I've commented there and added one minor suggestion. I'll close this PR in favour of the duckdb/duckdb#18431 one.

@staticlibs staticlibs closed this Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants