在C#单元测试中使用64位本机.DLL

在C#单元测试中使用64位本机.DLL

问题描述:

我有一个由CMake自动生成的vcxproj文件构建的64位本机库。部分CMake构建脚本自动生成csharp中的Swig文件。我使用下面标记为.csproj的.csproj生成.cs文件,该文件标记为Articaft 1。然后,我有一个C#单元测试项目神器2低于它引用从Articaft 1产生的.dll文件。 Articafct 2也复制由64位原生库生成的dll。在C#单元测试中使用64位本机.DLL

如果我编译为32位库一切工作正常,但如果我去使用64位本机库中的C#单元测试项目,测试失败,本机库:

C:\Users\mehoggan\Devel\QuadKeys\UnitTestCSharp\TestBingSystem.cs:line 13 
Result Message: 
Test method QuadKeyTests.TestBingSystem.TestCtor threw exception: 
System.TypeInitializationException: The type initializer for 'QuadKeyNS.SwigQuadKeyPINVOKE' threw an exception. ---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception. ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) 

如何如何让我的C#单元测试使用库的64位版本?


神器1

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> 
    <PropertyGroup> 
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> 
     <ProjectGuid>f7fe6699-ce1d-443b-b6e9-05eb4e135ac6</ProjectGuid> 
     <OutputType>Library</OutputType> 
     <AppDesignerFolder>Properties</AppDesignerFolder> 
     <RootNamespace>QuadKeyCSharp</RootNamespace> 
     <AssemblyName>QuadKeyCSharp</AssemblyName> 
     <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> 
     <FileAlignment>512</FileAlignment> 
    </PropertyGroup> 
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> 
     <DebugSymbols>true</DebugSymbols> 
     <DebugType>full</DebugType> 
     <Optimize>false</Optimize> 
     <OutputPath>bin\Debug\</OutputPath> 
     <DefineConstants>DEBUG;TRACE</DefineConstants> 
     <ErrorReport>prompt</ErrorReport> 
     <WarningLevel>4</WarningLevel> 
    </PropertyGroup> 
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> 
     <DebugType>pdbonly</DebugType> 
     <Optimize>true</Optimize> 
     <OutputPath>bin\Release\</OutputPath> 
     <DefineConstants>TRACE</DefineConstants> 
     <ErrorReport>prompt</ErrorReport> 
     <WarningLevel>4</WarningLevel> 
    </PropertyGroup> 
    <ItemGroup> 
     <Reference Include="System"/> 
     <Reference Include="System.Core"/> 
     <Reference Include="System.Xml.Linq"/> 
     <Reference Include="System.Data.DataSetExtensions"/> 
     <Reference Include="Microsoft.CSharp"/> 
     <Reference Include="System.Data"/> 
     <Reference Include="System.Net.Http"/> 
     <Reference Include="System.Xml"/> 
    </ItemGroup> 
    <ItemGroup> 
     <Compile Include=".\csharp\*.cs" /> 
     <Compile Include="AssemblyInfo.cs" /> 
    </ItemGroup> 
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 
</Project> 

神器2

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <PropertyGroup> 
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> 
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> 
     <ProjectGuid>{1306B89A-7ED0-4A90-B7A2-6A39ABD9016C}</ProjectGuid> 
     <OutputType>Library</OutputType> 
     <AppDesignerFolder>Properties</AppDesignerFolder> 
     <RootNamespace>UnitTestCSharp</RootNamespace> 
     <AssemblyName>UnitTestCSharp</AssemblyName> 
     <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> 
     <FileAlignment>512</FileAlignment> 
     <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> 
     <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> 
     <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> 
     <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath> 
     <IsCodedUITest>False</IsCodedUITest> 
     <TestProjectType>UnitTest</TestProjectType> 
    </PropertyGroup> 
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> 
     <DebugSymbols>true</DebugSymbols> 
     <DebugType>full</DebugType> 
     <Optimize>false</Optimize> 
     <OutputPath>bin\Debug\</OutputPath> 
     <DefineConstants>DEBUG;TRACE</DefineConstants> 
     <ErrorReport>prompt</ErrorReport> 
     <WarningLevel>4</WarningLevel> 
    </PropertyGroup> 
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> 
     <DebugType>pdbonly</DebugType> 
     <Optimize>true</Optimize> 
     <OutputPath>bin\Release\</OutputPath> 
     <DefineConstants>TRACE</DefineConstants> 
     <ErrorReport>prompt</ErrorReport> 
     <WarningLevel>4</WarningLevel> 
    </PropertyGroup> 
    <ItemGroup> 
     <Reference Include="QuadKeyCSharp"> 
      <HintPath>..\Swig\bin\$(Configuration)\QuadKeyCSharp.dll</HintPath> 
     </Reference> 
    <Reference Include="System" /> 
    </ItemGroup> 
    <Choose> 
     <When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'"> 
      <ItemGroup> 
       <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" /> 
      </ItemGroup> 
     </When> 
     <Otherwise> 
      <ItemGroup> 
       <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" /> 
      </ItemGroup> 
     </Otherwise> 
    </Choose> 
    <ItemGroup> 
     <Compile Include="*.cs" /> 
    </ItemGroup> 
    <ItemGroup> 
     <Content Include="SwigQuadKey.dll" /> 
    </ItemGroup> 
    <Choose> 
     <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'"> 
      <ItemGroup> 
       <Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> 
        <Private>False</Private> 
       </Reference> 
       <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> 
        <Private>False</Private> 
       </Reference> 
       <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> 
        <Private>False</Private> 
       </Reference> 
       <Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> 
        <Private>False</Private> 
       </Reference> 
      </ItemGroup> 
     </When> 
    </Choose> 
    <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" /> 
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 
    <PropertyGroup> 
     <PostBuildEvent>copy "$(ProjectDir)..\build\QuadKey\$(Configuration)\QuadKey.dll" "$(ProjectDir)bin\$(Configuration)\SwigQuadKey.dll" /Y</PostBuildEvent> 
    </PropertyGroup> 
</Project> 

您正在运行VSTest作为32位进程。在Visual Studio的测试菜单中,您会发现一个设置,告诉VSTest以32位或64位进程运行。只需将其更改为64位,并将您的单元测试程序集作为AnyCPU。

+0

非常感谢您的先生。 –

你公顷已经指定C#项目应该只能作为64位程序运行。将“AnyCPU”目标更改为“x64”(我想;我要离开内存)。这将告诉托管编译器生成64位代码而不是32位。

+0

不是Visual Studio专家。这只是将PropertyGroup中的平台从AnyCPU更改为x64的一个主要方面? –

+0

@MthetheHHgan是的,如果我没有记错的话。 – 1201ProgramAlarm

+0

仍然得到'System.BadImageFormatException'。生病继续玩proj文件。 –