Click here to Skip to main content
15,846,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We are planning to migrate an MFC project to WinUI 3. The problem we are faced with is the MFC project we have is developed in MBCS, not UNICODE. To avoid problems in the new WinUI 3 project, we consider to setup the project without UNICODE.

When we create the new WinUI project (as blank project, packaged), the project is created for UNICODE by default. We have achieved configured for non Unicode undefining de UNICODE and _UNICODE defines, in the options of the project. That's ok, but when we compile the project, we have an error in one file generated (App.xaml.g.hpp), in one line like this:
C++
auto module = ::LoadLibrary(L"Microsoft.ui.xaml.dll");

The string is preceded by L"..." indicating is a UNICODE string. Because we configure to NO UNICODE, the compiler can't convert from wchar_t to LPCSRT.

We don't understand if we configured the preprocessor to undefine UNICODE why they generate code with Unicode string. Maybe we need to change any other configuration? or maybe the WinUI 3 only works in Unicode?

What I have tried:

We have changed the project options for undefine the UNICODE and the _UNICODE defines.
Posted
Updated 3-Oct-23 7:42am
v2

This has nothing to do with WinUI 3. Use of the L prefix on a string is a feature of the C / C++ language, independent of any preprocessor defines. An MBCS program can need the ability to use a UNICODE string, say when talking to an API that only speaks UNICODE. The L gives it that ability. Likewise, a UNICODE program can use MBCS strings by careful usage of data types and functions that always manipulate MBCS, regardless of preprocessor defines.

By not defining UNICODE, the Win32 API expects MBCS strings. LoadLibrary, a function in that API, must be supplied with an MBCS string as its parameter. You are trying to give it a UNICODE string. Simply remove the L from the start of the string. If this line of code had originally been written to be correct for both UNICODE and MBCS, it would have been:
auto module = ::LoadLibrary(_T("Microsoft.ui.xaml.dll"));

To quote MSDN: "Use the _T macro to code literal strings generically, so they compile as Unicode strings under Unicode or as ANSI strings (including MBCS) without Unicode"
 
Share this answer
 
Comments
Vicente Flich 3-Oct-23 8:32am    
Thanks for your solution, but my problem is not with the L prefix. My problem is that this code with the L prefix is generated automatically from the compiler by WinUI 3 and although I can modify to substitute with _T, when I compile again, the compile regenerate the code with L.

The question is how to configure a project WinUI 3 in Windows App SDK to generate the code in MBCS.
JudyL_MD is correct but there are a few more details involved. Nearly all of the Windows API comes in MBCS and Unicode flavors. The function LoadLibrary is one example. That function actually does not exist. There is LoadLibraryA and LoadLibraryW for MBCS and Wide character sets. If you do not define UNICODE or _UNICODE then the MBCS API will be used and that defines LoadLibrary to be LoadLibaryA. Regardless of which variant of the API your app uses by default, you can also be explicit with your usage. For example, you could write
C++
auto hmodule = LoadLibraryW( L"Microsoft.ui.xaml.dll" );
and that will work regardless of whether UNICODE is defined or not. Similarly, you can write
C++
auto hmodule = LoadLibraryA( "Microsoft.ui.xaml.dll" );
and it will also work. As previously mentioned, you can also write
C++
auto hmodule = LoadLibrary( _T( "Microsoft.ui.xaml.dll" ) );
and it will compile both with and without UNICODE being defined.

A brief anecdote: I recently wrote a chess game display program (for .PGN files) and all drawing was done in text mode in a dialog. There are chess pieces in several standard fonts so I used those. The thing is, those are characters in the 0x2600 range. To handle that, I defined the pieces as wide characters and then converted them into MBCS character strings (with WideCharToMultiByte) and then displayed them with standard Windows calls in my MBCS application. Incidentally, to make that work one must utilize a manifest file and specify UTF-8 as the active code page. Standard text calls like SetWindowText can then display UTF-8 strings in various languages.
 
Share this answer
 
Comments
Vicente Flich 3-Oct-23 8:34am    
Yes, thank you, but the code is generated by WinUI3, and I can't modify it. The question is how to configure the project to force the WinUI 3 to generate code in MBCS from .xaml files.
Rick York 3-Oct-23 12:57pm    
You are on the right course. Make sure UNICODE and _UNICODE are undefined and the program uses the MBCS. That should be enough to make it happen. You can always use wide characters where they are required even with those options. I think that is what needs to happen here. With those options, LoadLibrary will actually be calling the LoadLibraryA function. Since WinUI3 wants to use the wide character prefix for that library's name you can call LoadLibraryW and it should work just fine with the rest of your code in MBCS mode.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900