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.

Re: Custom StringBuilder Marshaling by Mattias

Mattias
Tue Nov 21 14:12:21 CST 2006

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

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

Re: Custom StringBuilder Marshaling by Olivier

Olivier
Wed Nov 22 01:42:00 CST 2006

Mattias,
You're right, but I'm not going to use DllImport so, I need to do it by
hand.

"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:uE6Q1kaDHHA.1220@TK2MSFTNGP04.phx.gbl...
> 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
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.



Re: Custom StringBuilder Marshaling by wawang

wawang
Wed Nov 22 07:12:08 CST 2006

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.


Re: Custom StringBuilder Marshaling by Olivier

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.
>



Re: Custom StringBuilder Marshaling by wawang

wawang
Thu Nov 23 00:46:19 CST 2006

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.



Re: Custom StringBuilder Marshaling by Olivier

Olivier
Thu Nov 23 05:09:24 CST 2006

Thanks fot that, I'll copy the content in a pinned buffer allocated by hand.
Olivier.

"Walter Wang [MSFT]" <wawang@online.microsoft.com> wrote in message
news:lKcrassDHHA.4372@TK2MSFTNGXA01.phx.gbl...
> 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.
>
>