c++ - Very Strange Behavior of LPCTSTR -


i've been working on system reversivly search directories findfirstfile , findnextfile i've encountered issue don't understand.

below code snippet.

int winapi winmain(hinstance hinstance,hinstance hprevinstance,lpstr lpcmdline                   ,int ncmdshow) {         searchdrive((lpctstr)"c:\\",(lpctstr)"*.bdjf");     return 0; }  bool searchdrive(lpctstr lpfolder, lpctstr lpfilepattern) {     tchar  szfullpattern[max_path];     win32_find_data findfiledata;     handle hfile = invalid_handle_value;     pathcombine(szfullpattern, lpfolder, l"x");     messagebox(null,szfullpattern,lpfilepattern,mb_iconwarning |                                                 mb_canceltrycontinue |                                                 mb_defbutton2);     handle hfind = findfirstfile(szfullpattern, &findfiledata); 

i'm using visual studio 2008.

enter image description here

as can see except last character , '\' can't seen, the rest have come out in asian characters.

(note don't worry of other issues code.)

any ideas on why happening appreciated.

here's problem:

searchdrive((lpctstr)"c:\\",(lpctstr)"*.bdjf"); 

by default, visual studio compiles programs in unicode mode. you're casting both of "ansi" (8-bit characters) strings "unicode" (16-bit character) string type.

this doesn't convert strings. tells compiler pretend unicode strings along. it's hardly surprising doesn't work; upshot each pair of ansi characters treated single unicode character.

you can fix problem this:

searchdrive(text("c:\\"), text("*.bdjf")); 

but unless have specific reason support ansi mode, better still use

searchdrive(l"c:\\", l"*.bdjf"); 

and change declaration of searchdrive use lpcwstr instead of lpctstr.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -