Board index » Visual Studio » Conversion from ANSI to UNICODE errors

Conversion from ANSI to UNICODE errors

Visual Studio168
Can any body help to figure out why the following code generates the

following errors at compile time, and also any suggestions on how to fix it?



BACKGROUND INFO

--HRESULT AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW) <-- Function

definition

--private:

CString m_saveAsFilename; <-- Private variable definition inside class

--CString fullpath = GetDocument()->GetPathName();



CODE IN QUESTION

-- AnsiToUnicode(m_saveAsFilename,&m_pwBuffer);

-- AnsiToUnicode(fullpath,&m_pwBuffer);



ERROR MESSAGED FOR BOTH FUNCTION CALLS

error C2664: 'AnsiToUnicode' : cannot convert parameter 1 from 'class

CString' to 'const char *'

No user-defined-conversion operator available that can perform this

conversion, or the operator cannot be called





Fausto


-
 

Re:Conversion from ANSI to UNICODE errors

Are you compiling with _UNICODE defined?



If so, CString will contain Unicode strings and will only be able to

automatically convert to w_char*.



--

Jonathan Wood

SoftCircuits

www.softcircuits.com">www.softcircuits.com

Available for consulting: www.softcircuits.com/jwood/resume.htm">www.softcircuits.com/jwood/resume.htm





"Fausto Lopez" <fuzzlog2000@earthlink.net>wrote in message

Quote
Can any body help to figure out why the following code generates the

following errors at compile time, and also any suggestions on how to fix

it?



BACKGROUND INFO

--HRESULT AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW) <-- Function

definition

--private:

CString m_saveAsFilename; <-- Private variable definition inside

class

--CString fullpath = GetDocument()->GetPathName();



CODE IN QUESTION

-- AnsiToUnicode(m_saveAsFilename,&m_pwBuffer);

-- AnsiToUnicode(fullpath,&m_pwBuffer);



ERROR MESSAGED FOR BOTH FUNCTION CALLS

error C2664: 'AnsiToUnicode' : cannot convert parameter 1 from 'class

CString' to 'const char *'

No user-defined-conversion operator available that can perform

this

conversion, or the operator cannot be called





Fausto









-

Re:Conversion from ANSI to UNICODE errors

Fausto,



What this error message is saying is that the compiler can't convert a

CString object to a const char*. You will need to double type-cast the

CString object to get it into a const char* format.



Example:



(LPCSTR)(LPCTSTR)m_saveAsFilename

(LPCSTR)(LPCTSTR)fullpath



Also note:

If you are compiling the project with _UNICODE defined, CString

automatically uses unicode as the default (could be why you are receiving

this compiler error message). This could cause problems with your

type-casting if you have it so. If you don't have _UNICODE defined, the

double type-cast should work ok for you.



Relvinian



"Fausto Lopez" <fuzzlog2000@earthlink.net>wrote in message

Quote
Can any body help to figure out why the following code generates the

following errors at compile time, and also any suggestions on how to fix

it?



BACKGROUND INFO

--HRESULT AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW) <-- Function

definition

--private:

CString m_saveAsFilename; <-- Private variable definition inside

class

--CString fullpath = GetDocument()->GetPathName();



CODE IN QUESTION

-- AnsiToUnicode(m_saveAsFilename,&m_pwBuffer);

-- AnsiToUnicode(fullpath,&m_pwBuffer);



ERROR MESSAGED FOR BOTH FUNCTION CALLS

error C2664: 'AnsiToUnicode' : cannot convert parameter 1 from 'class

CString' to 'const char *'

No user-defined-conversion operator available that can perform

this

conversion, or the operator cannot be called





Fausto









-

Re:Conversion from ANSI to UNICODE errors

Don't do those casts. You're just masking the error.



"Relvinian" <m@msn.com>wrote in message

Quote
Fausto,



What this error message is saying is that the compiler can't convert a

CString object to a const char*. You will need to double type-cast the

CString object to get it into a const char* format.



Example:



(LPCSTR)(LPCTSTR)m_saveAsFilename

(LPCSTR)(LPCTSTR)fullpath



Also note:

If you are compiling the project with _UNICODE defined, CString

automatically uses unicode as the default (could be why you are receiving

this compiler error message). This could cause problems with your

type-casting if you have it so. If you don't have _UNICODE defined, the

double type-cast should work ok for you.



Relvinian



"Fausto Lopez" <fuzzlog2000@earthlink.net>wrote in message

