Re: Cast CString to unsigned char*

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Nathan Holt (nathanh_at_ccei-nm.com)
Date: 08/06/04


Date: Fri, 6 Aug 2004 09:20:16 -0600


"Dan Bloomquist" <EXTRApublic20@lakeweb.com> wrote in message
news:41113785.1060109@lakeweb.com...
>
>
> Frank E Rogers wrote:
> > Is there anyone who can tell me how to cast a CString to unsigned char*
?
>
> use two casts:
> (unsigned char*)(LPCTSTR)str;
>
> The LPCTSTR cast is necessary to invoke the overloaded operator of
> CString to return a pointer. Once you have that pointer, you can recast
it.
>
> Best, Dan.
>
> --
> http://lakeweb.net
> http://ReserveAnalyst.com
> No EXTRA stuff for email.

    As for (unsigned char*)(LPCTSTR)str; I recommend NOT doing that. It may
be useful for calling functions in a library that isn't const correct, but
I'd tend to assume that such a library actually does change the characters
in the string. This is bad because the string buffer may be shared with
several other string objects, and a change to the string's characters will
change all of the strings that use the buffer.
    The proper way to get an "unsigned char *" is to use GetBuffer and call
ReleaseBuffer after you are done with it. You may have to cast the result
of GetBuffer from "char *" to "unsigned char *".

                                        Nathan Holt



Relevant Pages

  • Re: Invalid CastException with SqlDBType
    ... Add(string*, SqlDbType, int, string**) ... The DateTime.Now is trying to cast into a int. ... not in Sql set up the column as identity and let it seed itself. ... > update a MS SQL Database with a stored procedure. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Porting up to VC++ 2005
    ... Since you are dealing with strings that contain file names, ... characters, but that's a different question to be handled in a different way. ... done, which should be unnecessary, it should be an LPTSTR cast, which ... It only applies to string and ...
    (microsoft.public.vc.mfc)
  • Re: Confused about using Reflection to examine a collection
    ... the precise type that Reflection is saying it is. ... say that 'obj' is actually a custom collection. ... If it were a basic type, such as 'string' then I would do a cast like this: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: TRUE and FALSE are treated differently
    ... No -- you can't echo a boolean in PHP. ... the boolean is implicitly cast to a string. ... If you look up the manual on type casting, you'll find that FALSE is cast to an empty string, and TRUE is cast to a non-empty string. ...
    (comp.lang.php)
  • Re: Error whilst processing ORDER BY clause
    ... My guess would be that you need to CAST it to a boolean since it is assuming ... > We receieve the error "Error whilst processing ORDER BY clause in the SQL ... name in the clause to reference a string or int data type it also succeeds, ... Can we use a CAST ...
    (microsoft.public.exchange2000.applications)