3

I'm working on Windows 7 with Visual Studio 2008.

I have a .NET assembly that makes calls into a native DLL with P/Invoke. I have set up a separate .NET unit test project in my Visual Studio solution that tests the assembly by making various calls into it. However, when the unit test makes a call into the assembly, and the assembly makes a call using P/Invoke, it can't find the native DLL.

When I write a standalone .NET console application, there is no problem. The assembly can use P/Invoke and find the DLL successfully.

I can make the unit test work by calling LoadLibrary with the absolute path of the DLL before using the assembly. However, this approach is ugly and requires an absolute path - which will be problematic for other users.

In short, my question is - how can I specify or amend the DLL search path that is being used when a Visual Studio unit test is being executed?

Any help will be greatly appreciated.

Regards, Dan

3 Answers3

3

It sounds like the problem is that your native DLL is not being deployed with your unit test DLLs. This is not an uncommon problem since managed DLL's have no reference to native ones in metadata and hence deployment packages don't know to deploy them.

The most appropriate solution is to fix deployment as opposed to changing the DLL search paths. This is a unit test specific problem though. Can you tell us what framework you're using so we can help you out?

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
3

If you are using the Visual Studio 2008 integrated testing framework, you should check out the DeploymentItemAttribute Class

Basically you just decorate your test method with this attribute and it automatically copies the deployment item into the output directory before the test executes. You can even copy a whole tree of dependencies using this. It supports variable expansion and relative paths.

Also please see How to: Configure Test Deployment for more generalized information on VS2008 test deployment.

user700390
  • 2,287
  • 1
  • 19
  • 26
0

My solution was adding a post build command to my test project as follows:

xcopy /Y /S "$(SolutionDir)\ShredLibraries\*" "$(TargetDir)"

this is explained in msdn docs: https://msdn.microsoft.com/en-us/library/ms182475.aspx

cahit beyaz
  • 4,829
  • 1
  • 30
  • 25