How does marshalling work?

c#
    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
Was this page helpful?