Thread: converting "String___gc" simply to "string"

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    7

    Question converting "String___gc" simply to "string"

    Hi

    I am using windows forms c++ to write a program, i need to get some string value from the user and pass it to a function of another class that is part of the project. However, the type of the string that I get from the text box is String___gc and I need to pass a string to my class what should I do ?

    here is a sample code of what i want to do:

    -----in the main form i have got :


    objectClass object;

    object.run(textBox1->text);

    ---- the definition of run function in the object class is:

    void object::run(string input) {}

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Is String a .NET string? Or an MFC CString? Or some other string class?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    i think it is a .Net string...it is what ever the text in a textbox in windows forms.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you can convert using the following method
    Code:
    String dotNetString;
    std::string cppString = (const char*)Marshal::StringToHGlobalAnsi(dotNetString).ToPointer()
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >i think it is a .Net string
    I don't know if there is any way to go directly to a C++ string. You may have to go to a char array, then to a string. It looks like the .NET String has a function called To_Char_Array(). So you might be able to do this:
    Code:
    char *temp = gc.To_Char_Array();
    string my_string(temp);
    //Now my_string should contain the contents of gc
    EDIT: It looks like ChaosEngine has your solution.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    i can't go to char array either !!... i tried it and i get the following:

    cannot convert from '__wchar_t __gc[]' to 'char *'

    this was the code i tried : char* ip=IPTextBox->Text->ToCharArray();

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try what ChaosEngine posted. I think To_Char_Array may convert to unicode.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think it would be something like:
    Code:
    String gc = IPTextBox->Text();
    string my_string = (const char*) StringToHGlobalAnsi(gc).ToPointer());
    That first line may not be quite right. I can't quite tell from what ChaosEngine posted.

  9. #9
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    forgot to mention that you'll also need
    Code:
    using namespace System::Runtime::InteropServices;
    at the top of your file.

    Please bear in mind that as soon as you start using .Net classs in managed C++ or C++/CLI you've basically thrown standard C++ out the window.

  10. #10
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by swoopy
    I think it would be something like:
    Code:
    String gc = IPTextBox->Text();
    string my_string = (const char*) Marshall::StringToHGlobalAnsi(gc).ToPointer());
    That first line may not be quite right. I can't quite tell from what ChaosEngine posted.
    it really depends on which version of visual studio you're using. if it's vs2003 then it's
    Code:
    String* gc = textbox->get_Text();
    string my_string = (const char*) Marshall::StringToHGlobalAnsi(gc).ToPointer());
    if it's vs2005 textbox->text is a property not a method os it's
    Code:
    String^ gc = textbox->Text;
    string my_string = (const char*) Marshall::StringToHGlobalAnsi(gc).ToPointer());
    actually reading the docs again you're supposed to free the memory you get back
    so the code should be (assuming vs2005)
    Code:
    String^ gc = textbox->Text;
    IntPtr textPtr = Marshall::StringToHGlobalAnsi(gc);
    string my_string = (const char*) textPtr .ToPointer());
    Marshall::FreeHGlobal(textPtr)
    there will also be some security issues calling this.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    ChaosEngine thanks ,,,i am using 2003 .net.. I have tried the code you mentioned with the using namespace System::Runtime::InteropServices; heading, but I get an error saying : 'Marshall' : is not a class or namespace name

    is there any library or class i should include?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    thanks anyways ...i figured it out

    this is the right way:
    String* managedString = IPTextBox->get_Text();
    string ip = (const char*)(Marshal::StringToHGlobalAnsi(managedString) ).ToPointer();

  13. #13
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    You need to free the pointer you are getting from marshalling. Look at the post above to see how it can be done.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. converting a char to a "string"
    By Bladactania in forum C Programming
    Replies: 4
    Last Post: 04-20-2009, 01:03 PM
  2. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  3. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  4. A Good Discussion - Stay on Topic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 71
    Last Post: 04-28-2004, 08:52 AM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM