简体   繁体   English

是什么导致在单元测试(NUnit或MSTest)中从C#调用的C ++函数产生的结果不同于在控制台应用程序中运行的同一代码?

[英]What is causing a C++ function called from C# in a unit test (NUnit or MSTest) to produce a different result from the same code run in a console app?

I'm calling a C++ DLL from C# via P/Invoke using DllImport. 我正在使用DllImport通过P / Invoke从C#调用C ++ DLL。 The DLL is produced by a third party and is x86 only (so our C# code is built as x86 as well). DLL是由第三方产生的,并且仅是x86(因此我们的C#代码也被构建为x86)。

If I call a certain function on the DLL from a console application, I always get one result which is correct. 如果我从控制台应用程序调用DLL上的某个函数,则总会得到一个正确的结果。 The function takes a file path and extracts some information from the file. 该函数采用文件路径,并从文件中提取一些信息。

The signature of the function is: 该函数的签名是:

private static extern int Function(
    string str1,
    int bool1,
    ref uint outUint1,
    ref uint outUint2,
    ref uint outUint3,
    ref double outDouble1,
    ref double outDouble2,
    StringBuilder outStr1);

And the unexpected result is in one of the ref uint parameters. 意外结果是ref uint参数之一。

If I create a unit test and call the exact same code (with all parameters and such hardcoded), I get an entirely different result, which is incorrect. 如果我创建一个单元测试并调用完全相同的代码(带有所有参数和类似的硬编码),则会得到完全不同的结果,这是不正确的。 The incorrect result is always the same. 错误的结果总是相同的。 I've tried both MSTest and NUnit tests using various different runners with the same result. 我已经使用各种不同的运行程序尝试了MSTest和NUnit测试,并且结果相同。

Cases producing correct results: 产生正确结果的案例:

  • Console app 控制台应用
  • Winforms app Winforms应用
  • Console app running the code but launched from a Unit Test (NUnit) 运行代码但从单元测试(NUnit)启动的控制台应用程序

Cases producing incorrect results: 产生不正确结果的案例:

  • Test run from Resharper Test Runner (NUnit) 从Resharper Test Runner(NUnit)进行测试运行
  • Test run from Visual Studio Test Runner (MSTest) 从Visual Studio测试运行程序(MSTest)进行测试运行
  • Test run from NUnit GUI Test Runner 通过NUnit GUI Test Runner进行测试运行
  • Test run from NUnit Console Test Runner 通过NUnit Console Test Runner进行测试运行

My test environment is Windows 8 and the C# is built for x86 targeting .NET framework 4. 我的测试环境是Windows 8,C#是针对面向.NET Framework 4的x86构建的。

Do any of you have any ideas about what's causing this issue or what I can do to debug it further? 你们中的任何人对导致此问题的原因或进一步调试它的方法有任何想法吗?

I will definitely be attempting to contact the third party that created the DLL, but having a good idea about exactly what's causing this issue would substantially increase the chance of it getting resolved. 我肯定会尝试与创建DLL的第三方联系,但是对导致此问题的确切原因有一个好主意会大大增加解决该问题的机会。

It sounds like the "filepath" it is expecting may be relative to the working directory, or the working directory or even the application directory is important to the dll. 听起来像它所期望的“文件路径”可能与工作目录有关,或者工作目录甚至应用程序目录对dll很重要。

That dll may also try to reference other dll's in its directory, but can't find them because it was loaded from its own folder instead of the exe running in its directory. 该dll也可能尝试引用其目录中的其他dll,但找不到它们,因为它是从其自己的文件夹而不是在其目录中运行的exe加载的。

About half way down this page it describes the search order for dll resolution: 此页面的大约一半处,它描述了dll解析的搜索顺序:

  1. The directory from which the application loaded. 从中加载应用程序的目录。

  2. The current directory. 当前目录。

  3. The system directory. 系统目录。 Use the GetSystemDirectory function to get the path of this directory. 使用GetSystemDirectory函数获取此目录的路径。

  4. The 16-bit system directory. 16位系统目录。 There is no function that obtains the path of this directory, but it is searched. 没有获取该目录路径的函数,但会对其进行搜索。

  5. The Windows directory. Windows目录。 Use the GetWindowsDirectory function to get the path of this directory. 使用GetWindowsDirectory函数获取此目录的路径。

  6. The directories that are listed in the PATH environment variable. PATH环境变量中列出的目录。 Note that this does not include the per-application path specified by the App Paths registry key. 请注意,这不包括“应用程序路径”注册表项指定的每个应用程序路径。 The App Paths key is not used when computing the DLL search path. 计算DLL搜索路径时不使用“应用程序路径”键。

Hope that helps. 希望能有所帮助。

EDIT: Another idea... You could go and write a wrapper for that DLL so that it returns the correct values, and make an interface that works with your unit tester built into your wrapper. 编辑:另一个想法...您可以去为该DLL写一个包装器,以便它返回正确的值,并创建一个与内置在包装器中的单元测试器一起工作的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用NUnit 3(C#)从代码运行特定的NUnit测试 - How to run a specific NUnit test from code using NUnit 3 (C#) 未从 C++ 代码调用 C# 回调 function - The C# callback function is not called from C++ code 在 c# MSTest 单元测试中调用 Dispose() 方法是什么? - What calls Dispose() method in c# MSTest unit test? C#单元测试(Nunit)控制台应用程序的主要方法? - C# Unit Testing(Nunit) the Main method of a console app? 使用C#代码和来自文件的测试用例进行NUnit测试的问题 - Problem with NUnit testing with C# code and test case from file 从控制台应用程序调用但未从NUnit测试调用时,库工作 - Library working when called from Console app but not from NUnit test C# 单元测试 DynamicDataDisplayName 使用 MSTest - C# Unit Test DynamicDataDisplayName using MSTest 如何从C#代码运行NUnit测试 - How to Run NUnit Tests from C# Code 与 C# 控制台应用程序中的相同代码相比,Regex.Match 在 Unity 中返回不同/错误的结果 - Regex.Match returns different/wrong result in Unity compared to same exact code in a C# console app 我单位客户端的序列化object结果和c#控制台服务器的结果不一样 - The serialized object result of my unit client is different from that of the c# console server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM