Dangerous Convertion?

Discussion in 'C++' started by Thomas.li@autodesk.com, May 30, 2008.

  1. Guest

    Hi,
    I want to convert CString to LPBYTE like
    LPBYTE lpByte = (BYTE*)(LPCTSTR)cstring;
    is it very dangerous to do that?
    , May 30, 2008
    #1
    1. Advertising

  2. red floyd Guest

    wrote:
    > 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.
    red floyd, May 30, 2008
    #2
    1. Advertising

  3. Lars Uffmann Guest

    wrote:
    > 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
    Lars Uffmann, May 30, 2008
    #3
  4. Thomas Guest

    Thanks for help:)
    Thomas, May 30, 2008
    #4
  5. Thomas Guest

    On 5ÔÂ30ÈÕ, ÏÂÎç2ʱ48·Ö, red floyd <> wrote:
    > wrote:
    > > 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.


    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?
    Thomas, May 30, 2008
    #5
  6. Guest

    On 30 Mai, 08:48, red floyd <> wrote:
    > wrote:
    > > 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
    , May 30, 2008
    #6
  7. red floyd Guest

    On May 30, 3:04 am, wrote:
    > On 30 Mai, 08:48, red floyd <> wrote:
    >
    > > wrote:
    > > > 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?


    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.
    red floyd, May 30, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    1,882
    Mike Treseler
    Feb 10, 2005
  2. isore
    Replies:
    0
    Views:
    529
    isore
    Jul 1, 2004
  3. Hiranya Samarasekera

    BMP to GIF convertion

    Hiranya Samarasekera, Aug 26, 2003, in forum: Java
    Replies:
    3
    Views:
    2,549
    Marco Schmidt
    Aug 27, 2003
  4. cccc
    Replies:
    8
    Views:
    10,027
    Andrew Thompson
    Sep 21, 2004
  5. mark

    Dangerous convertion?

    mark, Aug 9, 2009, in forum: C Programming
    Replies:
    59
    Views:
    1,393
    Antoninus Twink
    Aug 12, 2009
Loading...

Share This Page