0

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Storo
  • 988
  • 1
  • 7
  • 31
  • 1
    For converting the `const uint8_t` array into a string, you need to include "stdlib.h". Then, do the following: `string adrs = (const char*) nativeCharArray`. This will give you a String Object. For the other way around: the String class in stdlib.h defines a function called c_str(). You can use it like this: `const uint8_t * adrsOtherwayAround = (const uint8_t *) adrs.c_str()` –  May 28 '15 at 13:16
  • @Eregrith I'm writting a DLL in C++ but part of the code is in C. – Storo May 28 '15 at 13:32
  • @Storo Is your problem a C problem or a C++ problem? – Eregrith May 28 '15 at 13:32
  • 1
    `I haven't seen C++ code in more than 10 years and now` And the code you posted in the question is not C++ (referring to `String^`). – PaulMcKenzie May 28 '15 at 13:32
  • @DodgerThud Thanks for the advice. I tried it and after including stdlib.h get the error "string" identifier not defined. – Storo May 28 '15 at 13:33
  • @PaulMcKenzie The part I put is not C++, ergo, the C tag on the question. The DLL is made in C++ but this part of the code is in C. Plus I don't think that C has `String^`, am I wrong? – Storo May 28 '15 at 13:35
  • @Storo `String^` does not exist in the C++ language. – PaulMcKenzie May 28 '15 at 13:36
  • @PaulMcKenzie It is C++/CLI. See http://en.wikipedia.org/wiki/C%2B%2B/CLI – Storo May 28 '15 at 13:37
  • @Storo I know what it is. What it is is *not* C++. That's why I edited the tags. – PaulMcKenzie May 28 '15 at 13:38
  • @Eregrith I'm receiving the parameters in C type definitions but I need to change them to C++ type definitions in order to use the `Ping` class. – Storo May 28 '15 at 13:39
  • @DodgerThud I included `` instead of `stdlib.h`. Also, the only thing I'm missing is the length of the new `const uint8_t *` and storing that length in a `uint32_t` variable. Do you have any idea of how to get it? – Storo May 28 '15 at 14:07

1 Answers1

2

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thanks a lot for posting your answer but I solved it using the first comment on the post, therefore, I haven't tried your solution. – Storo May 28 '15 at 17:44
  • 1
    Hmm, that doesn't convert from utf-8 and doesn't generate a String^. I can of course only answer the actual question asked. – Hans Passant May 28 '15 at 17:49
  • You are right. I used the following: `std::string stdStringIPAddress = (const char*)nativeCharArray; String^ ipAddress = gcnew String(stdStringIPAddress.c_str());`. As you can see is much simpler. – Storo May 28 '15 at 18:47
  • 2
    Well, of course it is simpler if you completely ignore the requirement to handle utf-8 encoded text. You'll be back. – Hans Passant May 28 '15 at 18:48
  • Why do you think that I could have a problem if I'm only sending an IP address in the form of a string that has been validated before? – Storo May 28 '15 at 21:10