I have an unmanaged VC++ project that I can build for both x86 and x64 targets. It is being consumed by a .NET program that is compiled for Any CPU. This is on VS2010, but I had the same issue when the solution was built with VS2008 and vcbuild.
Here is the build command in my build script:
msbuild .\Project.vcxproj /t:Build /p:PlatformToolset=v100 /p:Configuration=Release /p:Platform=%platform%
If build on an x86 machine, with %platform%=Win32, everything builds and executes fine
If build on an x64 machine, with %platform%=x64, everything builds and executes fine.
However, if I build on an x64 machine, with %platform%=Win32, it builds successfully with no errors. But when I copy those supposedly-x86 binaries to an x86 machine, they cause the following error:
System.Runtime.InteropServices.SEHException was caught
Message=External component has thrown an exception.
Tracing that error back to the C++ dll, it failed on this line:
if ( FAILED( g_Connection.CreateInstance( _T("ADODB.Connection") ) ) )
ThrowException(_T("Cannot create connection."));
I've gone through my .vcxproj file looking for issues but everything appears correct. There are no imported property files or custom build tasks or anything like that.
This is causing issues because we are trying to use a single x64 build server to create builds for both platforms. Our current workaround involves pre-building the C++ dll on two different machines.