I have been playing with writing Unicode code using Windows API and have found one thing particularly frustrating.
I made a simple interface for wrapping "MessageBox" into an "alert", reducing number of arguments needed to call it from 4 to 1.
The issue is that this one argument MUST be called with L"My string", which I want to avoid.
Here is my interface so far:
UseCase.hpp
#pragma once
#include <string>
#include <iostream>
/**
* The User should not need to know any Windows API ideally.
* They will have to know L"Str" notation though...
*/
namespace UseCase
{
void alert(const wchar_t *message);
};
UseCase.cpp
#include <Windows.h>
#include "UseCase.hpp"
/**
* Kind-of like javascript:alert(message).
* This is a very common usecase, and heavily simplifies call convention.
*/
void UseCase::alert(const wchar_t *message)
{
MessageBox(0, message, L"message box", MB_OK | MB_ICONEXCLAMATION);
}
main.cpp
#include "UseCase.hpp"
using namespace UseCase;
const wchar_t *msg = L"Привет мир";
int wmain(int argc, wchar_t **argv)
{
alert(msg);
return 0;
}
My concern is that no matter how the main tries to call alert, it has to use L"String" notation which is visually annoying to me.
To elaborate why L is annoying to me, it is for two reasons:
- It treats Rvalue string literals differently from variables containing an ascii string.
- If you try to call it on an ascii string "hello, world", it will give a confusing error message to an average user.
- It's obvious that the string cannot be stored in ascii, and you are trying to assign it to a unicode string, there shouldn't be much getting in a way of an automatic conversion.
Is there a way to get rid of the L"String" convention and make the program automatically treat the function input as wide character input?
Current viable solutions are:
- Create a macro for calling messagebox with a string literal that wraps it in L for you. The issue with this approach is the cryptic error messages on variables passed into the function, as L cannot convert variables from ascii to unicode for you.
- Others proposed but I haven't fully wrapped my head around implementing them.
- Have the user register their string externally and stored as unicode inside a file/binary cache. I can then provide an interface to load this string as a wide string and call the function using it.