簡體   English   中英

在C#單元測試中使用64位本機.DLL

[英]Use 64-bit Native .DLL in C# Unit Test

我有一個由CMake自動生成的vcxproj文件構建的64位本機庫。 CMake構建腳本的一部分會在csharp中自動生成Swig文件。 我使用下面標記為Articaft 1的.csproj 構建Swig生成的.cs文件。 然后,我有一個C#單元測試項目Artifact 2,下面引用了從Articaft 1生成的.dll。 Articafct 2還復制了由64位本機庫生成的dll。

如果將本機庫編譯為32位庫,那么一切正常,但是如果我在C#單元測試項目中使用64位本機庫,則測試將失敗並顯示以下內容:

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#項目僅應作為64位程序運行。 將“ AnyCPU”目標更改為“ x64”(我想;我的內存不足了)。 然后,這將告訴托管編譯器生成64位代碼而不是32位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM