# How to force VTK use the Unicode version of MFC?

**URL:** https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712
**Category:** Support
**Created:** [November 14, 2023, 10:17pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712 "2023-11-14T22:17:52Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Paavo\_Helde](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paavo_helde/32/8016_2.png) [@Paavo\_Helde](https://discourse.vtk.org/u/Paavo_Helde)
#### Post date: [November 14, 2023, 10:17pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/1 "2023-11-14T22:17:52Z")

</div>

We are in the process of upgrading to VTK v9.2.6. We have a Windows MFC program and it gives debug asserts in AfxGetInstanceHandle(), apparently because the VTK vtkGUISupportMFCd.dll appears to be linked to mfc140d.dll, not mfc140ud.dll like the rest of our code. How to force it to link against the Unicode version of MFC? Adding -DUNICODE to CXXFLAGS did not help. This is our current build of vtk, in a Windows bash shell script:

```auto
    export CMAKE_GENERATOR='Visual Studio 16 2019'
    export CXXFLAGS="-DUNICODE -D_UNICODE -Od -MP"
    cmake -A x64 -Wno-dev \
	-S $DOWNLOAD_ROOT/$PROJECT \
	-B $BUILD_ROOT/$PROJECT \
	-D CMAKE_INSTALL_PREFIX=$INSTALL_ROOT/$PROJECT \
	-D CMAKE_CONFIGURATION_TYPES=Debug \
	-D VTK_MODULE_ENABLE_VTK_GUISupportMFC=YES \
	-D VTK_RELOCATABLE_INSTALL=ON \
	-D VTK_VERSIONED_INSTALL=OFF \
	-D BUILD_SHARED_LIBS=ON

```

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [November 14, 2023, 11:13pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/2 "2023-11-14T23:13:09Z")

</div>

Might be a cmake issue. Instead of changing the CXXFLAGS globally, you can try setting the flags by editing `GUISupport/MFC/CmakeLists.txt` around line 60:

```auto
  vtk_module_definitions(VTK::GUISupportMFC
    PRIVATE                                                          
     _AFXDLL _UNICODE UNICODE)

```

But really I’m not sure what magic is required for CMake to tell Visual Studio to link to the right MFC library. Maybe an MFC/CMake expert is reading this and can chime in.

---

<div class="post-metadata">

### Author: ![Paavo\_Helde](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paavo_helde/32/8016_2.png) [@Paavo\_Helde](https://discourse.vtk.org/u/Paavo_Helde)
#### Post date: [November 15, 2023, 7:03am UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/3 "2023-11-15T07:03:52Z")

</div>

Thanks, hacking the CMakeLists.txt indeed got this thing working!

This page [https://gitlab.kitware.com/vtk/vtk/-/issues/18115](https://gitlab.kitware.com/vtk/vtk/-/issues/18115) suggests VTK is now using UTF-8 internally and has thus removed UNICODE everywhere. Our own code also uses UTF-8 internally, so that would be fine, but the thing is that I also need to link against a pile of legacy MFC-based libraries which have no idea about UTF-8 and require to be built with UNICODE in order to cope.

Suspecting that the GUISupport/MFC/CmakeLists.txt should still define UNICODE, maybe in a configurable way.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [November 15, 2023, 1:04pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/4 "2023-11-15T13:04:12Z")

</div>

> [@Paavo\_Helde](#):
>
> Suspecting that the GUISupport/MFC/CmakeLists.txt should still define UNICODE, maybe in a configurable way.

But as far as I understand, it never did define UNICODE, and the CXXFLAGS has always been the only way for people to do a UNICODE build of VTK (unless they manually edit the CMakeLists.txt).

So I’m a bit confused about why this MFC unicode issue would appear for VTK 9.2, but not for older versions of VTK.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [November 17, 2023, 4:30am UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/5 "2023-11-17T04:30:25Z")

</div>

My guess is that the library to link is specified using `#pragma comment(lib, "…")` (“autolinking”) based on the preprocessor state. I don’t see where VTK ever defined `_UNICODE` or `UNICODE` itself either.

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [November 17, 2023, 11:36pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/6 "2023-11-17T23:36:11Z")

</div>

[https://gitlab.kitware.com/vtk/vtk/-/merge\_requests/7865#7f25decd1a082913057526868a1d59fc32daa6aa](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/7865#7f25decd1a082913057526868a1d59fc32daa6aa)

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [November 18, 2023, 3:03am UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/7 "2023-11-18T03:03:04Z")

</div>

I know we removed _usage_ of it, but I’ve not found where VTK defined `-DUNICODE` or the like; it seems like it was always a user-selection thing via `CMAKE_CXX_FLAGS` or something.

---

<div class="post-metadata">

### Author: ![Paavo\_Helde](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paavo_helde/32/8016_2.png) [@Paavo\_Helde](https://discourse.vtk.org/u/Paavo_Helde)
#### Post date: [November 20, 2023, 11:25am UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/8 "2023-11-20T11:25:42Z")

</div>

I found where UNICODE was defined in VTK 7.1.0 : in GUISupport/MFC/VTKMFCSettings.cmake , included by GUISupport/MFC/CMakeLists.txt :

```auto
# Newer versions of MSVC do not support non-Unicode MFC, which is a good.
# However, we need to convince also CMake about that:
ADD_DEFINITIONS(-D_UNICODE)

```

It looks like newer VTK versions do not contain this file any more.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [November 20, 2023, 12:56pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/9 "2023-11-20T12:56:45Z")

</div>

I’m not seeing that in VTK’s history…

Anyways, if we need it for modern MFC, let’s get the MFC module to do what is necessary.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [November 20, 2023, 2:17pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/10 "2023-11-20T14:17:46Z")

</div>

I think that the comment about MSVC not supporting non-Unicode MFC is out-of-date. After some digging, I found that this may have been the case for Visual Studio 2013 and 2015, but not for Visual Studio 2017 and later:  
[https://learn.microsoft.com/en-us/cpp/mfc/mfc-mbcs-dll-add-on](https://learn.microsoft.com/en-us/cpp/mfc/mfc-mbcs-dll-add-on)

The current MSVC documentation makes it very clear that both Unicode and non-Unicode versions of the library are supported:  
[https://learn.microsoft.com/en-us/cpp/mfc/mfc-library-versions](https://learn.microsoft.com/en-us/cpp/mfc/mfc-library-versions)

There is also documentation on the use of `_UNICODE` with MFC:  
[https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/unicode-and-multibyte-character-set-mbcs-support](https://learn.microsoft.com/en-us/cpp/atl-mfc-shared/unicode-and-multibyte-character-set-mbcs-support)

So it seems that `_UNICODE` in MFC needs to be configurable, and possibly `_MBCS` as well.

Edit: another useful link, showing the options that are available in the Visual Studio GUI itself:  
[https://learn.microsoft.com/en-us/cpp/build/reference/general-property-page-project](https://learn.microsoft.com/en-us/cpp/build/reference/general-property-page-project)

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [November 20, 2023, 2:38pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/11 "2023-11-20T14:38:59Z")

</div>

Given that VTK is UTF-8 everywhere now (much thanks to @toddy), do we want the Unicode version? IIUC, UTF-8 uses the `A` functions, but Unicode would be the `W` variants. I’m…not sure what that means for MFC.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [November 20, 2023, 3:00pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/12 "2023-11-20T15:00:59Z")

</div>

In general, we would want the Unicode version, for the same reason that the KWSys CMakeLists.txt uses `add_definitions(-DUNICODE -D_UNICODE)`.

When VTK made the utf-8 switchover, it switched to using KWSys to open the files, and VTK uses KWSys to perform the conversion from wide characters to utf-8 and back again. So VTK does its IO via the ‘W’ functions in KWSys, not via the ‘A’ functions.

The wrapping tools, which don’t use KWSys, explicitly use the ‘W’ functions for all I/O, and call `MultiByteToWideChar(CP_UTF8, ...` directly.

---

<div class="post-metadata">

### Author: ![Paavo\_Helde](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paavo_helde/32/8016_2.png) [@Paavo\_Helde](https://discourse.vtk.org/u/Paavo_Helde)
#### Post date: [November 20, 2023, 3:50pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/13 "2023-11-20T15:50:47Z")

</div>

The traditional way to use Unicode with MFC has been to use UNICODE and W functions. Given that most MFC projects are probably legacy and nobody wants to port them to use UTF-8 instead (which was not even possible in recent past, not sure about current state), I believe VTK should support the MFC UNICODE version, probably in a configurable way.

---

<div class="post-metadata">

### Author: ![Paavo\_Helde](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paavo_helde/32/8016_2.png) [@Paavo\_Helde](https://discourse.vtk.org/u/Paavo_Helde)
#### Post date: [November 20, 2023, 4:22pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/14 "2023-11-20T16:22:24Z")

</div>

I now found out from svn history that the UNICODE define was added by our own team, so indeed it was not in official VTK. Sorry for the noise!

---

<div class="post-metadata">

### Author: ![toddy](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/t/edb3f5/32.png) [@toddy](https://discourse.vtk.org/u/toddy)
#### Post date: [November 20, 2023, 11:57pm UTC](https://discourse.vtk.org/t/how-to-force-vtk-use-the-unicode-version-of-mfc/12712/15 "2023-11-20T23:57:23Z")

</div>

Actually UTF-8 uses the W variants via KWSYS conversion.

The only places where I used the A variants for the Windows API was for hard-coded ANSI text in the source code or class names, which I assumed would always be plain English.
