Re: Combo Box String to char*
From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/18/04
- Next message: Joseph M. Newcomer: "Re: Help me!"
- Previous message: Joseph M. Newcomer: "Re: PostThreadMessage to CWinThread failing to be processed."
- In reply to: Martin Schmid: "Combo Box String to char*"
- Next in thread: Martin Schmid: "Re: Combo Box String to char*"
- Reply: Martin Schmid: "Re: Combo Box String to char*"
- Messages sorted by: [ date ] [ thread ]
Date: Tue, 18 May 2004 15:03:27 -0400
Well, the real problem is most likely that the function should have been a const char *,
or better still, an LPCTSTR parameter. char [] is, shall we say "so retro".
You have chosen an incredibly complex way of handling a simple task. There is no need to
do a copy, particularly if the value is not modified by the someFunc. The simplest method
would be to rewrite that parameter as
someFunc(LPCTSTR someStr)
and then the automatic casting of a CString to an LPCTSTR will work, and you will just
pass the name of the CString as the parameter.
If you need the buffer to be modifiable, you would do it as
someFunc(cmdConduitTypeStr.GetBuffer(SOME_SIZE));
where SOME_SIZE can be specified as 0 if the size should be no longer than the actual
number of characters in the string; if you give a longer value, the string buffer is
extended to cover the size you want. You must, upon return, do
cmdConduitTypeStr.ReleaseBuffer();
to release the buffer. But if you write the parameter correctly (you should always use
const if the string will not be modified) you probably don't even need this much.
It is good programming practice in Windows to forget entirely that 'char' is a datatype.
If you need a character, use TCHAR. If you need a pointer, use LPTSTR or LPCTSTR. If you
need a literal, place it inside an _T() macro, e.g., _T("String") or _T('c'). Then you are
writing Unicode-aware code.
And please, please, forget that malloc and free exist! You are using C++, not C! In the
case where you need to allocate a string, you would do
LPCTSTR b;
b = new TCHAR[cmdConduitTypeStr.GetLength()];
...test for successful allocation
lstrcpy(b, cmdConduitTypeStr);
...do things with string
delete b;
Read my essay on CStrings on my MVP Tips site.
joe
On Tue, 18 May 2004 08:28:34 -0500, "Martin Schmid" <martinschmid@sbcglobal.net.nospam>
wrote:
>I have a CComboBox with a String variable 'cmbConduitTypeStr'
>
>I have function in which I need to pass a char[]... someFunc(char
>someStr[])
>
>I've tried to convert to char* using the following w/ limited success...
>after a few executions of the snippet, the program crashes.
>
>
>char* b;
>b = (char*)malloc(cmbConduitTypeStr.GetLength());
>b = cmbConduitTypeStr.GetBuffer(0);
>
>... using 'b' here...
>
>free (b);
>
>I think I need to do the following:
>declare char b[] to be large enough to hold what is in cmbConduitTypeStr
>strcpy what is in cmbConduitTypeStr to b[]
>free/delete b[] when I am done w/ it.
>
>
>I am horrible in working w/ char[], CStrings, etc... anyway... if someone
>can offer a snippet w/ some explanation, I'd appreciate it.
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
- Next message: Joseph M. Newcomer: "Re: Help me!"
- Previous message: Joseph M. Newcomer: "Re: PostThreadMessage to CWinThread failing to be processed."
- In reply to: Martin Schmid: "Combo Box String to char*"
- Next in thread: Martin Schmid: "Re: Combo Box String to char*"
- Reply: Martin Schmid: "Re: Combo Box String to char*"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|