managed C++ StringBuilder & wchar_t*

  • Thread starter Thread starter Lloyd Dupont
  • Start date Start date
L

Lloyd Dupont

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