© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•5mo ago•
124 replies
☠ Pointman ☠

How does marshalling work?

    public static partial int MessageBoxW(nint hWnd, string text, string caption, uint type)
    {
        int __retVal;
        // Pin - Pin data in preparation for calling the P/Invoke.
        fixed (void* __caption_native = &global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.GetPinnableReference(caption))
        fixed (void* __text_native = &global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.GetPinnableReference(text))
        {
            __retVal = __PInvoke(hWnd, (ushort*)__text_native, (ushort*)__caption_native, type);
        }
    public static partial int MessageBoxW(nint hWnd, string text, string caption, uint type)
    {
        int __retVal;
        // Pin - Pin data in preparation for calling the P/Invoke.
        fixed (void* __caption_native = &global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.GetPinnableReference(caption))
        fixed (void* __text_native = &global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.GetPinnableReference(text))
        {
            __retVal = __PInvoke(hWnd, (ushort*)__text_native, (ushort*)__caption_native, type);
        }


Hey lads, as you can see in this [LibraryImport] generated code it's calling a native function by just getting the address to a string.. But don't native libraries need strings to be null terminated? I don't see any code within GetPinnableReference() that suggests it makes a character array with a '\0' at the end
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Marshalling bool
C#CC# / help
2y ago
Strange Marshalling issue
C#CC# / help
2y ago
How does Dapper work
C#CC# / help
2y ago
✅ How does Bot work?
C#CC# / help
2y ago