The problem is that when I choose Win32 build target, VS uses libraries from C:/Windows/SysWOW64 folder which is 64-bit libraries.
Nope. SysWOW64
contains the 32 bit libraries.
And when I choose x64 build target, VS uses libraries from C:/Windows/System32.
Yes, because the 64 bit libraries are located in System32
.
Before you ask "Wait, what?! What kind of drugs got the Windows developers intoxicated with?" let me tell you that this has its very well founded reasons. The problem is, that many programs have the System32 path hardcoded somewhere. And when these programs get recompiled for 64 bit these hardcoded paths remain and even though they're 64 bits, they're looking for the libraries at the System32 location. That's also the reason why the OpenGL interface DLL is named opengl32.dll
also on 64 bit systems.
When running 32 bit applications the filesystem name resolution transparently replaces the paths to resolve into the SysWOW64 directory.
So, in your question title you asked about a crash in ntdll.dll. What about that?