Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > CString to const char conversion

Reply
Thread Tools

CString to const char conversion

 
 
Fausto Lopez
Guest
Posts: n/a
 
      07-12-2004
I'm getting the following error:

'strlen' : cannot convert parameter 1 from 'class CString' to 'const char *'

when I try to compile the following code:

HRESULT AnsiToUnicode(CString pszA, LPOLESTR* ppszW)
{
ULONG cCharacters;
DWORD dwError;
// If input is null then just return the same.
if (NULL == pszA)
{
*ppszW = NULL;
return NOERROR;
}
// Determine number of wide characters to be allocated for the
// Unicode string.
cCharacters = strlen(pszA)+1; <-------------------------
..........................
}

Any suggestions as to what the problem might be?

Fausto


 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      07-12-2004
Fausto Lopez wrote:
> I'm getting the following error:
>
> 'strlen' : cannot convert parameter 1 from 'class CString' to 'const char *'
>
> when I try to compile the following code:
>
> HRESULT AnsiToUnicode(CString pszA, LPOLESTR* ppszW)
> {
> ULONG cCharacters;
> DWORD dwError;
> // If input is null then just return the same.
> if (NULL == pszA)
> {
> *ppszW = NULL;
> return NOERROR;
> }
> // Determine number of wide characters to be allocated for the
> // Unicode string.
> cCharacters = strlen(pszA)+1; <-------------------------
> .........................
> }
>
> Any suggestions as to what the problem might be?
>
> Fausto
>
>


CString is not a part of the C++ language.
Many string classes have a method to convert to and
from C-Style strings. If you were using the std::string
class, you would use the std::sting::c_str() method.
See if the CString class has a similar method.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      07-12-2004
Fausto Lopez wrote:

> I'm getting the following error:
>
> 'strlen' : cannot convert parameter 1 from 'class CString' to 'const char

*'

Try http://groups.google.com to find a group qualified to discuss CString.
This group can only reliably discuss platform neutral C++.

> when I try to compile the following code:
>
> HRESULT AnsiToUnicode(CString pszA, LPOLESTR* ppszW)
> {


Oookay. Nobody uses Hungarian Notation anymore. But "psz" means "pointer to
zero terminated string". This Department of Redundancy Department worked in
C using untypesafe conventions. CString is an object.

> ULONG cCharacters;
> DWORD dwError;
> // If input is null then just return the same.
> if (NULL == pszA)


This code treats pszA as a pointer, not an object. Try pszA.IsEmpty().

> {
> *ppszW = NULL;
> return NOERROR;
> }
> // Determine number of wide characters to be allocated for the
> // Unicode string.
> cCharacters = strlen(pszA)+1; <-------------------------


What's wrong with pszA.GetLength()?

I don't know what's wrong with pszA.operator LPCTSTR(), but you don't need
it. You could use a little more learning about both C++ and the MS library
supplying CString.

Also, MFC sucks, but you don't seem to be using all of it. Try WTL.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-12-2004
Fausto Lopez wrote:
> I'm getting the following error:
>
> 'strlen' : cannot convert parameter 1 from 'class CString' to 'const char *'
>
> when I try to compile the following code:
>
> HRESULT AnsiToUnicode(CString pszA, LPOLESTR* ppszW)
> {
> ULONG cCharacters;
> DWORD dwError;
> // If input is null then just return the same.
> if (NULL == pszA)
> {
> *ppszW = NULL;
> return NOERROR;
> }
> // Determine number of wide characters to be allocated for the
> // Unicode string.
> cCharacters = strlen(pszA)+1; <-------------------------
> .........................
> }
>
> Any suggestions as to what the problem might be?


Nope. CString (IIRC) has 'operator const char*' defined, so it
should be picked up for the conversion.

Unfortunately, any more detail on this is off-topic here since
none of MFC classes are part of standard C++. Try the newsgroup
microsoft.public.vc.mfc or comp.os.ms-windows.programmer.tools.mfc

Victor
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      07-12-2004
Phlip wrote:

> Fausto Lopez wrote:


> > // Determine number of wide characters to be allocated for the
> > // Unicode string.
> > cCharacters = strlen(pszA)+1; <-------------------------


> I don't know what's wrong with pszA.operator LPCTSTR()...


There I go not reading the comments again...

If you have _UNICODE turned on then pszA has operator LPCWSTR(), and you
need MS Windows's private lstrlen() function.

But first clean up your attrocious style, and many problems like that will
just go away.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      07-12-2004
Juha Kettunen wrote:
>
> Phlip wrote:


> > Also, MFC sucks,

>
> Why is that?
> I have been using it many years with now real problems.


When asked why I declare something sucks, I often point to issues writing
unit tests for it.

Such issues are typically symbolic of more important coupling issues.

In MFC, you can't create a window without coupling it, at run-time, to your
entire application. That breaks the principle "Test Isolation".

Try WTL to see what you have been missing.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


 
Reply With Quote
 
Juha Kettunen
Guest
Posts: n/a
 
      07-12-2004

"Phlip" <> wrote in message
news:5lCIc.1557$ ...
> Also, MFC sucks,


Why is that?
I have been using it many years with now real problems.


 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      07-13-2004
On Mon, 12 Jul 2004 22:06:04 GMT, "Juha Kettunen" <>
wrote:

>
>"Phlip" <> wrote in message
>news:5lCIc.1557$ m...
>> Also, MFC sucks,

>
>Why is that?
>I have been using it many years with now real problems.


MFC is a rather thin layer over Win32; it doesn't use proper object
oriented design. Obviously, it's perfectly usable in the same way that
Win32 is. However code using it is not as concise, understandable,
maintainable nor flexible as it that using better frameworks. Check
out almost any more recent GUI framework for better ways of doing
things.

Tom
 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      07-13-2004
tom_usenet wrote:

> On Mon, 12 Jul 2004 22:06:04 GMT, "Juha Kettunen" <>
> wrote:
>
>
>>"Phlip" <> wrote in message
>>news:5lCIc.1557$ om...
>>
>>>Also, MFC sucks,

>>
>>Why is that?
>>I have been using it many years with now real problems.

>
> MFC is a rather thin layer over Win32; it doesn't use proper object
> oriented design.


Also it is designed around the capabilities of the C++ compilers of the
early nineties. So essentially C with classes; no templates, no
exceptions, no standard library...etc. Hence many ugly macro hacks.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      07-13-2004
In message <>, Peter van Merkerk
<> writes
>tom_usenet wrote:
>
>> On Mon, 12 Jul 2004 22:06:04 GMT, "Juha Kettunen" <>
>> wrote:
>>
>>>"Phlip" <> wrote in message
>>>news:5lCIc.1557$. com...
>>>
>>>>Also, MFC sucks,
>>>
>>>Why is that?
>>>I have been using it many years with now real problems.

>> MFC is a rather thin layer over Win32; it doesn't use proper object
>> oriented design.

>
>Also it is designed around the capabilities of the C++ compilers of the
>early nineties. So essentially C with classes; no templates, no
>exceptions, no standard library...etc. Hence many ugly macro hacks.
>

As opposed to, say, ATL, which is designed around the capabilities of a
certain (allegedly-)C++ compiler of the later nineties ;-(

--
Richard Herring
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
Is char** (or char*[]) implicitly convertible to 'const char * const *'? kevin.hall@motioneng.com C Programming 24 10-30-2005 08:07 AM
Converting const char* to CString without Memory Leak nsyforce@aol.com C++ 3 04-28-2005 04:08 PM
extern const char * vs. extern const char []http://tinyurl.com/47e3k Thomas Matthews C++ 5 08-02-2004 10:36 AM



Advertisments