Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How to convert CString into Char * ?

pasapasa Posts: 23Member
I have variable:
[code]
char * str;
[/code]
and m_str is a member of TextBox Control, its type: CString
And I want when I click on button, str will get the text in the text box.

I used:
[code]
str = m_str;
[/code]

I know it's wrong, but I don't have any better idea. Can anyone help me please?

Comments

  • veliusvelius Posts: 456Member
    : I have variable:
    : [code]
    : char * str;
    : [/code]
    : and m_str is a member of TextBox Control, its type: CString
    : And I want when I click on button, str will get the text in the text box.
    :
    : I used:
    : [code]
    : str = m_str;
    : [/code]
    :
    : I know it's wrong, but I don't have any better idea. Can anyone help me please?
    :

    First you need to allocate memory for the char* str to point to.
    For example:
    [code]
    char* str = new char[m_str.GetLength() +1];
    [/code]
    That would allocate enough memory for a C-Style null terminated string.
    After that you'll need to copy the contents of the CString m_str variable to the newly allocated memory pointed to by the char* str variable.

    You can look this up on msdn.microsoft.com for more detailed information.

    Best of luck.

    [hr]
    We'll be an army of theives
    Of self-freed slaves
    Of mild-mannered maids
    We'll fight with whispers and blades
    So get ready, a new day is dawning
    - The New Wild West -- Jewel

  • john_vp2007john_vp2007 Posts: 2Member
    : : I have variable:
    : : [code]:
    : : char * str;
    : : [/code]:
    : : and m_str is a member of TextBox Control, its type: CString
    : : And I want when I click on button, str will get the text in the text box.
    : :
    : : I used:
    : : [code]:
    : : str = m_str;
    : : [/code]:
    : :
    : : I know it's wrong, but I don't have any better idea. Can anyone help me please?
    : :
    :
    : First you need to allocate memory for the char* str to point to.
    : For example:
    : [code]:
    : char* str = new char[m_str.GetLength() +1];
    : [/code]:
    : That would allocate enough memory for a C-Style null terminated
    : string.
    : After that you'll need to copy the contents of the CString m_str
    : variable to the newly allocated memory pointed to by the char* str
    : variable.
    :
    : You can look this up on msdn.microsoft.com for more detailed
    : information.
    :
    : Best of luck.
    :
    : [hr]
    : We'll be an army of theives
    : Of self-freed slaves
    : Of mild-mannered maids
    : We'll fight with whispers and blades
    : So get ready, a new day is dawning
    : - The New Wild West -- Jewel
    :
    : I can suggest you one moreway.
    suppose you have a string csString.
    and you want to convert it into a char* then....

    char *string = new char[csString.GetLength() +1];
    string = csString.GetBuffer(csString.GetLength() +1);
    csString.ReleaseBuffer();
Sign In or Register to comment.