@SteveWilson
I'm right now investigating a bug report for MATLink on Linux. It appears that cycling a string through MATLink appends a zero character code at the end somehow, on Linux only (but not on OS X). I still haven't gotten to the bottom of the issue, and it is possible that I'm using the MathLink API incorrectly, but it looks more like a bug. I'm going to report it once I have had the time to create a minimal test case, but I wanted to give you advance warning.
The zero is likely appended when sending the string through MathLink the first time, not when receiving it back the Mma kernel.
The sending function is as simple as this:
void eng_make_String(const unsigned short *str, int len, int characters) {
mwSize mbDims[2] = {1, len}; // use len, not characters, because no support for 4-byte characters in either Mma 9 or MATLAB
mxArray *var = mxCreateCharArray(2, mbDims);
std::copy(str, str+len, (unsigned short *) mxGetChars(var));
returnHandle(var);
}
with template:
:Begin:
:Function: eng_make_String
:Pattern: engMakeString[string_]
:Arguments: {string}
:ArgumentTypes: {UTF16String}
:ReturnType: Manual
:End:
(You can actually ignore the guts of the functions, as it's MATLAB-specific code, but I thought a bit of context is always good. Also, I'm not sure it's still true that MathLink doesn't support UTF16 characters that need to be stored on 4 (not 2) bytes).