2

I inherited a c++ visual studio project that I can't get to build. The problem is that a lot of api calls like "CreateEvent" have char* inputs being fed to them but the error code expects "LPCWSTR." It's literally hundreds of lines of code with more or less a similar complaint.

Searching for help, the common solution seems to be that I should disable using the Unicode Character Set. However, when I do this, I get the "Building an MFC project for a non-Unicode character set is deprecated" error. Searching for help, the common solution seems to be that I should enable the Unicode Character Set.

So I'm hosed if I do, hosed if I don't. What's the right move here?

user2913869
  • 481
  • 2
  • 5
  • 23
  • May be you find useful this: https://stackoverflow.com/questions/19715144/how-to-convert-char-to-lpcwstr – Genaro Morales Jul 19 '17 at 19:54
  • You should update your entire codebase to properly use wide characters. Actually this should've been done 15 years ago. – user7860670 Jul 19 '17 at 20:08
  • "char*" is still standard on UTF-8. Most of software engineers hate "wchar_t*" and adding L for each string expression. It looks like Microsoft is the only one to force programmers to use UNICODE. Microsoft wants to discontinue MBCS but I don't think it's going to happen sometime soon. – JazzSoft Jul 19 '17 at 20:38
  • @JazzSoft in this case it has nothing to do with UTF-8. In WinAPI, you either have UTF-16 or ASCII. – Vasiliy Galkin Jul 19 '17 at 21:45
  • What I meant was there are so many libraries using "char*" as text string in the world and Microsoft would not discontinue MBCS. I developed multi-platform libraries and it's based on MBCS/UTF-8. I didn't want to use UNICODE to develop my libraries. If you write code in UNICODE, your code may not be portable for other OS like Linux, iOS, macOS, etc. – JazzSoft Jul 19 '17 at 23:03

2 Answers2

3

CreateEvent itself is nothing but a macro #define'd to be one of the two functions which are actually declared in WinAPI's synchapi.h

#ifdef UNICODE
#define CreateEvent  CreateEventW
#else
#define CreateEvent  CreateEventA
#endif // !UNICODE

Normally (at least in all Windows projects I used to work with) this UNICODE macro is defined, so in fact one works with CreateEventW function, which accepts LPCWSTR (in other words const wchar_t*), words argument, which is supposed to be a UTF-16 string literal. If it is not defined in your project, then it uses CreateEventA, which accepts LPCSTR argument (in fact, equivalent to const char*), and treats it as ASCII string.

Your project seems to have been set up to support ASCII strings only. And...well, now this is deprecated :) Previously there was some special add-on to enable builds for such situations, called MFC MBCS DLL Add-on. However, I'm not 100% sure it's still available, here's a question here at StackOverflow about that.

If you don't find such addon or any alternative solution like that, then I'm afraid the only way to make it compile will be porting your project to Unicode strings. Actually, even if you find a workaround, I suggest you to add this activity to your backlog if you're going to maintain this project any further, because such workaround may cease to exist at any moment.

In any case, it would be interesting to learn about your end result.

Vasiliy Galkin
  • 1,894
  • 1
  • 14
  • 25
  • I'm using Visual Studio 2017 and didn't need to install MBCS add-on, although it was needed for VS 2015. That means Microsoft realized MBCS support is needed at this point. – JazzSoft Jul 19 '17 at 23:07
0

It seems you are using Microsoft Visual Studio 2013. This version was distributed without the MBCS version of MFC. On the other hand Visual Studio 2015 and 2017 come with the MBCS version of MFC.

If you want to build your project with MBCS, you need to download and install this:

https://www.microsoft.com/en-us/download/details.aspx?id=40770

The other option is converting your whole project to Unicode, but this can be a lot of work depending on your project.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115