Error: 在64位操作系统上使用Visual Studio 2010生成ArcGIS Engine控件应用程序失败

文章编号 : 37879
软件: ArcGIS Engine Developer Kit 10
操作系统: Windows 2003Server, 2008Server, Win 7
已邀请:

EsriSupport

赞同来自:

错误信息: 在64位操作系统上使用Visual Studio 2010以Microsoft .NET 3.5 framework为目标框架生成ArcGIS Engine 控件应用程序时,发生如下错误:

"Could not load file or assembly 'file:///C:/Program Files (x86)/ArcGIS/DeveloperKit10.0/DotNet/ESRI.ArcGIS.3DAnalyst.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Line 162, position 5. C:\temp\Projects\MyArcGISControlApp\Form1.resx"
取决于项目的建立方式,具体的错误信息可能不尽相同。错误信息中,被引用的那个程序集会使用ArcObject .NET SDK在计算机中的安装路径。
Microsoft已经意识到该问题,并提供了一个一个解决方案:Article ID: 2028833: "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."
原因: 当 Visual Studio 项目包含一个引用了32 位 (x86) 的程序集,并且在Visual Studio 2010中以.NET 3.5编译的资源文件 (.resx) 时,会出现此问题。Visual Studio在生成过程中使用“ResGen.exe”可执行文件,该可执行文件被标记为MSIL(Any CPU),并且以64位(x64)进程运行于64位的操作系统上。由于Esri的控件是32位的并被标记,所以尝试加载3位的程序集时发生失败。

解决方法:
Microsoft的技术支持文章(Arcticle ID:2028833)提供的方法足以解决该问题。改变可执行文件CorFlags的文件头信息,强制以32位运行。
请参阅下面的情景来理解如何程序是如何工作的。 如果开发人员创建 可执行文件,将其目标平台设置到任何的 CPU ,本质上是为可执行文件创建了一个头文件以告知公共语言运行时(CLR)以相同的进程空间来加载和运行该可执行文件。 当在 32 位计算机上运行可执行文件,它将运行作为 32 位应用程序 ; 和 64 位计算机上将作为 64 位应用程序运行。

当创建一个引用了特定进程空间的程序集的可执行文件时,例如一个ArcGIS Engine ArcObjects 应用程序,进程空间应该始终设置为 x86。因为ArcObject程序集是32位的,那么无论目标机器是32位还是64位,都强制可执行文件必须始终以32位进程运行,并只能以32位的方式加载该程序集。

这个解决方法是使用 CorFlags.exe 来更改 ResGen.exe 的头文件,使它作为 32 位应用程序运行,并使它能够正确加载 Esri 32 位程序集。
Microsoft技术文章(Arcticle ID:2028833)中提供了详细的步骤可以在项目中自动完成,为了正确的解决该问题,应该以管理员身份运行Visual Studio。
下面的解决方案给Visual Studio项目添加了一个自定义生成步骤,临时性的更改Resgen.exe的头文件的CorFlags部分,以完成应用程序的编译,在完成之后再更改回原样。
注意:下面的操作说明将会更改Microsoft Visual Studio的基本文件,并且有可能存在潜在的风险。选择继续操作,自己承担风险。
1、在Visual Studio 2010中右键单击项目,选择“卸载项目”。
2、再次右键单击该项目并选择“编辑<项目名称>”。可以从该位置编辑项目的XML文件。
3、在项目的任何位置添加下述XML。最好是在文件的末尾,在“</Project>”标签的上方,这个位置很容易找得到。

<!--

Workaround for VS2010 .NET 3.5 application referencing x86 assembly in resx file on 64-bit OS
http://social.msdn.microsoft.c ... 06bf2

-->
<PropertyGroup>
<ForceResGen32Bit Condition="'$(MSBuildToolsVersion)'=='4.0' And '$(PROCESSOR_ARCHITEW6432)'!='' And '$(TargetingClr2Framework)'=='true' And '$(PlatformTarget)'=='x86'">true</ForceResGen32Bit>

</PropertyGroup>
<Target Name="BeforeResGen" Condition="'$(ForceResGen32Bit)' == 'true'">
<PropertyGroup>
<ResGenSdkToolsPath>$(IntermediateOutputPath)ResGenForced32Bit\</ResGenSdkToolsPath>
</PropertyGroup>
<!-- Copy resgen.exe to intermediate working directory for UAC settings -->
<Copy SourceFiles="$(TargetFrameworkSDKToolsDirectory)ResGen.exe"
DestinationFiles="$(ResGenSdkToolsPath)ResGen.exe" />

<!-- corflags.exe resgen.exe /32BIT+ /Force-->
<Exec WorkingDirectory="$(ResGenSdkToolsPath)"
Command="&amp;quot;$(TargetFrameworkSDKToolsDirectory)corflags.exe&amp;quot; ResGen.exe /32BIT+ /Force" />

<!-- GenerateResource Task parameters
Using the non-64bit Tracker.exe and indicate resgen.exe has been forced to x86 -->
<PropertyGroup>
<ResGenTrackerSdkPath>$(SDK40ToolsPath)</ResGenTrackerSdkPath>
<ResGenToolArchitecture>Managed32Bit</ResGenToolArchitecture>
<CacheTargetFrameworkSDKToolsDirectory>$(TargetFrameworkSDKToolsDirectory)</CacheTargetFrameworkSDKToolsDirectory>
<TargetFrameworkSDKToolsDirectory>$(ResGenSdkToolsPath)</TargetFrameworkSDKToolsDirectory>
</PropertyGroup>
</Target>
<Target Name="AfterResGen" Condition="'$(ForceResGen32Bit)' == 'true'">
<PropertyGroup>
<TargetFrameworkSDKToolsDirectory>$(CacheTargetFrameworkSDKToolsDirectory)</TargetFrameworkSDKToolsDirectory>
</PropertyGroup>

<RemoveDir Directories="$(ResGenSdkToolsPath)" Condition="Exists('$(ResGenSdkToolsPath)')" />
</Target>

4、右键单击项目并选择“重新加载项目”。
5、生成该项目。
相关信息
  1. Microsoft Knowledge Base Article ID: 2028833



创建时间:2011-08-26
最近更新: 2011-08-26


原文链接
http://support.esrichina.com.c ... .html

要回复问题请先登录注册