NETCF 2 Beta StringBuilder incompatibility

  • Thread starter Thread starter Andreas Selle
  • Start date Start date
A

Andreas Selle

Hi,

I guess I found some small incompatibility with Beta 2 NETCF V2.

I have the following P/Invoke scenario: I create a StringBuilder instance
with a large enough capacity and pass it to a native function that fills
this buffer with multiple adjacent nul-terminated strings and returns the
total count of characters (including nul characters) that have been stored
in the buffer. After the call I convert the StringBuffer to a String. The C#
code looks like this:

StringBuilder mszBuffer = new StringBuilder(2000);
int cch = MyNativeFunction(mszBuffer)
string msz = mszBuffer.ToString(0, cch);

This used to work with the old NETCF V1 but now the call to ToString()
throws an ArgumentOutOfRange exception. Probably ToString() now checks the
given cch against mszBuffer.Length, however mszBuffer.Length only returns
the length of the first string that was added to the buffer.

Hope that my explanation was clear enough.

Cheers,

Andreas
 
I don't know where the problem is.

My suggestion is you look at StringBuilder using a decompiler (such as
reflector) to spot the difference between v1 and v2. If there isn't one then
the difference is with the pinvoke layer (v1 vs v2).

In any case, here is a documented difference for the desktop version that
may apply to the CF version of StringBuilder (apologies if it is not
relevant):
http://blogs.msdn.com/bclteam/archive/2005/02/17/375560.aspx

Cheers
Daniel
 
Hi,

thanks for the pointer to the article. I am not sure whether it applies to
my reported problem. Actually the ArgumentOutOfRange exception is thrown by
String.Substring which is called by StringBuilder.ToString.

Meanwhile I changed my code to use a simple char[] array instead of a
StringBuilder instance. Now it works on both 1.0 and 2.0 beta 2.

Andreas
 
Glad you got around it in the end... consider reporting it to ladybug as I
know the CF team are very interested in compatibility...

Cheers
Daniel
--
http://www.danielmoth.com/Blog/


Andreas Selle said:
Hi,

thanks for the pointer to the article. I am not sure whether it applies to
my reported problem. Actually the ArgumentOutOfRange exception is thrown
by String.Substring which is called by StringBuilder.ToString.

Meanwhile I changed my code to use a simple char[] array instead of a
StringBuilder instance. Now it works on both 1.0 and 2.0 beta 2.

Andreas
 
Back
Top