Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. unicode trouble

unicode trouble

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Ernesto D
    wrote on last edited by
    #1

    Hello all, im trying to learn about implementing unicode support in my apps, but i cant seem to get it straight, heres a simple example: //////////////////// test.cpp ////////////////////// #include #include #include int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) { HANDLE hmutex=CreateMutex(NULL, TRUE, _T("mymutex")); if(GetLastError()==ERROR_ALREADY_EXISTS) { // allready up, so get outa here return 0; } // use some string functions & write some txt to a file TCHAR str1[50]=_T("string1"); TCHAR str2[20]=_T("string 2"); strcat(str1, str2); FILE* pfile=fopen(_T("thefile.txt"), _T("w")); if(pfile) { fprintf(pfile, _T("the full string is:%s"), str1); fclose(pfile); } // Remove the mutex, so we can run next time ReleaseMutex(hmutex); return 0; } ibe added _UNICODE to the preprocesor definitions in project->settings, and when i complile, i get this: Compiling... test.cpp d:\code\test\test.cpp(9) : error C2664: 'CreateMutexA' : cannot convert parameter 3 from 'unsigned short [8]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(19) : error C2664: 'strcat' : cannot convert parameter 1 from 'unsigned short [50]' to 'char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(21) : error C2664: 'fopen' : cannot convert parameter 1 from 'unsigned short [12]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(24) : error C2001: newline in constant d:\code\test\test.cpp(24) : error C2143: syntax error : missing ')' before 'string' d:\code\test\test.cpp(24) : error C2664: 'fprintf' : cannot convert parameter 2 from 'unsigned short [22]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Error executing cl.exe. test.exe - 6 error(s), 0 warning(s) if i also define UNICODE (apart from _UNICODE), the error about CreateMutex() goes away, but not the other ones, its obvious that the char versions of these functions are being called instead of the wide character versions!. i tought the whole point of using TCHAR and the _T() macros, was that if i decide to build my

    I M E 3 Replies Last reply
    0
    • E Ernesto D

      Hello all, im trying to learn about implementing unicode support in my apps, but i cant seem to get it straight, heres a simple example: //////////////////// test.cpp ////////////////////// #include #include #include int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) { HANDLE hmutex=CreateMutex(NULL, TRUE, _T("mymutex")); if(GetLastError()==ERROR_ALREADY_EXISTS) { // allready up, so get outa here return 0; } // use some string functions & write some txt to a file TCHAR str1[50]=_T("string1"); TCHAR str2[20]=_T("string 2"); strcat(str1, str2); FILE* pfile=fopen(_T("thefile.txt"), _T("w")); if(pfile) { fprintf(pfile, _T("the full string is:%s"), str1); fclose(pfile); } // Remove the mutex, so we can run next time ReleaseMutex(hmutex); return 0; } ibe added _UNICODE to the preprocesor definitions in project->settings, and when i complile, i get this: Compiling... test.cpp d:\code\test\test.cpp(9) : error C2664: 'CreateMutexA' : cannot convert parameter 3 from 'unsigned short [8]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(19) : error C2664: 'strcat' : cannot convert parameter 1 from 'unsigned short [50]' to 'char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(21) : error C2664: 'fopen' : cannot convert parameter 1 from 'unsigned short [12]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(24) : error C2001: newline in constant d:\code\test\test.cpp(24) : error C2143: syntax error : missing ')' before 'string' d:\code\test\test.cpp(24) : error C2664: 'fprintf' : cannot convert parameter 2 from 'unsigned short [22]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Error executing cl.exe. test.exe - 6 error(s), 0 warning(s) if i also define UNICODE (apart from _UNICODE), the error about CreateMutex() goes away, but not the other ones, its obvious that the char versions of these functions are being called instead of the wide character versions!. i tought the whole point of using TCHAR and the _T() macros, was that if i decide to build my

      I Offline
      I Offline
      Ian Darling
      wrote on last edited by
      #2

      Ernesto D. wrote: what im i doing wrong?? What you're doing wrong is assuming that the standard C library understands Unicode the same way the Win32 API does. Win32 defines unicode and ascii versions of all APIs (ie, CreateMutex is actually a macro that maps onto CreateMutexA or CreateMutexW as appropriate). The Standard C library implemented on Windows does have wide-character versions of fopen, fprintf etc (they're _wfopen and fwprintf) - look in MSDN. You may need to define your own macros to handle these seamlessly, but it's probably easier to use the Win32 API or MFC functions instead if you need unicode *and* ascii version of your application. I think there's a way of getting unicode support on 9X, but I can't remember how. You might also want to read this article[^] on Joel On Software. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky

      1 Reply Last reply
      0
      • E Ernesto D

        Hello all, im trying to learn about implementing unicode support in my apps, but i cant seem to get it straight, heres a simple example: //////////////////// test.cpp ////////////////////// #include #include #include int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) { HANDLE hmutex=CreateMutex(NULL, TRUE, _T("mymutex")); if(GetLastError()==ERROR_ALREADY_EXISTS) { // allready up, so get outa here return 0; } // use some string functions & write some txt to a file TCHAR str1[50]=_T("string1"); TCHAR str2[20]=_T("string 2"); strcat(str1, str2); FILE* pfile=fopen(_T("thefile.txt"), _T("w")); if(pfile) { fprintf(pfile, _T("the full string is:%s"), str1); fclose(pfile); } // Remove the mutex, so we can run next time ReleaseMutex(hmutex); return 0; } ibe added _UNICODE to the preprocesor definitions in project->settings, and when i complile, i get this: Compiling... test.cpp d:\code\test\test.cpp(9) : error C2664: 'CreateMutexA' : cannot convert parameter 3 from 'unsigned short [8]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(19) : error C2664: 'strcat' : cannot convert parameter 1 from 'unsigned short [50]' to 'char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(21) : error C2664: 'fopen' : cannot convert parameter 1 from 'unsigned short [12]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(24) : error C2001: newline in constant d:\code\test\test.cpp(24) : error C2143: syntax error : missing ')' before 'string' d:\code\test\test.cpp(24) : error C2664: 'fprintf' : cannot convert parameter 2 from 'unsigned short [22]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Error executing cl.exe. test.exe - 6 error(s), 0 warning(s) if i also define UNICODE (apart from _UNICODE), the error about CreateMutex() goes away, but not the other ones, its obvious that the char versions of these functions are being called instead of the wide character versions!. i tought the whole point of using TCHAR and the _T() macros, was that if i decide to build my

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        You have to do everything in Unicode. What you have now is TCHARs for the Win32 APIs and ANSI for the CRT functions, which (as you've seen) won't work. You need to use the TCHAR macros for the CRT functions, see The Complete Guide to C++ Strings, Part I - Win32 Character Encodings[^] for a longer explanation. --Mike-- Ericahist | CP SearchBar v2.0.2 | Homepage | RightClick-Encrypt | 1ClickPicGrabber You cannot stop me with paramecium alone!

        1 Reply Last reply
        0
        • E Ernesto D

          Hello all, im trying to learn about implementing unicode support in my apps, but i cant seem to get it straight, heres a simple example: //////////////////// test.cpp ////////////////////// #include #include #include int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) { HANDLE hmutex=CreateMutex(NULL, TRUE, _T("mymutex")); if(GetLastError()==ERROR_ALREADY_EXISTS) { // allready up, so get outa here return 0; } // use some string functions & write some txt to a file TCHAR str1[50]=_T("string1"); TCHAR str2[20]=_T("string 2"); strcat(str1, str2); FILE* pfile=fopen(_T("thefile.txt"), _T("w")); if(pfile) { fprintf(pfile, _T("the full string is:%s"), str1); fclose(pfile); } // Remove the mutex, so we can run next time ReleaseMutex(hmutex); return 0; } ibe added _UNICODE to the preprocesor definitions in project->settings, and when i complile, i get this: Compiling... test.cpp d:\code\test\test.cpp(9) : error C2664: 'CreateMutexA' : cannot convert parameter 3 from 'unsigned short [8]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(19) : error C2664: 'strcat' : cannot convert parameter 1 from 'unsigned short [50]' to 'char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(21) : error C2664: 'fopen' : cannot convert parameter 1 from 'unsigned short [12]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast d:\code\test\test.cpp(24) : error C2001: newline in constant d:\code\test\test.cpp(24) : error C2143: syntax error : missing ')' before 'string' d:\code\test\test.cpp(24) : error C2664: 'fprintf' : cannot convert parameter 2 from 'unsigned short [22]' to 'const char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast Error executing cl.exe. test.exe - 6 error(s), 0 warning(s) if i also define UNICODE (apart from _UNICODE), the error about CreateMutex() goes away, but not the other ones, its obvious that the char versions of these functions are being called instead of the wide character versions!. i tought the whole point of using TCHAR and the _T() macros, was that if i decide to build my

          E Offline
          E Offline
          Ernesto D
          wrote on last edited by
          #4

          Aha!, now i see the trouble(s). 1.- i DO have to define both _UNICODE and UNICODE (bummer) 2.- im using the char versions of the standard string functions (strlen, strcat, etc), instead of the TCHAR versions of them (_tcscat, _ftprintf, etc.) 3.- i forgot to UN-define _MBCS (dough!) just one more question. reading MSDN, i noticed 2 versions of winmain, one for mbcs and one for unicode, so, is there a tchar version of it too? and, i havent changed the winmain to _twinmain or whatever and still the app compiles fine with unicode defined. so, do i have to change the entry point of the app to point to it? (ok its 2 questions). thanks very much for your answers! ps. Michael, your article on strings RULES! (as usual) :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups