sgen.exe x64 .net c# 因“程序集格式不正确”而失败

发布于 2024-08-16 12:34:03 字数 470 浏览 12 评论 0原文

我有 ws2008 x64 和 vs2008。

当我将 vs 设置为 x64(因为我有 64 位 dll)并运行编译 sgen 时,显示

尝试加载格式不正确的程序集

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

我认为它应该从 C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\x64\

当我将 64 位版本的 sgen 放入 C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\ (替换 32 位版本)。我能够编译。

我应该怎么做才能在 vs. 下指向正确的 sgen 版本

?我可以以某种方式为一个项目配置解决方案平台以指向正确的 sgen(对于 x86 到 32 位以及 x64 到 64 位 sgen 版本)吗?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蹲在坟头点根烟 2024-08-23 12:34:03

这对你有帮助吗?看一下他使用 sgen 作为后期构建的部分 :

因此,您需要将 SGen 命令添加为 VS 项目属性的“构建事件”选项卡上的自定义构建后事件:

<代码>“C:\ Program Files \ Microsoft Visual Studio 8 \ SDK \ v2.0 \ Bin \ sgen.exe”/force / assembly:“$(TargetPath)”/编译器:/ keycontainer:VS_KEY_5EFB7881D71082EDCF85DBBFCD748B9A /编译器:/delaysign-

Does this help you out? Take a look at the section where he uses sgen as a post build:

As a consequence you need to add the SGen command as a custom post-build event on the Build Events tab of your VS project properties:

"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\sgen.exe" /force /assembly:"$(TargetPath)" /compiler:/keycontainer:VS_KEY_5EFB7881D71082EDCF85DBBFCD748B9A /compiler:/delaysign-

三岁铭 2024-08-23 12:34:03

这是我能找到的最佳答案: Conditional Post-为 x64 sgen 构建事件命令Michael Hanes 的博客文章。

使用构建后事件,有条件地检查是否安装了 64 位 SGEN,并在需要时使用它:

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)"

这旨在替代给定 Visual Studio 项目的“生成序列化程序集”下拉设置“打开”。

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.

悟红尘 2024-08-23 12:34:03

添加一些预构建操作只是为了转储在构建时有效的环境变量。

检查 vcvarsall.bat 并按照它加载不同主机/构建平台组合的其他 bat-s。

检查 devenv 进程的实际位数(例如使用进程资源管理器)。

Add a little pre-build action just to dump out env vars that are in effect at build time.

Check vcvarsall.bat and follow it as it loads other bat-s for different host/build platform combos.

Check actual bitness of devenv process (say with process explorer).

寻梦旅人 2024-08-23 12:34:03

此博文上发布了关于指定 __SdkSgenTool 有条件地:

唯一缺少的是我确实需要将 SGenToolPath 设置为我的构建输出目录。正如预期的那样,这更困难,因为作为普通属性,它被其他 MsBuild 任务覆盖。最终有效的解决方案是创建已经存在的属性,并在没有其他任务干扰的情况下将值设置为其最终值。

下面是使 Sgen 在 64 位下工作的“代码”。您需要在所有构建模式中定义 __SdkSgenTool 变量,因为无论构建模式如何,都会执行复制等构建后步骤。

 
   ....
    <生成序列化程序集>打开
    false
    <__SdkSgenTool Condition="exists('C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe')">C:\Program Files (x86) )\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe
    <__SdkSgenTool Condition="exists('C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe')">C:\Program Files\Microsoft SDKs\Windows \v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe
  
...

  <导入项目=“$(MSBuildToolsPath)\Microsoft.CSharp.targets”/>
  <目标名称=“BeforeBuild”>
    <复制 SourceFiles="$(__SdkSgenTool)" DestinationFiles="$(TargetDir)\sgen.exe" SkipUnchangedFiles="true" />
    
      
    

我听说这个问题将在 VS2012 中得到解决,这是一个很好的解决方案
东西。

VS2012 中似乎没有修复此问题。我会谨慎使用它,因为 __SdkSgenTool 似乎是一个内部属性,因此不是您可以依赖的东西。

There's a different solution posted on this blog post about specifying __SdkSgenTool conditionally:

The only missing thing is that I do need to set SGenToolPath to my build output directory. This was harder as expected since as a normal property it was overwritten by other MsBuild tasks. The solution that finally did work was to create the already existing property and set the value to its final value when no other tasks could interfere.

Below is the “code” to make Sgen work in 64 bit. You need to define the __SdkSgenTool variable in all build modes since the post build steps like copy are executed regardless of the build mode.

 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
   ....
    <GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>
    <SGenUseProxyTypes>false</SGenUseProxyTypes>
    <__SdkSgenTool Condition="exists('C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe')">C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe</__SdkSgenTool>
    <__SdkSgenTool Condition="exists('C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe')">C:\Program Files\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\sgen.exe</__SdkSgenTool>
  </PropertyGroup>
...

  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Target Name="BeforeBuild">
    <Copy SourceFiles="$(__SdkSgenTool)" DestinationFiles="$(TargetDir)\sgen.exe" SkipUnchangedFiles="true" />
    <CreateProperty Value="$(TargetDir)">
      <Output TaskParameter="Value" PropertyName="SGenToolPath" />
    </CreateProperty>

I have heard that this issue will be fixed with VS2012 which is a good
thing.

This doesn't appear to be fixed in VS2012. I would use this with caution, because __SdkSgenTool appears to be an internal property, and therefore not something you can rely on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文