1

I have two different projects in C++, one compiles to a static library and uses Unicode character encoding as it has to support multiple languages (Spell Checker), the other is a legacy MFC project that uses a MultiByteCharacterSet encoding.

The legacy project has to use the library Project (to Spell check the contents of a richedit control interactively).

when I build a lone project with a richedit control, set the character encoding to unicode and reference the library, it works fine. but where I try to reference the library from the legacy project the same way I have done with the lone project, it doesn't work.

I investigated and realised that the TEXTRANGE structure that is being populated by the EM_GETTEXTRANGE expects a Unicode value in its lpstrtext member, which wouldn't be the case for the legacy project. the implementation in the library is as follows:

TEXTRANGE txtRange;
  memset (&txtRange, 0, sizeof(txtRange));
  memset (bufW, 0, sizeof (bufW));
  //debug_log("Size of TEXTRANGE: x: %d and Size of BufW: %d.", sizeof(txtRange), sizeof(bufW));
  txtRange.lpstrText  = string_from_unicode_cp(bufW, CP_ACP);
  txtRange.chrg.cpMin = 0;
  txtRange.chrg.cpMax = _countof(bufW);
  debug_log("count of bufw: %s.", bufW);
  SendMessage (hwnd, EM_GETTEXTRANGE, 0, (LPARAM)&txtRange);
  debug_log("txtRange.lpstrText is: %S",txtRange.lpstrText);

but I get the error:

SpellChecker.cpp(215): error C2440: '=' : cannot convert from 'char *' to 'LPWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Visual Studio says that the lpstrtext member int he library is LPWSTR (perhaps because of the UNICODE format selected), which is understandable.

My question is, Is there a way to force lpstrtext member to expect LPSTR (cast) even when the project that contains it, is set to use unicode.

Ashahmali
  • 113
  • 7
  • You probably need the [`MultiByteToWideChar`](https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072(v=vs.85).aspx) function. – Jabberwocky Jan 26 '16 at 13:21

1 Answers1

2

TEXTRANGE is a macro:

#ifdef UNICODE
#define TEXTRANGE   TEXTRANGEW
#else
#define TEXTRANGE   TEXTRANGEA
#endif // UNICODE 

So you should be able to use TEXTRANGEA instead to force char* type.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
  • Thanks @Vlad, your response answers my question but doesn't solve my problem unfortunately. – Ashahmali Jan 26 '16 at 15:04
  • Well, may be you should ask another question then :) Anyway, most Microsoft API functions are macros defined based on your _UNICODE setting. This makes it hard to mix _UNICODE with MBCS. But if you must - your library should clearly expose two versions of each function, to be called explicitly by its clients. – Vlad Feinstein Jan 26 '16 at 15:08
  • Thanks @Vlad, your response answers my question but doesn't solve my problem unfortunately. Put in another way, is there a way to create a Richedit control in an MBCS encoded MFC application, but the richedit should be Unicode encoded (or a way to convert the character encoding of an MFC richedit HWND from MBCS to UNICODE without changing the encoding format of the whole project) . – Ashahmali Jan 26 '16 at 15:10
  • 1
    I am trying to understand the API. It appears that you pass a handle to your CRichEdit control window to the spell check library, and it then communicates with that window via SendMessage() - is that right? The library assumes that the rich edit is in UNICODE, so to work with this library correctly you need to create your control as UNICODE. You just have no opportunity to do an MBCS-to-UNICODE conversion, as that conversation doesn't involve you, it seams. You **CAN** have a UNICODE control in MBCS app, but you would have to rework the internals of that legacy app, which I assume is bad. – Vlad Feinstein Jan 26 '16 at 17:12
  • Exactly Vlad, Thats exactly what is happening and Exactly what am trying to do (You are a hero).. I have now found a temporary workaround. but I hope I don't get to support any of the wide character specific languages anytime soon.. I will update this post with the result of my research moving forward. I would vote up you response but stackoverflow says I cannot vote until I attain a reputation 15, but Thanks a lot man your response really helped. – Ashahmali Jan 26 '16 at 17:23