Skip to main content
Participant
May 24, 2021
Answered

[C++] Chinese characters in SetEventStringUnicode (wanted English)

  • May 24, 2021
  • 1 reply
  • 619 views

I've wrapped your log system:

 

int log_info(const char *message) {
    if (strlen(message) == 0) return EXIT_FAILURE;

#ifdef PLUGIN_MODE
    if (sErrorSuitePtr == NULL) return EXIT_FAILURE;
    sErrorSuitePtr->SetEventStringUnicode(PrSDKErrorSuite3::kEventTypeInformational,
                                          reinterpret_cast<prUTF16Char*>((wchar_t*)(PROJECT_NAME)),
                                          reinterpret_cast<prUTF16Char*>((wchar_t*)(message)));
#else
    puts(message);
#endif
    return EXIT_SUCCESS;
}

 

The C style cast is because `const_cast` failed with this error

C++ a const_cast can only adjust type qualifiers; it cannot change the underlying type

 

Output [first alert is `log_info("info successfully")`]:

Should I be using `SetErrorString` or something? - I'd prefer to use the Unicode ones in case I want to output the filename at some stage.

This topic has been closed for replies.
Correct answer Samuel5E6F

I ended up using`mbstowcs_s` doing:

#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
static const std::wstring PROJECT_NAME_W(WIDEN(PROJECT_NAME));
#ifdef PLUGIN_MODE
static prUTF16Char* PROJECT_NAME_UTF16 = const_cast<prUTF16Char *>(PROJECT_NAME_W.c_str());
#endif

prUTF16Char * to_wchar(const char* message) {
    const size_t cSize = strlen(message);
    wchar_t *w_str = new wchar_t[cSize];
    size_t outSize;
    mbstowcs_s(&outSize, w_str, cSize, message, cSize-1);
    return w_str;
}

int log_info(const char *message) {
    if (strlen(message) == 0) return EXIT_FAILURE;

#ifdef PLUGIN_MODE
    if (sErrorSuitePtr == NULL) return EXIT_FAILURE;

    fputs(message, stderr);
    prUTF16Char *w_msg = to_wchar(message);
    sErrorSuitePtr->SetEventStringUnicode(PrSDKErrorSuite3::kEventTypeInformational,
                                          w_msg,
                                          PROJECT_NAME_UTF16
    );
    free(w_msg);

#else
    puts(message);
#endif
    return EXIT_SUCCESS;
}

 

1 reply

Bruce Bullis
Legend
May 24, 2021

Have a look at copyConvertStringLiteralIntoUTF16(), in SDK_File.cpp. It may do what you want.

Samuel5E6FAuthorCorrect answer
Participant
May 30, 2021

I ended up using`mbstowcs_s` doing:

#define WIDEN2(x) L ## x
#define WIDEN(x) WIDEN2(x)
static const std::wstring PROJECT_NAME_W(WIDEN(PROJECT_NAME));
#ifdef PLUGIN_MODE
static prUTF16Char* PROJECT_NAME_UTF16 = const_cast<prUTF16Char *>(PROJECT_NAME_W.c_str());
#endif

prUTF16Char * to_wchar(const char* message) {
    const size_t cSize = strlen(message);
    wchar_t *w_str = new wchar_t[cSize];
    size_t outSize;
    mbstowcs_s(&outSize, w_str, cSize, message, cSize-1);
    return w_str;
}

int log_info(const char *message) {
    if (strlen(message) == 0) return EXIT_FAILURE;

#ifdef PLUGIN_MODE
    if (sErrorSuitePtr == NULL) return EXIT_FAILURE;

    fputs(message, stderr);
    prUTF16Char *w_msg = to_wchar(message);
    sErrorSuitePtr->SetEventStringUnicode(PrSDKErrorSuite3::kEventTypeInformational,
                                          w_msg,
                                          PROJECT_NAME_UTF16
    );
    free(w_msg);

#else
    puts(message);
#endif
    return EXIT_SUCCESS;
}