Click here to Skip to main content
15,911,139 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: New Project - Totally Lost Pin
Roger Wright9-May-02 4:47
professionalRoger Wright9-May-02 4:47 
GeneralHidecaret() help Pin
Ryan28458-May-02 20:35
Ryan28458-May-02 20:35 
GeneralRe: Hidecaret() help Pin
Nish Nishant8-May-02 20:43
sitebuilderNish Nishant8-May-02 20:43 
GeneralRe: Hidecaret() help Pin
Mike Nordell9-May-02 20:50
Mike Nordell9-May-02 20:50 
QuestionCString casted to CONST BYTE* - Is that safe? Pin
Nish Nishant8-May-02 20:19
sitebuilderNish Nishant8-May-02 20:19 
AnswerRe: CString casted to CONST BYTE* - Is that safe? Pin
Joaquín M López Muñoz8-May-02 21:14
Joaquín M López Muñoz8-May-02 21:14 
GeneralRe: CString casted to CONST BYTE* - Is that safe? Pin
Nish Nishant8-May-02 21:17
sitebuilderNish Nishant8-May-02 21:17 
GeneralRe: CString casted to CONST BYTE* - Is that safe? Pin
Jonathan Craig9-May-02 3:27
Jonathan Craig9-May-02 3:27 
Nish,
Note that to cast a CString to a (CONST BYTE *) is a safe in this case. Since RegSetValueEx method is looking for a (const BYTE *) and is just going to write the data a byte at a time to the registry, this works.

BUT, this would not be true if a function wanting a (CONST BYTE *) was going to look at it as a BYTE array of text characters. If compiling for a UNICODE release CString characters are 16 bit instead of 8 bit. Remember a BYTE is 8 bit for non-UNICODE and UNICODE both. So this could be a problem in other situations.

Also, note that your cast above would NOT work if you complied for UNICODE and told the RegSetValueEx method that the length of your data was CString::GetLength(). It would return the number of text characters not the number of bytes.

In cases where you want complete compatibility, use the MFC and ATL string conversion macros. See the MSDN article String Conversion Macros.
void CMyClass::HandleString(LPCTSTR szPath)
{
  if(szPath != NULL) 
  {
    USES_CONVERSION;
    char *s = T2A((LPTSTR)szPath);
 
    //Now 's' points to a 8 byte character string if complied
    // for unicode or not

    //Do something with the string...
    ...
  }
}
Nish, I'm sure you already know this, I mainly posted this for any new programmers who might need help with casting.

Hack on... Smile | :)

Jonathan Craig
www.mcw-tech.com
GeneralRe: CString casted to CONST BYTE* - Is that safe? Pin
Nish Nishant9-May-02 3:39
sitebuilderNish Nishant9-May-02 3:39 
GeneralUsing Win 2K Event Viewer are an App event viewer Pin
niverson8-May-02 19:41
niverson8-May-02 19:41 
GeneralRe: Using Win 2K Event Viewer are an App event viewer Pin
Mike Nordell9-May-02 20:55
Mike Nordell9-May-02 20:55 
GeneralRe: Using Win 2K Event Viewer are an App event viewer Pin
niverson10-May-02 4:41
niverson10-May-02 4:41 
GeneralRe: Using Win 2K Event Viewer are an App event viewer Pin
Mike Nordell10-May-02 15:51
Mike Nordell10-May-02 15:51 
GeneralRe: Using Win 2K Event Viewer are an App event viewer Pin
niverson13-May-02 4:21
niverson13-May-02 4:21 
GeneralCan't use the CMenu options in the OnPaint under VC++ 7 Pin
MaTrIX2k28-May-02 19:38
MaTrIX2k28-May-02 19:38 
GeneralRe: Can't use the CMenu options in the OnPaint under VC++ 7 Pin
Nish Nishant8-May-02 20:05
sitebuilderNish Nishant8-May-02 20:05 
GeneralSaving COleDateTime To Access using CRecordset Pin
Komtiki8-May-02 19:11
Komtiki8-May-02 19:11 
GeneralQuestion about viewing memory address. Pin
Maxwell Chen8-May-02 18:56
Maxwell Chen8-May-02 18:56 
GeneralRe: Question about viewing memory address. Pin
Nish Nishant8-May-02 20:03
sitebuilderNish Nishant8-May-02 20:03 
GeneralRe: Question about viewing memory address. Pin
Maxwell Chen9-May-02 0:53
Maxwell Chen9-May-02 0:53 
Questionhow to load a Dll in an ATL COM Server Pin
GooGoo8-May-02 18:39
GooGoo8-May-02 18:39 
AnswerRe: how to load a Dll in an ATL COM Server Pin
Ollie8-May-02 23:14
Ollie8-May-02 23:14 
GeneralMFC Extension DLL Problems Pin
Tony Fontenot8-May-02 17:51
Tony Fontenot8-May-02 17:51 
GeneralRe: MFC Extension DLL Problems Pin
Tony Fontenot8-May-02 18:12
Tony Fontenot8-May-02 18:12 
QuestionHow do I make this Unicode? Pin
Matt Newman8-May-02 17:08
Matt Newman8-May-02 17:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.