Search code examples
c#visual-studio64-bitsgen

sgen.exe x64 .net c# fails with "assembly with an incorrect format"


I have ws2008 x64 with vs2008.

When I set my vs to x64 (because I have 64bit dlls) and run compilation sgen says that

An attempt was made to load an assembly with an incorrect format

VS takse sgen from C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\

and I think that it should take it from C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\x64\

when i take 64bit version of sgen and put it into C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\ (replace 32bit version). I was able to compile.

What should I do to point to the correct version of sgen under vs.

Can I somehow configure solutinon platforms for one project to point to the correct sgens (for x86 to 32 bit and for x64 to 64 bit sgen version)?


Solution

  • This is the best answer I could find: Conditional Post-Build Event Command for x64 sgen, a blog post by Michael Hanes.

    Use a post build event, that conditionally checks if the 64 bit SGEN is installed, and use it when needed:

    REM Use the 64-bit sgen from the Win 2008 and 
    REM .NET 3.5 SDK in a 64-bit dev environment
    REM ProgramFiles variable is set to 
    REM 'Program Files (x86)' in a x64 environment 
    REM Processor_Architecture variable returns x86 
    REM in both an x86 and x64 environment within VS.
    
    if /I "%ProgramFiles%" == "C:\Program Files" (
    set SgenToolPath="C:\Program Files\Microsoft
    SDKs\Windows\v6.0A\Bin\sgen.exe"
    ) else (
    set SgenToolPath="C:\Program Files\Microsoft
    SDKs\Windows\v6.1\Bin\x64\sgen.exe"
    )
    
    %SgenToolPath% /compiler:"\"/keyfile:$(ProjectDir)
    MyKeyFile.snk"\" /force "$(TargetPath)"
    

    This is intended to be a replacement for the "Generate Serialization Assemblies" dropdown setting for "On" for a given Visual Studio project.