I had a similar problem: I saw the SGEN error "wrong format" when building in VS or MSBuild from the command line. My x64 project, but MSBuild insisted on using the 32-bit version of the tool. (Some of my colleagues are working on this, gathering in VS 2015, but I only have VS 2017 installed, and I want to keep it that way.)
Looking at the output of the diagnostic assembly, it seems that SGEN is working from a directory named by its SdkToolsPath parameter (for me: C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\ ). This is assigned from TargetFrameworkSDKToolsDirectory. Looking at the target files, this comes from the SDK40ToolsPath. And this is installed from the MSBuild.config file.
I solved this problem by editing C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe.config (requires administrator privilege) by setting the SDK40ToolsPath property using
<property name="SDK40ToolsPath" value="$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SDKs\NETFXSDK\4.6.2\WinSDK-NetFx40Tools-x64', 'InstallationFolder', null, RegistryView.Registry32))" />
(Note: If you are looking for this path in the registry on a 64-bit OS, go to HKLM \ SOFTWARE \ WOW6432Node \ Microsoft ...)
The main change, of course, from x86 to x64 is the use of 64-bit tools. I also changed the structure to what we use (4.6.2). This may mean that we can reliably use the tools only for 64-bit projects and for this environment, taking into account this change. However, I hope this can help someone encounter this problem. (I'm shocked and alarmed. MSBuild does not automatically change the path of tools based on Framework & Architecture.)
Keith Robertson Jul 31 '18 at 21:14 2018-07-31 21:14
source share