I haven't seen C++ code in more than 10 years and now I'm in the need of developing a very small DLL to use the Ping
class (System::Net::NetworkInformation)
to make a ping to some remoteAddress
.
The argument where I'm receiving the remoteAddress
is a FREObject
which then needs to be transformed into a const uint8_t *
. The previous is mandatory and I can't change anything from it. The remoteAddress
has to be received as a FREObject
and later be transformed in a const uint8_t *
.
The problem I'm having is that I have to pass a String^
to the Ping
class and not a const uint8_t *
and I have no clue of how to convert my const uint8_t *
to a String^
. Do you have any ideas?
Next is part of my code:
// argv[ARG_IP_ADDRESS_ARGUMENT holds the remoteAddress value.
uint32_t nativeCharArrayLength = 0;
const uint8_t * nativeCharArray = NULL;
FREResult status = FREGetObjectAsUTF8(argv[ARG_IP_ADDRESS_ARGUMENT], &nativeCharArrayLength, &nativeCharArray);
Basically the FREGetObjectAsUTF8
function fills the nativeCharArray
array with the value of argv[ARG_IP_ADDRESS_ARGUMENT]
and returns the array's length in nativeCharArrayLength
. Also, the string uses UTF-8 encoding terminates with the null character.
My next problem would be to convert a String^
back to a const uint8_t *
. If you can help with this as well I would really appreciate it.
As I said before, non of this is changeable and I have no idea of how to change nativeCharArray
to a String^
. Any advice will help.
PS: Also, the purpose of this DLL is to use it as an ANE (Air Native Extension) for my Adobe Air app.
You'll need UTF8Encoding to convert the bytes to characters. It has methods that take pointers, you'll want to take advantage of that. You first need to count the number of characters in the converted string, then allocate an array to store the converted characters, then you can turn it into System::String. Like this:
auto converter = gcnew System::Text::UTF8Encoding;
auto chars = converter->GetCharCount((Byte*)nativeCharArray, nativeCharArrayLength-1);
auto buffer = gcnew array<Char>(chars);
pin_ptr<Char> pbuffer = &buffer[0];
converter->GetChars((Byte*)nativeCharArray, nativeCharArrayLength-1, pbuffer, chars);
String^ result = gcnew String(buffer);
Note that the -1 on nativeCharArrayLength compensates for the zero terminator being included in the value.