Click here to Skip to main content
15,907,392 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Dll prob - HELP Pin
vmaltsev10-Apr-02 0:26
vmaltsev10-Apr-02 0:26 
Generalclosing a view Pin
9-Apr-02 13:55
suss9-Apr-02 13:55 
GeneralRe: closing a view Pin
Michael Dunn9-Apr-02 14:25
sitebuilderMichael Dunn9-Apr-02 14:25 
GeneralRe: closing a view Pin
16-Apr-02 21:29
suss16-Apr-02 21:29 
GeneralCDCs and CBitmap Pin
marcela9-Apr-02 13:09
marcela9-Apr-02 13:09 
GeneralRe: CDCs and CBitmap Pin
moredip9-Apr-02 13:18
moredip9-Apr-02 13:18 
GeneralWideCharToMultiByte Pin
kasturirawat9-Apr-02 12:00
kasturirawat9-Apr-02 12:00 
GeneralRe: WideCharToMultiByte Pin
Philippe Mori9-Apr-02 13:04
Philippe Mori9-Apr-02 13:04 
I assume that valid null terminated strings are passed to the functions and that cch is the length (not including the ending NULL) of the original string (0 means compute) and szLen is the buffer size of the output including the room for the NULL terminator.

In such case, then if cch is greater or equal to szLen, psz[cch] will write one character after the end of the buffer (it should be psz[cch-1]).

Also the alternate code is wrong. The reason why it will often works is because szLen is greater that 4 in your case. Doing a sizeof on the returned size return the size of a variable of type size_t (which is probably an unsigned int or an unsigned long) and not the value hold in the variable.

Note that you uses assertion in your code to validate what you are doing. For ex. adding ASSERT(strlen(psz) < szLen) would have typically catch the error after the memory is overwritten but before the application crash so it would then be easy to apply the fix!

You should also check the result of the call to WideCharToMultiByte. If it fails (and it can), the output will contains garbage (if it wasn't initialized externally).

Also if output is really multibyte, then some UNICODE characters may be converted to 2 multibyte characters and it that case, the code would be incorrect.

Finally, it may be easier (and less error-prone) to uses macros like W2A (or classes like _bstr_t, CString, CComBSTR,...) to make conversions if you do not needs the extra functionality accessible only from the API.

In a few words: length and size are different... Knows what you are doing. Also, you may allocate a bit more memory than necessary to be on the safe side.


Philippe Mori
GeneralPlease answer. CReBar Pin
kursatkaraca9-Apr-02 10:58
kursatkaraca9-Apr-02 10:58 
GeneralRe: Please answer. CReBar Pin
Shog99-Apr-02 16:22
sitebuilderShog99-Apr-02 16:22 
QuestionDoes file exist? Pin
Rickard Andersson209-Apr-02 10:14
Rickard Andersson209-Apr-02 10:14 
AnswerRe: Does file exist? Pin
Mike Nordell9-Apr-02 10:19
Mike Nordell9-Apr-02 10:19 
AnswerRe: Does file exist? Pin
Jack Handy9-Apr-02 10:28
Jack Handy9-Apr-02 10:28 
AnswerRe: Does file exist? Pin
Joaquín M López Muñoz9-Apr-02 10:50
Joaquín M López Muñoz9-Apr-02 10:50 
GeneralRe: Does file exist? Pin
9-Apr-02 18:36
suss9-Apr-02 18:36 
AnswerRe: Does file exist? Pin
mynab9-Apr-02 22:22
mynab9-Apr-02 22:22 
Generaldialog return value Pin
9-Apr-02 10:05
suss9-Apr-02 10:05 
GeneralRe: dialog return value Pin
Joaquín M López Muñoz9-Apr-02 10:32
Joaquín M López Muñoz9-Apr-02 10:32 
GeneralRe: dialog return value Pin
9-Apr-02 10:43
suss9-Apr-02 10:43 
GeneralRe: dialog return value Pin
9-Apr-02 10:46
suss9-Apr-02 10:46 
GeneralMFC Controls in CDHtmlDialog Pin
shypht9-Apr-02 9:48
shypht9-Apr-02 9:48 
GeneralRe: MFC Controls in CDHtmlDialog Pin
Chris Losinger9-Apr-02 11:09
professionalChris Losinger9-Apr-02 11:09 
GeneralRe: MFC Controls in CDHtmlDialog Pin
Shog99-Apr-02 11:36
sitebuilderShog99-Apr-02 11:36 
GeneralRe: MFC Controls in CDHtmlDialog Pin
shypht9-Apr-02 15:55
shypht9-Apr-02 15:55 
GeneralRemove button from system tray Pin
radix9-Apr-02 9:39
radix9-Apr-02 9:39 

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.