BUG: StringBuilder Field in a Structure Cannot Be Marshaled Properly (327109)
The information in this article applies to:
- Microsoft .NET Framework 1.0
- Microsoft Windows .NET Framework 1.1
This article was previously published under Q327109 SYMPTOMS If you marshal a structure that contains a StringBuilder field to an unmanaged function, the StringBuilder field is not marshaled correctly. RESOLUTION If you must initialize a buffer to some value before you
pass to an unmanaged function, use one of the following workarounds:
- Use the StringBuilder class, and apply to ANSI and Unicode strings. For example:
StringBuilder buffer = new StringBuilder("content", 100);
buffer.Append((char)0);
buffer.Append('*', buffer.Capacity - 8); //add anything to the end
MyStrStruct mss;
mss.buffer = buffer.ToString();
mss.size = mss.buffer.Length;
Win32.TestStringInStruct(ref mss); // call unmanaged function
- Use the Marshal class, and apply only to Unicode strings. For example:
IntPtr pMem = Marshal.AllocCoTaskMem(100);
String inString = "content";
Marshal.Copy(inString.ToCharArray(), 0, pMem, inString.Length);
MyStrStruct mss;
mss.buffer = pMem;
mss.size = 100;
Win32.TestStringInStruct(ref mss); // call unmanaged function
String outString = Marshal.PtrToStringAuto(mss.buffer);
Marshal.FreeCoTaskMem(pMem);
STATUSMicrosoft has confirmed that this is a bug in the Microsoft
products that are listed at the beginning of this article.
Modification Type: | Minor | Last Reviewed: | 5/28/2003 |
---|
Keywords: | kbbug kbCOMInterop kbnofix KB327109 |
---|
|