Olivier
Wed Nov 22 08:58:09 CST 2006
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++.
"Walter Wang [MSFT]" <wawang@online.microsoft.com> wrote in message
news:xUPTXfjDHHA.4808@TK2MSFTNGXA01.phx.gbl...
> 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.
>