I'm writing native C++ library to be used from C#. I need a C++ method receiving string (or char array) and Encoding. Inside this method I want to convert this string to byte array with respect of Encoding, work with it and send back string converted from byte array with respect of Encoding. As far as this method will be called from C#, I can pass System.Text.Encoding to it, but I don't know any analog in C++ for it. What approach would you suggest?
Asked
Active
Viewed 453 times
0
-
Not sure what you are asking. C# strings are always Unicode, equivalent to std::wstring or wchar[]. You don't need to pass any encoding unless you have already converted the string to something else. Even then, you can pass the codepage you used as a simple int – Panagiotis Kanavos Oct 02 '13 at 12:00
-
See http://stackoverflow.com/a/5622600/77724. – Anton Tykhyy Oct 02 '13 at 12:05
-
How are you planning to use it from C#? Are you writing a mixed-mode assembly in C++/CLI, or writing in unmanaged C++ and using P/Invoke? – dan04 Oct 04 '13 at 13:00
-
I'm writing in unmanaged C++ and then pinvoke. – Olga Oct 04 '13 at 13:27
1 Answers
1
It would be much simpler to pass the byte array to the C++ library if you're only going to operate on the bytes anyway...

helb
- 7,609
- 8
- 36
- 58
-
Thanks, I thought about it and I probably will choose this approach, if nothing better appears. But I'm just interested what if I needed to have such a method in C++ called from C++ as well, what then? – Olga Oct 02 '13 at 11:52
-
1@Olga When handling strings and encoding in C++, you're in a world of hurt. But that's just my experience ;-) – helb Oct 02 '13 at 12:16