How to convert CString into Char * ? - Programmers Heaven

Howdy, Stranger!

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

Categories

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.