I'm using C# and providing a callback function to an unmanaged C++
function that uses the callback to send back a character string. It
works find if the string sent through the callback is less than
StringBuilder's default capacity (16 char). If it is greater, then C#
stops and issues a "capacity is greater than maximum capacity" error.
Is there a way to increase the StringBuilder capacity when it's used as
part of the parameter list of a callback function? It's different than a
call to a function like GetWindowText() where you can define the
StringBuilder instance before passing it to GetWindowText. I don't see
where I can preallocate the StringBuilder capacity when the C++ function
calls the callback routine.
Thanks,
Ed
Here's the boiled-down version of the code:
public delegate int MyCallback( StringBuilder stuff );
[DllImport ("MyDll.dll", CharSet=CharSet.Ansi)]
public static extern unsafe int FetchStuff (
,MyCallback myCallBack );
private static string myString;
public unsafe static int CallBackFunction(StringBuilder stuff)
{
string myString = stuff.ToString();
...
}
public class Main
{
MyCallback callBack= new MyCallback (Form1.CallBackFunction);
int return;
return = FetchStuff (callBack);
}
Mattias Sj?gren - 24 Oct 2003 11:05 GMT
Ed,
This is a bug and according to
FIX: Call to Managed Class Method with StringBuilder as In or Out
Parameter May Fail
http://support.microsoft.com/?kbid=317577
it's fixed in v1.1 of the framework. If you're stuck with v1.0, you
could change the parameter type to an IntPtr and then use the Marshal
class to retrieve the string.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.