Marshalling Strings
Platform invoke copies string parameters, converting them from the .NET Framework format (Unicode) to the unmanaged format (ANSI), if needed. Because managed strings are immutable, platform invoke does not copy them back from unmanaged memory to managed memory when the function returns.
The following table lists marshalling options for strings, describes their usage, and provides a link to the corresponding .NET Framework sample.
String | Description | Sample |
---|---|---|
By value. | Passes strings as In parameters. | MsgBox |
As result. | Returns strings from unmanaged code. | Strings |
By reference. | Passes strings as In/Out parameters using StringBuilder. | Buffers |
In a structure by value. | Passes strings in a structure that is an In parameter. | Structs |
In a structure by reference (char*). | Passes strings in a structure that is an In/Out parameter. The unmanaged function expects a pointer to a character buffer and the buffer size is a member of the structure. | Strings |
In a structure by reference (char[]). | Passes strings in a structure that is an In/Out parameter. The unmanaged function expects an embedded character buffer. | OSInfo |
In a class by value (char*). | Passes strings in a class (a class is an In/Out parameter). The unmanaged function expects a pointer to a character buffer. | OpenFileDlg |
In a class by value (char[]). | Passes strings in a class (a class is an In/Out parameter). The unmanaged function expects an embedded character buffer. | OSInfo |
As an array of strings by value. | Creates an array of strings that is passed by value. | Arrays |
As an array of structures that contain strings by value. | Creates an array of structures that contain strings and the array is passed by value. | Arrays |
See also
Cộng tác với chúng tôi trên GitHub
Bạn có thể tìm thấy nguồn cho nội dung này trên GitHub, nơi bạn cũng có thể tạo và xem lại các vấn đề và yêu cầu kéo. Để biết thêm thông tin, hãy xem hướng dẫn dành cho người đóng góp của chúng tôi.