Using String Buffers of Win32 Libraries in C#
-
Hi, everyone For functions in Win32 libraries that will copy a string value into a string buffer, I try to use string. It works. But recently, I read an article about this, it suggests to use StringBuilder instead of string. Because string type is an immutable type in .Net. If using string for string buffer, it could corrupt the temporary buffer created by the marshaler when a string is translated or corrupt the managed heap. But my program works pretty good and I saw some other people's code is doing the same thing as min. Can anyone give me more ideas about this? Thanks in advance.
-
Hi, everyone For functions in Win32 libraries that will copy a string value into a string buffer, I try to use string. It works. But recently, I read an article about this, it suggests to use StringBuilder instead of string. Because string type is an immutable type in .Net. If using string for string buffer, it could corrupt the temporary buffer created by the marshaler when a string is translated or corrupt the managed heap. But my program works pretty good and I saw some other people's code is doing the same thing as min. Can anyone give me more ideas about this? Thanks in advance.
I've been using
string
almost every time without problem, and so does Microsoft most of the time in the Framework Class Library (FCL) for various native API P/Invoke declarations and struct fields. It wouldn't hurt to use aStringBuilder
instead, but so long as you take care of allocating and freeing your strings appropriately (when necessary), you probably should be fine.Microsoft MVP, Visual C# My Articles