Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat 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?
    text
    copied!<p>I'm calling a C++ DLL from C# via P/Invoke using DllImport. The DLL is produced by a third party and is x86 only (so our C# code is built as x86 as well).</p> <p>If I call a certain function on the DLL from a console application, I always get one result which is correct. The function takes a file path and extracts some information from the file.</p> <p>The signature of the function is:</p> <pre><code>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); </code></pre> <p>And the unexpected result is in one of the <code>ref uint</code> parameters.</p> <p>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.</p> <p>Cases producing correct results:</p> <ul> <li>Console app</li> <li>Winforms app</li> <li>Console app running the code but launched from a Unit Test (NUnit)</li> </ul> <p>Cases producing incorrect results:</p> <ul> <li>Test run from Resharper Test Runner (NUnit)</li> <li>Test run from Visual Studio Test Runner (MSTest)</li> <li>Test run from NUnit GUI Test Runner</li> <li>Test run from NUnit Console Test Runner</li> </ul> <p>My test environment is Windows 8 and the C# is built for x86 targeting .NET framework 4.</p> <p>Do any of you have any ideas about what's causing this issue or what I can do to debug it further?</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload