Skip to content

Conversation

ophura
Copy link
Contributor

@ophura ophura commented Sep 29, 2025

Summary

this change adds the out keyword to fix such compile-time error as well as inlines the variable declaration for simplicity and renames the variable from st to systemTime for both clarity and consistency.

additionally, it removes the string[] args parameter from the method signature as it isn't required and also unused.


Internal previews

📄 File 🔗 Preview link
docs/standard/native-interop/type-marshalling.md Type marshalling

this change adds the `out` keyword to fix such compile-time error as well as inlines the variable declaration for simplicity and renames the variable from `st` to `systemTime` for both clarity and consistency.
@ophura ophura requested review from jkoritzinsky and a team as code owners September 29, 2025 18:48
@dotnetrepoman dotnetrepoman bot added this to the September 2025 milestone Sep 29, 2025
@dotnet-policy-service dotnet-policy-service bot added dotnet-fundamentals/svc community-contribution Indicates PR is created by someone from the .NET community. labels Sep 29, 2025
Copy link
Contributor

@adegeo adegeo left a comment

Choose a reason for hiding this comment

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

Thank you for this improvement! Do you think you can improve it some more? As it is, you can't really even copy-paste this code into a code file and have it work, there are a few errors such as it has to be declared in a partial class and a scope modifier needs to be added too.

If not, I'll take your improvement and iterate on it myself, then merge.

Thanks again!

@ophura
Copy link
Contributor Author

ophura commented Sep 29, 2025

I'll do just that sensei > ~ <

@adegeo
Copy link
Contributor

adegeo commented Sep 29, 2025

It doesn't have to be "Main" it could just be a utility class with a method named PrintSystemTime() => Console.WriteLine... or something like that.

this change makes the code block ready-to-run for ease of use with copying.
@ophura
Copy link
Contributor Author

ophura commented Sep 29, 2025

@dotnet-policy-service agree

Copy link
Contributor

@adegeo adegeo left a comment

Choose a reason for hiding this comment

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

Beautiful! Thanks!

@adegeo adegeo enabled auto-merge (squash) September 29, 2025 19:38
@adegeo adegeo merged commit 36487da into dotnet:main Sep 29, 2025
8 checks passed
@ophura
Copy link
Contributor Author

ophura commented Sep 29, 2025

@adegeo I also considered other changes, regarding the first block, here's what I had in mind:

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

internal static partial class Interop
{
    [LibraryImport("SomeNativeLibrary.dll")]
    internal static partial int MethodA([MarshalAs(UnmanagedType.LPStr)] string parameter);

    // or

    [LibraryImport("SomeNativeLibrary.dll")]
    internal static partial int MethodB([MarshalAs(UnmanagedType.LPUTF8Str)] string parameter);

    // or

    [LibraryImport("SomeNativeLibrary.dll")]
    internal static partial int MethodC([MarshalUsing(typeof(Utf8StringMarshaller))] string parameter);

    // or

    [LibraryImport("SomeNativeLibrary.dll", StringMarshalling = StringMarshalling.Utf8)]
    internal static partial int MethodD(string parameter);
}

this is to complete the set of options originally proposed. I'd also like to note that one of the originally suggested approaches behaves differently in semantics, and that is UnmanagedType.LPStr; which relies on AnsiStringMarshaller, whereas the others use Utf8StringMarshaller for marshaling.

that is worrisome to me since the documentation states that:

if you want the string to be sent as a null-terminated UTF-8 string, you could do it like this:

followed by the code block, which again, the first method in it uses the AnsiStringMarshaller...
should MethodA be excluded in this case?

@adegeo
Copy link
Contributor

adegeo commented Sep 29, 2025

@ophura would you mind opening an issue with your suggestion? This way we can tag the engineer and get a discussion going. Go to https://learn.microsoft.com/en-us/dotnet/standard/native-interop/type-marshalling (you won't see your updates just yet) and use the link at the bottom of the page to give feedback on GitHub. This will create an issue and associate with that article.

@ophura
Copy link
Contributor Author

ophura commented Sep 29, 2025

@adegeo done #48785

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community. dotnet-fundamentals/svc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants