Michael,
It is possible to get a buffer back without a string builder. Will this cause problems?
Yes it can, so don't do it. To demonstrate, consider this code instead
// ---
string buffer = new string('\0', 255);
string someOtherString = buffer;
int len = buffer.Length;
GetComputerName(buffer, ref len);
Console.WriteLine(buffer);
Console.WriteLine(someOtherString);
// ---
Most people wouldn't expect someOtherString to be modified when buffer
is, because even though System.String is a reference type, its
immutability effectively gives it value semantics.
What you're doing here is basicly to break the immutability guarantee
of strings, which is a bad thing, since some runtime string
optimizations rely on the fact that a string can't change.
This may not seem so bad in this example, where all the code is
contained in a single method. But keep in mind the string interning
feature of the runtime, and the fact that completely separate pieces
of code may reference the same string, and it could have serious side
effects.
But in the original poster's case, where the parameter isn't an
LPWSTR, but rather a BSTR*, it's probably safe to pass a string. Since
the parameter is passed by ref, you can expect that the callee returns
a new BSTR (which the runtime then marshals to a new string object)
rather than to modify the existing string.
Mattias
--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.