天天看点

MSBuild 4.0 or VS2010 may fail to compile a 32bit application targeted for .Net Framework

MSBuild 4.0 or Visual Studio 2010 may fail to compile a 32-bit application targeted for .Net Framework 3.5, 3.0 or 2.0 on x64 machine.

SYMPTOMS

You are building an application targeted for x86 configuration on x64 machine with Visual Studio 2010. The project contains a resource file referencing a 32-bit assembly. Target Framework selected is 3.5, 3.0 or 2.0.

You may get the following error:

Form1.resx(161,5):error RG0000: Could not load file or assembly '<file_name>' or one of its dependencies. An attempt was made to load a program with an incorrect format. Line 161, position 5.

The diagnostic build-log shows that task “GenerateResource” fails.

Resgen.exe referenced:

"<system_drive>:\Program Files (x86)\Microsoft SDKs\Windows\vx.x\bin\ResGen.exe"

CAUSE

Resgen.exe, which is a part of Windows SDK, is marked as MSIL and hence it will run as a 64-bit process. It will try to load a 32-bit assembly and fail.

RESOLUTION

In order to work-around this issue you may need to perform the following steps:

1. Close all instances of Visual Studio.

2. From the Visual Studio Tools subfolder, open an elevated “Visual Studio Command Prompt (2010)” (using “Run as administrator” option). Change directory to "<system_drive>:\Program Files (x86)\Microsoft SDKs\Windows\v<x.xx>\bin\”.

eg:C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin

3. Issue the command :

corflags /32bit+ ResGen.exe /force

4. Open <project_name>.csproj in notepad.

5. Add the following property <ResGenToolArchitecture>Managed32Bit</ResGenToolArchitecture> under the PropertyGroup section. Save and close the csproj file.

MORE INFORMATION

Note: If, later on, you want to build a .resx file that references a *64* bit assembly targeting v3.5, v3.0 or v2.0 you may have toreverse this workaround before doing so.

继续阅读