CString::Format issue

Hi,

I've recently been forced to change my project from using a MBCS to Unicode.  This has thrown up a number of problems with my code one of which is below.

text.Format ("Row %d,Col %d",ln,col);

Thats is the line that I had in when using MBCS, I know how to format strings etc under unicode, and the problem isnt the change, it is what would that string appears as under MBCS, I cant seem to find much on what the %d thing would do, so if some one could tell me how the string text would appear after that code was successful, that would be great

Thanks


Will



Answer this question

CString::Format issue

  • Normality2000

    _wtof is a function that only takes wchar_t chars and this is true if UNICODE is defined or not! Note that "string" is just an MBCS string and L"string" is a Unicode string, also this does not depend on the compiler settings for UNICODE.
    Nearly all functions exists in a Unicode and a MBCS version! You have to take care about this. If you don't take care you get thousands of errors.
    Note also that nearly all Windows API functions exist in two versions a MBCS and a Unicode version (SetWindowTextA and SetWindowTextW).

    So every string constant not defined with _T used in a function that maps to Unicode (like SetWindowText) will cause an error!

    Just search the MSDN for tchar, you find this link:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vccore/html/_core_generic.2d.text_mappings_in_tchar..h.asp

  • KBaghaei

    NO! I am using a lot of modules either in MBCS or Unicode, but I am using the tchar notation and I have no problems at all!

  • Michael Tsai

    Yes, I understand what your saying what am saying is this.

    There is one file called martin.cpp

    and two projects, project 1 uses unicode and project 2 uses MBCS.  But both projects use the file martin.cpp.  This would cause the problem because to get the file to compile under project 1 it would have to use the unicode versions of the API but to get it to compile under project 2 it would have to use the MBCS versions of the API.  So u see thats why i get the error, becuase the file martin.cpp can be one not the other.  I would have to have one file for MBCS and one for unicode.  Under these circumstances of sharing the file i would get the error correct

    Will

  • Mr. Jerzy

    So this is the reason for my thousands of errors, that i am using unicode versions of methods in the unicode project that is referenced from an MBCS project

    correct

    Will

  • Kerberos Mansour

    NO! If I use strictly the _w functions and the Unicode function in the API and L"string" for all constants, than you would get again NO errors!
    And again if I use just the MBCS versions and the correct string constants I would get no errors neither in an MBCS project nor in an Unicode project!

    You get problems because you never cared about the different MBCS and Unicode versions of the API!

  • Emphyrio

    Ok, thanks, just so i can check, this is a ligitimate reason why i am getting loads of errors, for example things like "_wtof" wouldnt compile under MBCS, and now do compile under Unicode.  The problem was the one project that was changed to using Unicode, was being referenced in another project (or rather they were sharing the same files). but becuase the other project was still in MBCS, i got thousands of errors come up.  This is likely to be the reason correct

    Incidently is there a really good article you know of on this   I can find some, but nothing great

    Will

  • larkahn

    Thats what I just said, i'm saying the reason why it failed is because the two are mixed,  its not a solution its the reason why the error is there in first place.  I agree


  • MickT

    ok, but if you werent using the tchar notation, thats why you would get all these errors
  • Rui Silva18093

    NO!
    If martin.cpp just contains this code and function:
    This function will work always:

    void FooA()
    {
       MessageBoxA(NULL, "MyFoo", "MyFoo", MB_OK);
    }

    The next too:

    void FooW()
    {
       MessageBoxW(NULL, L"MyFoo", L"MyFoo", MB_OK);
    }

    And this also

    void FooT()
    {
       MessageBox(NULL, _T("MyFoo"), _T("MyFoo"), MB_OK);
    }

    BUT THIS WILL FAIL IN A UNICODE COMPILE

    void FooFail()
    {
       MessageBox(NULL, "MyFoo", "MyFoo", MB_OK);
    }

    Because MessageBox will map to MessageBoxW!

    So what can you do:
    1. So use either always the Unicode versions
    2. Use always the MBCS versions
    3. Use the tchar versions.

    Never mix!



  • vtcoder

    Use ether:
    text.Format (L"Row %d,Col %d",ln,col);

    or
    text.Format (_T("Row %d,Col %d"),ln,col);

    I would prefer the later one, because it allows a project to be dynamically changed from MBCS to UNICODE and vice versa!




  • CString::Format issue