Re: Combo Box String to char*

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Joseph M. Newcomer (newcomer_at_flounder.com)
Date: 05/18/04


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



Relevant Pages

  • Re: Converting BSTR to char*
    ... First, you should realize that a BSTR is a wide character string, whereas char* is a narrow string. ... This actually maintains both a narrow and wide string internally, and has cast operators for both const char* and const wchar_t*. ...
    (microsoft.public.vc.language)
  • Re: case insentisive file search
    ... CopyStringPrefix(const char *String, size_t PrefixLength) ... FileName = CopyString; ...
    (comp.lang.c)
  • Re: String Comparision
    ... int comp(const char *s, const char *t) { ... int comp(const restrict char *s, const char *t) { ... It could be argued that a lack of a string is less than an empty string, and that if both elements are "lack of string" they are of the same value. ...
    (comp.lang.c)
  • function (const char *) vs. function (char *)
    ... void myfunc (const char * somestring_ptr) ... strcpy (string, "What I want"); ... So I'm curious why "const char *" would be desirable? ...
    (comp.lang.c)
  • [PATCH 09/21] perf: rewire generic library stuff, p5
    ... +int eprintf(int level, const char *fmt, ...) ... * Helper function for splitting a string into an argv-like array. ... +static int count_argc(const char *str) ...
    (Linux-Kernel)