Dangerous Convertion?

T

Thomas.li

Hi,
I want to convert CString to LPBYTE like
LPBYTE lpByte = (BYTE*)(LPCTSTR)cstring;
is it very dangerous to do that?
 
R

red floyd

Hi,
I want to convert CString to LPBYTE like
LPBYTE lpByte = (BYTE*)(LPCTSTR)cstring;
is it very dangerous to do that?

Depends. What's BYTE, LPBYTE, LPCTSTR, and CString?

Ask in an MS newsgroup.

That said, in general, you should avoid C-style casts in favor of the
new casts (static_cast, reinterpret_cast, dynamic_cast, const_cast).
Furthermore, yes, you have a very dangerous conversion, you're casting
away a const.
 
L

Lars Uffmann

I want to convert CString to LPBYTE like
LPBYTE lpByte = (BYTE*)(LPCTSTR)cstring;
is it very dangerous to do that?

Yes, very dangerous. If you set a wrong bit there, your computer is
gonna blow up and flatten the house.

S,CNR

Lars
 
T

Thomas

Depends. What's BYTE, LPBYTE, LPCTSTR, and CString?

Ask in an MS newsgroup.

That said, in general, you should avoid C-style casts in favor of the
new casts (static_cast, reinterpret_cast, dynamic_cast, const_cast).
Furthermore, yes, you have a very dangerous conversion, you're casting
away a const.

Thanks for reminding, how about using GetBuffer(0) to return the
pointer
which allows me to modify. If it's correct, one more question: if the
_unicode
is turned on then GetBuffer() would return wchar_t*, then convert it
to
BYTE* is it also dangerous?
 
D

dertopper

Hi,
I want to convert CString to LPBYTE like
 LPBYTE lpByte = (BYTE*)(LPCTSTR)cstring;
is it very dangerous to do that?

Depends.  What's BYTE, LPBYTE, LPCTSTR, and CString? [snip]
Furthermore, yes, you have a very dangerous conversion, you're casting
away a const.

@red floyd:
Hah, you have outsmarted yourself: If you don't know what LPBYTE and
LPCTSTR mean, how come that you know that OP casts away constness?

@Thomas:
It really depends (so it should be avoided). Remember that LPCTSTR may
be either a UNICODE string or a ASCII string, so that cast may do what
you want or not. Casting away the constness is also bad. Consider the
T2CA macro from atlconv.h (MS specific).

Regards,
Stuart
 
R

red floyd

Depends. What's BYTE, LPBYTE, LPCTSTR, and CString? [snip]
Furthermore, yes, you have a very dangerous conversion, you're casting
away a const.

@red floyd:
Hah, you have outsmarted yourself: If you don't know what LPBYTE and
LPCTSTR mean, how come that you know that OP casts away constness?

I know damn well what they are. However, they are not defined by ISO/
IEC 14882:2003 (the C++ Standard) and they are therefore off-topic
here.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top