from some ManagedC++ code I'm writing I would like to call a function like
that
void myfunction(wchar_t*, unsigned);

I would like to avoid attribute, particularly as I import the header, there
are plenty of custom types and function and it's much more easy to just call
the function (otherwise I rather code in C#, in fact...)


I though I could just

wchar_t* buf = alloc(n * sizeof(wchar_t))
for(...)
{
// copy char 1 by 1
}

bur certainly there is a faster version?
involving some memcpy, maybe internally to the StringBuilder?

Re: managed C++ StringBuilder & wchar_t* by Lloyd

Lloyd
Fri Jun 24 00:36:30 CDT 2005

I read an example like that:
int sum(array<int>^ arr)
{
interior_ptr<int> begin = &arr[0];
//.............
}
However, I need to call a C function with a int*, how could I?


"Lloyd Dupont" <net.galador@ld> wrote in message
news:ubdXVgHeFHA.1920@tk2msftngp13.phx.gbl...
> from some ManagedC++ code I'm writing I would like to call a function like
> that
> void myfunction(wchar_t*, unsigned);
>
> I would like to avoid attribute, particularly as I import the header,
> there are plenty of custom types and function and it's much more easy to
> just call the function (otherwise I rather code in C#, in fact...)
>
>
> I though I could just
>
> wchar_t* buf = alloc(n * sizeof(wchar_t))
> for(...)
> {
> // copy char 1 by 1
> }
>
> bur certainly there is a faster version?
> involving some memcpy, maybe internally to the StringBuilder?
>



Re: managed C++ StringBuilder & wchar_t* by Lloyd

Lloyd
Mon Jun 27 20:43:04 CDT 2005

This is a multi-part message in MIME format.

------=_NextPart_000_0130_01C57BD6.8BCCA6B0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

just replying myself.. one of the macro could be optimized but hell! =
it's nicer like that...
and it does involves some copy....

#define STRING_TO_POINTER_UNICODE(str, ptr) \
pin_ptr<wchar_t> ptr =3D nullptr; \
if(str) {\
array<wchar_t>^ __private__##str__##ptr__ =3D gcnew =
array<wchar_t>(str->Length); \
str->CopyTo(0, __private__##str__##ptr__, 0, str->Length); \
ptr =3D &__private__##str__##ptr__[0]; \
}
#define STRING_TO_POINTER_ANSI(str, ptr) \
pin_ptr<unsigned char> ptr =3D nullptr; \
if(str) {\
array<unsigned char>^ __private__##str__##ptr__ =3D =
Encoding::ASCII->GetBytes(str);\
ptr =3D &__private__##str__##ptr__[0];\
}
#define STRING_BUILDER_TO_POINTER_UNICODE(sb, ptr)\
pin_ptr<wchar_t> ptr =3D nullptr;\
if(sb) {\
array<wchar_t>^ __private__##str__##ptr__ =3D gcnew =
array<wchar_t>(sb->Capacity);\
sb->CopyTo(0, __private__##str__##ptr__, 0, sb->Length);\
ptr =3D &__private__##str__##ptr__[0];\
}
#define STRING_BUILDER_SET_TEXT_UNICODE(sb, ptr)\
if(sb) {\
sb->Length =3D 0;\
sb->Append(gcnew String((wchar_t*)ptr));\
}
#define STRING_BUILDER_TO_POINTER_ANSI(sb, ptr)\
pin_ptr<unsigned char> ptr =3D nullptr;\
if(sb) {\
array<unsigned char>^ __private__##str__##ptr__ =3D gcnew =
array<unsigned char>(sb->Capacity);\
=
Encoding::ASCII->GetBytes(sb->ToString())->CopyTo(__private__##str__##ptr=
__, 0);\
ptr =3D &__private__##str__##ptr__[0];\
}
#define STRING_BUILDER_SET_TEXT_ANSI(sb, ptr)\
if(sb) {\
sb->Length =3D 0;\
sb->Append(gcnew String((char*) ptr));\
}


"Lloyd Dupont" <net.galador@ld> wrote in message =
news:ubdXVgHeFHA.1920@tk2msftngp13.phx.gbl...
> from some ManagedC++ code I'm writing I would like to call a function =
like=20
> that
> void myfunction(wchar_t*, unsigned);
>=20
> I would like to avoid attribute, particularly as I import the header, =
there=20
> are plenty of custom types and function and it's much more easy to =
just call=20
> the function (otherwise I rather code in C#, in fact...)
>=20
>=20
> I though I could just
>=20
> wchar_t* buf =3D alloc(n * sizeof(wchar_t))
> for(...)
> {
> // copy char 1 by 1
> }
>=20
> bur certainly there is a faster version?
> involving some memcpy, maybe internally to the StringBuilder?=20
>=20
>
------=_NextPart_000_0130_01C57BD6.8BCCA6B0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2900.2668" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>just replying myself.. one of the macro =
could be=20
optimized but hell! it's nicer like that...</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>and it does involves some =
copy....</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#0000ff size=3D2>
<P>#define</FONT><FONT size=3D2> STRING_TO_POINTER_UNICODE(str, ptr)=20
\<BR></FONT><FONT color=3D#0000ff size=3D2>&nbsp;&nbsp;&nbsp; =
pin_ptr</FONT><FONT=20
size=3D2>&lt;</FONT><FONT color=3D#0000ff size=3D2>wchar_t</FONT><FONT =
size=3D2>&gt; ptr=20
=3D </FONT><FONT color=3D#0000ff size=3D2>nullptr</FONT><FONT size=3D2>; =

\<BR>&nbsp;&nbsp;&nbsp; </FONT><FONT color=3D#0000ff =
size=3D2>if</FONT><FONT=20
size=3D2>(str) {\<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; </FONT><FONT=20
color=3D#0000ff size=3D2>array</FONT><FONT size=3D2>&lt;</FONT><FONT =
color=3D#0000ff=20
size=3D2>wchar_t</FONT><FONT size=3D2>&gt;^ __private__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__ =3D </FONT><FONT color=3D#0000ff=20
size=3D2>gcnew</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>array</FONT><FONT size=3D2>&lt;</FONT><FONT color=3D#0000ff=20
size=3D2>wchar_t</FONT><FONT size=3D2>&gt;(str-&gt;Length); =
\<BR>&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; str-&gt;CopyTo(0, __private__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__, 0, str-&gt;Length); =
\<BR>&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; ptr =3D &amp;__private__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__[0]; \<BR>&nbsp;&nbsp;&nbsp; =
}<BR></FONT><FONT=20
color=3D#0000ff size=3D2>#define</FONT><FONT size=3D2> =
STRING_TO_POINTER_ANSI(str,=20
ptr) \<BR></FONT><FONT color=3D#0000ff size=3D2>&nbsp;&nbsp;&nbsp;=20
pin_ptr</FONT><FONT size=3D2>&lt;</FONT><FONT color=3D#0000ff=20
size=3D2>unsigned</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>char</FONT><FONT size=3D2>&gt; ptr =3D </FONT><FONT =
color=3D#0000ff=20
size=3D2>nullptr</FONT><FONT size=3D2>; \<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2>&nbsp;&nbsp;&nbsp; if</FONT><FONT size=3D2>(str) =
{\<BR></FONT><FONT=20
color=3D#0000ff size=3D2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
array</FONT><FONT=20
size=3D2>&lt;</FONT><FONT color=3D#0000ff size=3D2>unsigned</FONT><FONT =
size=3D2>=20
</FONT><FONT color=3D#0000ff size=3D2>char</FONT><FONT size=3D2>&gt;^=20
__private__</FONT><FONT color=3D#0000ff size=3D2>#</FONT><FONT=20
size=3D2>#str__</FONT><FONT color=3D#0000ff size=3D2>#</FONT><FONT =
size=3D2>#ptr__ =3D=20
Encoding::ASCII-&gt;GetBytes(str);\<BR>&nbsp;&nbsp;&nbsp; =
&nbsp;&nbsp;&nbsp; ptr=20
=3D &amp;__private__</FONT><FONT color=3D#0000ff size=3D2>#</FONT><FONT=20
size=3D2>#str__</FONT><FONT color=3D#0000ff size=3D2>#</FONT><FONT=20
size=3D2>#ptr__[0];\<BR>}<BR></FONT><FONT color=3D#0000ff =
size=3D2>#define</FONT><FONT=20
size=3D2> STRING_BUILDER_TO_POINTER_UNICODE(sb, ptr)\<BR></FONT><FONT=20
color=3D#0000ff size=3D2>&nbsp;&nbsp;&nbsp; pin_ptr</FONT><FONT=20
size=3D2>&lt;</FONT><FONT color=3D#0000ff size=3D2>wchar_t</FONT><FONT =
size=3D2>&gt; ptr=20
=3D </FONT><FONT color=3D#0000ff size=3D2>nullptr</FONT><FONT=20
size=3D2>;\<BR></FONT><FONT color=3D#0000ff size=3D2>&nbsp;&nbsp;&nbsp; =
if</FONT><FONT=20
size=3D2>(sb) {\<BR></FONT><FONT color=3D#0000ff =
size=3D2>&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; array</FONT><FONT size=3D2>&lt;</FONT><FONT =
color=3D#0000ff=20
size=3D2>wchar_t</FONT><FONT size=3D2>&gt;^ __private__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__ =3D </FONT><FONT color=3D#0000ff=20
size=3D2>gcnew</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>array</FONT><FONT size=3D2>&lt;</FONT><FONT color=3D#0000ff=20
size=3D2>wchar_t</FONT><FONT =
size=3D2>&gt;(sb-&gt;Capacity);\<BR>&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; sb-&gt;CopyTo(0, __private__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__, 0, =
sb-&gt;Length);\<BR>&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; ptr =3D &amp;__private__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__[0];\<BR>}<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2>#define</FONT><FONT size=3D2> =
STRING_BUILDER_SET_TEXT_UNICODE(sb,=20
ptr)\<BR></FONT><FONT color=3D#0000ff size=3D2>&nbsp;&nbsp;&nbsp; =
if</FONT><FONT=20
size=3D2>(sb) {\<BR>&nbsp;&nbsp;&nbsp; sb-&gt;Length =3D =
0;\<BR>&nbsp;&nbsp;&nbsp;=20
sb-&gt;Append(</FONT><FONT color=3D#0000ff size=3D2>gcnew</FONT><FONT =
size=3D2>=20
String((</FONT><FONT color=3D#0000ff size=3D2>wchar_t</FONT><FONT=20
size=3D2>*)ptr));\<BR>}<BR></FONT><FONT color=3D#0000ff =
size=3D2>#define</FONT><FONT=20
size=3D2> STRING_BUILDER_TO_POINTER_ANSI(sb, ptr)\<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2>&nbsp;&nbsp;&nbsp; pin_ptr</FONT><FONT =
size=3D2>&lt;</FONT><FONT=20
color=3D#0000ff size=3D2>unsigned</FONT><FONT size=3D2> </FONT><FONT =
color=3D#0000ff=20
size=3D2>char</FONT><FONT size=3D2>&gt; ptr =3D </FONT><FONT =
color=3D#0000ff=20
size=3D2>nullptr</FONT><FONT size=3D2>;\<BR></FONT><FONT color=3D#0000ff =

size=3D2>&nbsp;&nbsp;&nbsp; if</FONT><FONT size=3D2>(sb) =
{\<BR></FONT><FONT=20
color=3D#0000ff size=3D2><FONT size=3D2><FONT face=3DArial=20
color=3D#000000>&nbsp;&nbsp;&nbsp; <FONT face=3D"Times New Roman"=20
color=3D#0000ff>&nbsp;&nbsp;&nbsp; </FONT></FONT></FONT><FONT =
color=3D#0000ff=20
size=3D2>array</FONT><FONT size=3D2>&lt;</FONT><FONT color=3D#0000ff=20
size=3D2>unsigned</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>char</FONT><FONT size=3D2>&gt;^ __private__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__ =3D </FONT><FONT color=3D#0000ff=20
size=3D2>gcnew</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>array</FONT><FONT size=3D2>&lt;</FONT><FONT color=3D#0000ff=20
size=3D2>unsigned</FONT><FONT size=3D2> </FONT><FONT color=3D#0000ff=20
size=3D2>char</FONT><FONT=20
size=3D2>&gt;(sb-&gt;Capacity);\<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;=20
Encoding::ASCII-&gt;GetBytes(sb-&gt;ToString())-&gt;CopyTo(__private__</F=
ONT><FONT=20
color=3D#0000ff size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__, 0);\<BR>&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; </FONT></FONT><FONT size=3D2>ptr =3D =
&amp;__private__</FONT><FONT=20
color=3D#0000ff size=3D2>#</FONT><FONT size=3D2>#str__</FONT><FONT =
color=3D#0000ff=20
size=3D2>#</FONT><FONT size=3D2>#ptr__[0];\<BR>}<BR></FONT><FONT =
color=3D#0000ff=20
size=3D2>#define</FONT><FONT size=3D2> STRING_BUILDER_SET_TEXT_ANSI(sb,=20
ptr)\<BR></FONT><FONT color=3D#0000ff size=3D2>&nbsp;&nbsp;&nbsp; =
if</FONT><FONT=20
size=3D2>(sb) {\<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sb-&gt;Length =
=3D=20
0;\<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; sb-&gt;Append(</FONT><FONT=20
color=3D#0000ff size=3D2>gcnew</FONT><FONT size=3D2> =
String((</FONT><FONT=20
color=3D#0000ff size=3D2>char</FONT><FONT size=3D2>*) =
ptr));\<BR>&nbsp;&nbsp;&nbsp;=20
}<BR></P></FONT><FONT face=3DArial size=3D2></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>"Lloyd Dupont" &lt;</FONT><A=20
href=3D"mailto:net.galador@ld"><FONT face=3DArial=20
size=3D2>net.galador@ld</FONT></A><FONT face=3DArial size=3D2>&gt; wrote =
in message=20
</FONT><A href=3D"news:ubdXVgHeFHA.1920@tk2msftngp13.phx.gbl"><FONT =
face=3DArial=20
size=3D2>news:ubdXVgHeFHA.1920@tk2msftngp13.phx.gbl</FONT></A><FONT =
face=3DArial=20
size=3D2>...</FONT></DIV><FONT face=3DArial size=3D2>&gt; from some =
ManagedC++ code=20
I'm writing I would like to call a function like <BR>&gt; that<BR>&gt; =
void=20
myfunction(wchar_t*, unsigned);<BR>&gt; <BR>&gt; I would like to avoid=20
attribute, particularly as I import the header, there <BR>&gt; are =
plenty of=20
custom types and function and it's much more easy to just call <BR>&gt; =
the=20
function (otherwise I rather code in C#, in fact...)<BR>&gt; <BR>&gt; =
<BR>&gt; I=20
though I could just<BR>&gt; <BR>&gt; wchar_t* buf =3D alloc(n *=20
sizeof(wchar_t))<BR>&gt; for(...)<BR>&gt; {<BR>&gt;&nbsp;&nbsp;&nbsp; // =
copy=20
char 1 by 1<BR>&gt; }<BR>&gt; <BR>&gt; bur certainly there is a faster=20
version?<BR>&gt; involving some memcpy, maybe internally to the =
StringBuilder?=20
<BR>&gt; <BR>&gt;</FONT></BODY></HTML>

------=_NextPart_000_0130_01C57BD6.8BCCA6B0--