news:zwVIc.2044$Qu5.1548@newsread2.news.pas.earthlink.net...

>Can any body help to figure out why the following code generates the

>following errors at compile time, and also any suggestions on how to fix

it?

>

>BACKGROUND INFO

>--HRESULT AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW) <-- Function

>definition

>--private:

>CString m_saveAsFilename; <-- Private variable definition inside

class

>--CString fullpath = GetDocument()->GetPathName();

>

>CODE IN QUESTION

>-- AnsiToUnicode(m_saveAsFilename,&m_pwBuffer);

>-- AnsiToUnicode(fullpath,&m_pwBuffer);

>

>ERROR MESSAGED FOR BOTH FUNCTION CALLS

>error C2664: 'AnsiToUnicode' : cannot convert parameter 1 from 'class

>CString' to 'const char *'

>No user-defined-conversion operator available that can perform

this

>conversion, or the operator cannot be called

>

>

>Fausto

>

>









-

Re:Conversion from ANSI to UNICODE errors

I wouldn't necessarily do them --- besides, if you watch the "guts", meaning

looking at the assembly, a pointer is a pointer, doesn't matter what type it

is. Just a four-byte value!



But if you know what the data is and are always aware, you'll be ok. Of

course, the better solution is finding out why!



But if you *always* compile for ANSI systems and you are using a CString

object and trying to pass data to a const char*, type casting is just fine.

But the question remains, why are you using CString or trying to pass data

to a const char* function instead of a (possible) w_char* function?



Relvinian



"Alexander Grigoriev" <alegr@earthlink.net>wrote in message

Quote
Don't do those casts. You're just masking the error.



"Relvinian" <m@msn.com>wrote in message

news:zLGdnYPtEKJOKWndRVn-sQ@comcast.com...

>Fausto,

>

>What this error message is saying is that the compiler can't convert a

>CString object to a const char*. You will need to double type-cast the

>CString object to get it into a const char* format.

>

>Example:

>

>(LPCSTR)(LPCTSTR)m_saveAsFilename

>(LPCSTR)(LPCTSTR)fullpath

>

>Also note:

>If you are compiling the project with _UNICODE defined, CString

>automatically uses unicode as the default (could be why you are

receiving

>this compiler error message). This could cause problems with your

>type-casting if you have it so. If you don't have _UNICODE defined, the

>double type-cast should work ok for you.

>

>Relvinian

>

>"Fausto Lopez" <fuzzlog2000@earthlink.net>wrote in message

>news:zwVIc.2044$Qu5.1548@newsread2.news.pas.earthlink.net...

>>Can any body help to figure out why the following code generates the

>>following errors at compile time, and also any suggestions on how to

fix

>it?

>>

>>BACKGROUND INFO

>>--HRESULT AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW) <-- Function

>>definition

>>--private:

>>CString m_saveAsFilename; <-- Private variable definition inside

>class

>>--CString fullpath = GetDocument()->GetPathName();

>>

>>CODE IN QUESTION

>>-- AnsiToUnicode(m_saveAsFilename,&m_pwBuffer);

>>-- AnsiToUnicode(fullpath,&m_pwBuffer);

>>

>>ERROR MESSAGED FOR BOTH FUNCTION CALLS

>>error C2664: 'AnsiToUnicode' : cannot convert parameter 1 from 'class

>>CString' to 'const char *'

>>No user-defined-conversion operator available that can perform

>this

>>conversion, or the operator cannot be called

>>

>>

>>Fausto

>>

>>

>

>









-

Re:Conversion from ANSI to UNICODE errors

That could be quite a useful debugging tool - if [when] your program crashes

you could just look for these: (LPCSTR)(LPCTSTR) and immediately find where

the problem is.



Bill



"Relvinian" <m@msn.com>wrote in message

Quote
Fausto,



What this error message is saying is that the compiler can't convert a

CString object to a const char*. You will need to double type-cast the

CString object to get it into a const char* format.



Example:



(LPCSTR)(LPCTSTR)m_saveAsFilename

(LPCSTR)(LPCTSTR)fullpath



Also note:

If you are compiling the project with _UNICODE defined, CString

automatically uses unicode as the default (could be why you are receiving

this compiler error message). This could cause problems with your

type-casting if you have it so. If you don't have _UNICODE defined, the

double type-cast should work ok for you.



Relvinian



"Fausto Lopez" <fuzzlog2000@earthlink.net>wrote in message

news:zwVIc.2044$Qu5.1548@newsread2.news.pas.earthlink.net...

