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?
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COHow different are the good and bad results? Off by one or a lot? Are all the "incorrect" runners producing the same incorrect result?
      singulars
    2. COAll incorrect runners produce the same incorrect result. After additional research I discovered that the incorrect result is the same as the result produced when the external library is set to extract the information from the supplied file using a different method, so I suspect that it's failing to extract the information using the primary method (which would result in the correct result) and falling back to this other method (which produces the incorrect result). Now I just need to figure out why it's unable to use the primary method when run from NUnit.
      singulars
    3. COAs @phyatt alluded to NUnit runs your code in its own sandbox which may be different than your output directory. You could use [ProcMon](http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) to see what files your DLL is trying to access, which may shine some light on what's going on. Just a thought.
      singulars
 

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