c# - 在 C# 单元测试中使用 64 位 native .DLL

标签 c# c++ 64-bit dynamic-linking

我有一个由 CMake 自动生成的 vcxproj 文件构建的 64 位 native 库。部分 CMake 构建脚本会在 csharp 中自动生成 Swig 文件。我使用下面标记为 Articaft 1 的 .csproj 构建了 Swig 生成的 .cs 文件。然后,我有一个 C# 单元测试项目 Artifact 2,下面引用了 Articaft 1 生成的 .dll。 Articafct 2 还复制 64 位 native 库构建生成的 dll。

如果我将 native 库编译为 32 位库,一切正常,但如果我在 C# 单元测试项目中使用 64 位 native 库,测试将失败并显示:

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。

关于c# - 在 C# 单元测试中使用 64 位 native .DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34279202/

相关文章:

c# - 将参数传递给 dotnet 测试项目?

c# - Left Join List<t> with Datatable -Linq

c++ - 在中序遍历中,如何从一个递归到下一个递归保存一个元素?

c++ - C 和 C++ 构建过程的区别

windows - 检查文件是 32 位还是 64 位 - 在 Windows 上

linux - 让新的 Squeak 5 在 64 位 Linux 上运行

objective-c - 64 位模式下偶发 EXC_BAD_INSTRUCTION (SIGILL)

c# - 如何增加单选按钮之间的间距

c# - LINQ/ Entity Framework : parameterless constructor issue

使用 antlr 和 line 指令的 C++ 代码插入