0

Currently I'm busy to get some text from the textbox and then convert in into a const char*. I have the following code:

System::String^ host = textBoxHostadres->Text;
    const char* host2 = (const char*)(void*)Marshal::StringToHGlobalAnsi(host);
    //system(host2); //What to do with this?
    Marshal::FreeHGlobal(host2);

It gives a redline (Visual Studio 2012) under Marshal::FreeHGlobal. Can somebody give me a right direction to get the text from the textbox and get it to a const char*? The first line works well and through debugging I see the text get captured.

Update:

System::String^ host = textBoxHostadres->Text;
                pin_ptr<const wchar_t> wch = PtrToStringChars(host);

            // Convert to a char*
            size_t origsize = wcslen(wch) + 1;
            const size_t newsize = 100;
            size_t convertedChars = 0;
            char nstring[newsize];
            wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
            strcat_s(nstring, " (char *)");

            const char* host2 = (const char*)(void*)Marshal::StringToHGlobalAnsi(host);
            system(host2);

            traceroute hostAddress(host2);
compu92
  • 101
  • 2
  • 12

2 Answers2

0

Here you will find all possible conversion between char *, wchar_t*, _bstr_t, CComBSTR, CString, basic_string, and System.String.

The strings types that are covered include char *, wchar_t*, _bstr_t, CComBSTR, CString, basic_string, and System.String. In all cases, a copy of the string is made when converted to the new type. Any changes made to the new string will not affect the original string, and vice versa.

Regarding the EDITs you've added:

It would be nice if you could at least try to understand the code you are copy-pasting. This line strcat_s(nstring, " (char *)"); adds some characters to your string and mainly (char *) and this is obvious now that your nstring doesn't contain an adress of a file allready, but adress + some trash. Also you don't need this line std::cout << nstring << std::endl; at all.

This is how your implementation should look like.

System::String^ host = textBoxHostadres->Text;
pin_ptr<const wchar_t> wch = PtrToStringChars(host);
// Convert to a char*
size_t origsize = wcslen(wch) + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
char nstring[newsize];  // nstring is an array of chars
wcstombs_s(&convertedChars, nstring, origsize, wch, _TRUNCATE);
// strcat_s(nstring, " (char *)");  this line adds trash to your nstring!!!
traceroute hostAddress(nstring);
Alexandru Barbarosie
  • 2,952
  • 3
  • 24
  • 46
  • I have updated my post, related to your answer above. How can can it be, that nstring is empty? I need the addres filled in to start a traceroute – compu92 Jun 25 '13 at 13:29
  • The last line is indeed not necessary, but it is handy to see what is printed out (debugging). I have updated my c++ code. This should do it. Only I still get an error, with that seems to do it with Boost::Asio. – compu92 Jun 25 '13 at 13:59
  • @computergek92 so you are converting your `host` to `nstring` and you never use `nstring` again... Remove `strcat_s(nstring, " (char *)");` and write `traceroute hostAddress(nstring);` – Alexandru Barbarosie Jun 25 '13 at 14:06
  • And for a String^ to int? On the listed MSDN website there is no information about it. On the internet there is only a lot of static casting – compu92 Jun 27 '13 at 07:41
  • @computergek92 well you make a `string` out of `String^` and then use `std::stoi`. – Alexandru Barbarosie Jun 27 '13 at 08:41
  • I have made this: System::String^ maxTTL = textBoxMaxTTL->Text; std::string bla = marshal_as(maxTTL); int maxTTL2 = atoi(bla.c_str()); But I stays '0'. I will try std::stoi – compu92 Jun 27 '13 at 10:45
  • I think you should try posting the issue you are having in a separate Q&A it is hard to understand from here what the actual problem is. – Alexandru Barbarosie Jun 27 '13 at 10:47
  • I will do that, see: http://stackoverflow.com/questions/17341479/text-from-textbox-covert-from-string-to-int – compu92 Jun 27 '13 at 11:24
0

You were not type-casting host2 in your call to Marshal::FreeHGlobal():

System::String^ host = textBoxHostadres->Text;
const char* host2 = (const char*)(void*)Marshal::StringToHGlobalAnsi(host);
system(host2);
traceroute hostAddress(host2);
Marshal::FreeHGlobal((IntPtr) host2);
Edward Clements
  • 5,040
  • 2
  • 21
  • 27