CString to const char conversion

F

Fausto Lopez

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
 
T

Thomas Matthews

Fausto said:
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
 
P

Phlip

Fausto said:
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.
 
V

Victor Bazarov

Fausto said:
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
 
P

Phlip

Phlip said:
Fausto Lopez wrote:
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.
 
P

Phlip

Juha said:
Phlip wrote:

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.
 
T

tom_usenet

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
 
P

Peter van Merkerk

tom_usenet said:
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.
 
R

Richard Herring

Peter van Merkerk said:
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 ;-(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top