1

Here is the method to convert from managed string to native char pointer, it'll return error code 42 when the input string is "€" (ASCII 128d):

void StringUtility::ManagedToNative(String^ source, char* pTarget, Int32 targetLength) { if(pTarget == NULL) throw gcnew System::ArgumentNullException("The source pointer cannot be empty."); if(targetLength <= 0) throw gcnew System::ArgumentOutOfRangeException("The target length has to be larger than 0."); memset(pTarget, 0, targetLength); if(String::IsNullOrEmpty(source)) { pTarget[0] = '\0'; return; } // Conversion to char* : size_t convertedChars = 0; size_t sizeInBytes = targetLength; size_t count = (source->Length > targetLength) ? _TRUNCATE : targetLength; errno_t err = 0; { /* minimize the scope of pined object */ pin_ptr<const wchar_t> wch = PtrToStringChars(source); err = wcstombs_s(&convertedChars, pTarget, sizeInBytes, wch, count); } // if truncate did happen and it's intended, return as successful. if( count == _TRUNCATE && err == STRUNCATE ) return; if (err != 0) throw gcnew System::InvalidOperationException("convert from String^ to char* failed"); } 

Is it just because I can't convert any string (with bytes >= 128) using wcstombs_s? (see this)

3
  • You're in managed code. Why are you using wcstombs_s rather than System::Text::Encoding? Commented Dec 24, 2014 at 13:07
  • I need to pin the pointer and let native dll access it Commented Dec 24, 2014 at 16:41
  • Then do exactly that, or use Encoding's pointer-based methods: msdn.microsoft.com/en-us/library/6s1x2atd%28v=vs.80%29.aspx Commented Dec 24, 2014 at 20:13

1 Answer 1

0

From MSDN doc about wcstombs_s:

If wcstombs_s encounters a wide character it cannot convert to a multibyte character, it puts 0 in *pReturnValue, sets the destination buffer to an empty string, sets errno to EILSEQ, and returns EILSEQ.

EILSEQ is error code 42.

To convert Unicode string to char* array, I'd suggest get the binary array of the Unicode string and encode it using Base64 and store the encoded result in a char* array.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.