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 / New Users / November 2006

Tip: Looking for answers? Try searching our database.

Custom StringBuilder Marshaling

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Olivier Matrot - 21 Nov 2006 14:46 GMT
Hello,
I'm in the process of writing a managed c++ wrapper class to use existing
unmanaged C++ code.
So I have to deal with custom marshaling.
Given a StringBuilder, I need to produce a LPTSTR to call unmanaged code,
and get back a modified StringBuilder on return. How Can I safely access the
internal StringBuilder buffer ?
TIA.
Mattias Sjögren - 21 Nov 2006 20:12 GMT
Olivier,

>Given a StringBuilder, I need to produce a LPTSTR to call unmanaged code,
>and get back a modified StringBuilder on return.

That sounds like the default marshaling for a StringBuilder. Can't you
just pass the StringBuilder as is and let the CLR handle the rest?

Mattias

Signature

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

Olivier Matrot - 22 Nov 2006 07:42 GMT
Mattias,
You're right, but I'm not going to use DllImport so, I need to do it by
hand.

> Olivier,
>
[quoted text clipped - 5 lines]
>
> Mattias
Walter Wang [MSFT] - 22 Nov 2006 13:12 GMT
Hi Olivier,

Based on my understanding, your scenario is:
1) You're writing a wrapper in managed C++ to wrap some unmanaged code
2) The input is a StringBuilder, the outout is also a StringBuilder
3) You don't want to use DllImport to do the marshaling of StringBuilder

Please correct me if I've misunderstood anything.

I believe you could use PtrToStringChars() to access the internal wchar_t*
of a managed string. For the reverse part, you could use
Marshal::PtrToStringAnsi or Uni to convert from ANSI or Unicode to a
managed string.

#StringConvertor : A convertor class for managed-unmanaged string
conversions that handles memory de-allocations - The Code Project - Managed
C++
http://www.codeproject.com/managedcpp/StringConvertor.asp
A convertor class for managed-unmanaged string conversions that handles
memory de-allocations. Caller need not worry about freeing unmanaged memory
allocations.

#I Love that New Syntax Smell : Pointer to String chars - Everett style
http://blogs.msdn.com/arich/archive/2003/12/22/45219.aspx
Can I get a native pointer to the data in a CLR String?  The short answer
is yes, so long as you don't mind a wchar_t* - which is native analog of
the actual backing store type for a CLR String (the CLR type System::Char).

#I Love that New Syntax Smell : Pinning Pointers
http://blogs.msdn.com/arich/archive/2004/08/27/221588.aspx
Pinning Pointers

#Managed C++ - Learn by Example - Part 1 - The Code Project - Managed C++
http://www.codeproject.com/managedcpp/cpptomancpp.asp
VC++ provides a helper function, PtrToStringChars(), defined in "vcclr.h"
that allows you to access the internal wchar_t* of a managed string.

Sincerely,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Olivier Matrot - 22 Nov 2006 14:58 GMT
Hi Walter,

You understand correctly. The problem is that PtrToStringChars is limited to
String, and I'm working with a stringBuilder. The unmanaged code must get a
buffer that is sized to the StringBuilder capacity ! PtrToStringChars will
work but will return a buffer sized to the actual length of the string which
is not what I need.

Ultimately, I could write an unmanaged DLL with a function that takes a
StringBuilder and get back the unmanaged buffer (this is done automatically
during DllImport interrop). Sure there is a more elegant way to achieve the
same result, in managed C++.

> Hi Olivier,
>
[quoted text clipped - 63 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
Walter Wang [MSFT] - 23 Nov 2006 06:46 GMT
Hi Olivier,

Thanks for the update.

The DllImport marshalling of StringBuilder internally copies the buffer
instead of returning the pointer to the internal buffer directly; while the
PtrToStringChars will return the buffer pointer directly (see Pinning
Pointers for more info). So far I don't know a way how to return the buffer
pointer of StringBuilder directly. I'm afraid for now you might have to use
the DllImport approach as a workaround.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Olivier Matrot - 23 Nov 2006 11:09 GMT
Thanks fot that, I'll copy the content in a pinned buffer allocated by hand.
Olivier.

> Hi Olivier,
>
[quoted text clipped - 21 lines]
> This posting is provided "AS IS" with no warranties, and confers no
> rights.

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.