Skip to content

[API Proposal]: Marshal.GetPInvokeErrorMessage() overload for localization #72546

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

Closed
AaronRobinsonMSFT opened this issue Jul 20, 2022 · 11 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime.InteropServices
Milestone

Comments

@AaronRobinsonMSFT
Copy link
Member

AaronRobinsonMSFT commented Jul 20, 2022

Background and motivation

Follow-up on #67872 for adding a localization overload.

The utility of this function is often for developer to log. However, the localization is dependent on the user's machine configuration, not the developers. It would be helpful for people for their logging utilities to be able to specify that locale and not rely on the user's machine.

User related conversation #40427 (comment)

API Proposal

namespace System.Runtime.InteropServices;

public partial class Marshal
{
    // Existing
    // public static int GetLastPInvokeError();
    // public static string GetLastPInvokeErrorMessage();
    // public static string GetPInvokeErrorMessage(int error);
+    public static string GetPInvokeErrorMessage(int error, CultureInfo cultureInfo);
}

There doesn't seem to be a strong need for an overload for public static string GetLastPInvokeErrorMessage(CultureInfo cultureInfo);, but that is an option.

API Usage

string presentToUser = Marshal.GetPInvokeErrorMessage(error, CultureInfo.CurrentUICulture);
string logForDev = Marshal.GetPInvokeErrorMessage(error, CultureInfo.CreateSpecificCulture("en-us"));

Alternative Designs

No response

Risks

No response

@AaronRobinsonMSFT AaronRobinsonMSFT added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime.InteropServices labels Jul 20, 2022
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 8.0.0 milestone Jul 20, 2022
@ghost
Copy link

ghost commented Jul 20, 2022

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

Follow-up on #67872 for adding a localization overload.

User related conversation #40427 (comment)

The utility of this function is often for developer to log. However, the localization is dependent on the user's machine configuration, not the developers. It would be helpful for people for their logging utilities to be able to specify that locale and not rely on the user's machine.

API Proposal

namespace System.Runtime.InteropServices;

public partial class Marshal
{
    // Existing
    // public static int GetLastPInvokeError();
    // public static string GetLastPInvokeErrorMessage();
    // public static string GetPInvokeErrorMessage(int error);
+    public static string GetPInvokeErrorMessage(int error, CultureInfo cultureInfo);
}

API Usage

string presentToUser = Marshal.GetPInvokeErrorMessage(error, CultureInfo.CurrentUICulture);
string logForDev = Marshal.GetPInvokeErrorMessage(error, CultureInfo.CreateSpecificCulture("en-us"));

Alternative Designs

No response

Risks

No response

Author: AaronRobinsonMSFT
Assignees: -
Labels:

api-suggestion, area-System.Runtime.InteropServices

Milestone: 8.0.0

@stephentoub
Copy link
Member

What does this do on Unix?

What does this do if the relevant resources aren't available?

Is CultureInfo the right thing to accept?

@AaronRobinsonMSFT
Copy link
Member Author

What does this do on Unix?
What does this do if the relevant resources aren't available?

It would be dependent on if the platform supports localized error messages. If the platform doesn't or if resources aren't available, the behavior would be identical to the non-localized API.

Is CultureInfo the right thing to accept?

I defer to @tarekgh and @ericstj for that.

@jkotas
Copy link
Member

jkotas commented Jul 20, 2022

It would be dependent on if the platform supports localized error messages.

This would work only on Windows and only if the machine has multiple language packs installed that I believe is relatively rare.

@jkotas
Copy link
Member

jkotas commented Jul 20, 2022

It may make more sense to change the current GetPInvokeErrorMessage(int error) API to respect CultureInfo.CurrentUICulture if possible, to match the behavior of regular exception messages.

@tarekgh
Copy link
Member

tarekgh commented Jul 20, 2022

The original issue triggered this proposal is how to get the English message when running on Windows localized version. This API can help in that scenario, but I agree with Jan it is limited to such specific scenario. May be better to think in other way achieving the same without such API?

@jkotas
Copy link
Member

jkotas commented Jul 20, 2022

The original issue triggered this proposal is how to get the English message when running on Windows localized version.

Are English resources guaranteed to be available on Windows localized versions? IIRC, I do not think they are out of the box.

@tarekgh
Copy link
Member

tarekgh commented Jul 20, 2022

I am not fully sure, but English resources should always exist. We can validate that.

@tarekgh
Copy link
Member

tarekgh commented Jul 20, 2022

I confirmed with Windows team that it is not guaranteed the English resources will exist all the time.

@tarekgh
Copy link
Member

tarekgh commented Jul 20, 2022

I guess after this discovery I suggest closing this proposal. Thanks @AaronRobinsonMSFT for being proactive and trying to help solve the issue.

@ericstj
Copy link
Member

ericstj commented Jul 21, 2022

No need to reopen, but I found this could be helpful if the language pack is installed. Not sure how many end users will be running with multiple languages. Here's a gist of what I did to help get Win32Exceptions in other languages. It's a shameless copy of this API https://gist.github.com/ericstj/c72b90b0c12f86b7918850ee276fac3b

@ghost ghost locked as resolved and limited conversation to collaborators Aug 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime.InteropServices
Projects
None yet
Development

No branches or pull requests

5 participants