Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2003

Tip: Looking for answers? Try searching our database.

Default StringBuilder Capacity and Callbacks

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ed Power - 23 Oct 2003 22:02 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.