>Can any body help to figure out why the following code generates the

>following errors at compile time, and also any suggestions on how to fix

it?

>

>BACKGROUND INFO

>--HRESULT AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW) <-- Function

>definition

>--private:

>CString m_saveAsFilename; <-- Private variable definition inside

class

>--CString fullpath = GetDocument()->GetPathName();

>

>CODE IN QUESTION

>-- AnsiToUnicode(m_saveAsFilename,&m_pwBuffer);

>-- AnsiToUnicode(fullpath,&m_pwBuffer);

>

>ERROR MESSAGED FOR BOTH FUNCTION CALLS

>error C2664: 'AnsiToUnicode' : cannot convert parameter 1 from 'class

>CString' to 'const char *'

>No user-defined-conversion operator available that can perform

this

>conversion, or the operator cannot be called

>

>

>Fausto

>

>









-

Re:Conversion from ANSI to UNICODE errors

Quote
--CString fullpath = GetDocument()->GetPathName();

-- AnsiToUnicode(fullpath,&m_pwBuffer);

You try to use the CString as a target for the function.

This does not work.

You should get a buffer big enough (GetBuffer or GetBufferSetLength)

use this as a parameter to your call, then release it

(ReleaseBuffer or ReleaseBufferSetLength)



And you should be carefull that the CString is ANSI.

And with Dev.Studio 6 or older the only way to do this is

to have a non-unicode application.





--

Mihai

-------------------------

Replace _year_ with _ to get the real email

-

Re:Conversion from ANSI to UNICODE errors

You don't need a cast to get LPCTSTR from CString (other than when passing

it to a printf/Format).



"Relvinian" <m@msn.com>wrote in message

Quote
I wouldn't necessarily do them --- besides, if you watch the "guts",

meaning

looking at the assembly, a pointer is a pointer, doesn't matter what type

it

is. Just a four-byte value!



But if you know what the data is and are always aware, you'll be ok. Of

course, the better solution is finding out why!



But if you *always* compile for ANSI systems and you are using a CString

object and trying to pass data to a const char*, type casting is just

fine.

But the question remains, why are you using CString or trying to pass data

to a const char* function instead of a (possible) w_char* function?



Relvinian



"Alexander Grigoriev" <alegr@earthlink.net>wrote in message

news:e3VQPrVaEHA.4052@TK2MSFTNGP10.phx.gbl...

>Don't do those casts. You're just masking the error.

>

>"Relvinian" <m@msn.com>wrote in message

>news:zLGdnYPtEKJOKWndRVn-sQ@comcast.com...

>>Fausto,

>>

>>What this error message is saying is that the compiler can't convert a

>>CString object to a const char*. You will need to double type-cast

the

>>CString object to get it into a const char* format.

>>

>>Example:

>>

>>(LPCSTR)(LPCTSTR)m_saveAsFilename

>>(LPCSTR)(LPCTSTR)fullpath

>>

>>Also note:

>>If you are compiling the project with _UNICODE defined, CString

>>automatically uses unicode as the default (could be why you are

receiving

>>this compiler error message). This could cause problems with your

>>type-casting if you have it so. If you don't have _UNICODE defined,

the

>>double type-cast should work ok for you.

>>

>>Relvinian

>>

>>"Fausto Lopez" <fuzzlog2000@earthlink.net>wrote in message

>>news:zwVIc.2044$Qu5.1548@newsread2.news.pas.earthlink.net...

>>>Can any body help to figure out why the following code generates the

>>>following errors at compile time, and also any suggestions on how to

fix

>>it?

>>>

>>>BACKGROUND INFO

>>>--HRESULT AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW) <-- Function

>>>definition

>>>--private:

>>>CString m_saveAsFilename; <-- Private variable definition

inside

>>class

>>>--CString fullpath = GetDocument()->GetPathName();

>>>

>>>CODE IN QUESTION

>>>-- AnsiToUnicode(m_saveAsFilename,&m_pwBuffer);

>>>-- AnsiToUnicode(fullpath,&m_pwBuffer);

>>>

>>>ERROR MESSAGED FOR BOTH FUNCTION CALLS

>>>error C2664: 'AnsiToUnicode' : cannot convert parameter 1 from

'class

>>>CString' to 'const char *'

>>>No user-defined-conversion operator available that can

perform

>>this

>>>conversion, or the operator cannot be called

>>>

>>>

>>>Fausto

>>>

>>>

>>

>>

>

>









-