Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Unmanaged dll cant be referenced
kestral
#1 Posted : Wednesday, August 17, 2022 12:51:49 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/30/2018(UTC)
Posts: 7
Location: Australia

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
I have an unmanaged C dll as part of my solution. When tests get run on the solution, it brings up the error

System.DllNotFoundException

Unable to load DLL ***** or one of it's dependents. The specified module could not be found (0x8007007e)

Is there a way to resolve this ?

I looked at "Copy referenced assemblies to workspace" but I couldnt find anything that looked like it would copy unmanaged dll's
Remco
#2 Posted : Wednesday, August 17, 2022 11:47:33 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Hi, thanks for posting.

We have a config setting designed specifically to deal with this scenario. See Copied Project Dependencies.
kestral
#3 Posted : Thursday, August 18, 2022 4:31:00 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/30/2018(UTC)
Posts: 7
Location: Australia

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
Thanks for the reply.

That option only seems to let me specify other projects within the same solution. The dll I'm using is a 3rd party dll and isnt in a project.

Is there an option where I can copy a specific file into the NCrunch output folder ?
Remco
#4 Posted : Thursday, August 18, 2022 9:15:41 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
kestral;16237 wrote:
Thanks for the reply.

That option only seems to let me specify other projects within the same solution. The dll I'm using is a 3rd party dll and isnt in a project.

Is there an option where I can copy a specific file into the NCrunch output folder ?


Understood. Perhaps try including it in the project as a resource file with copy local? I think something like this is very often required with native DLLs outside of NCrunch too (depends on your toolset perhaps).
kestral
#5 Posted : Thursday, August 18, 2022 2:20:42 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/30/2018(UTC)
Posts: 7
Location: Australia

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
I don;t know how to do what you suggested. Do you have a link to somewhere that shows how ?

The code I'm having trouble with looks like this

private const string Dll = @"my.dll";

[DllImport(Dll, CallingConvention = CallingConvention.Cdecl, EntryPoint = "methodname")]
[MethodImpl(MethodImplOptions.ForwardRef)]
private unsafe static extern NINT methodname_();

public unsafe static NINT methodname()
{
return methodname_();
}

It cant find the dll. I event tried with a network path, which worked when run outside of NCrunch but not inside. It would appear that NCrunch must be running inside some type of sandbox that doesnt have access to the outside drives or network.

Placing the dll in the one of the folders in the system path didnt help either.
kestral
#6 Posted : Thursday, August 18, 2022 2:57:19 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/30/2018(UTC)
Posts: 7
Location: Australia

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
I'm not sure if this is what you meant, but I added the dll as a resource, then set it to be embedded in the executable. The executable then extracts it before its accessed and writes the resource to disk. I still couldnt get the test to run. It still cant find the DLL.

I tried copying the dll to the current directory, and also to Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location and still no success. It all works when run outside of NCrunch.
Remco
#7 Posted : Friday, August 19, 2022 12:08:07 AM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
Ty setting the build action on the item to 'Content'. Set the 'Copy to output directory' setting to 'Copy always'. Make sure you add the native DLL to your test project, as this is the one that will be primarily hosted in the runtime environment. You can inspect the contents of the workspace by right-clicking on the failed build in the Tests Window an examining it in windows explorer. If things are configured right, MSBuild should treat the unmanaged DLL as a resource that is always copied adjacent to the test DLL when it builds the project. In this way, it sits in a resolution path for the code when the tests run.

You can find more information about NCrunch workspaces here.

Another option is to manually load the DLL inside your test code prior to it being needed, using Assembly.LoadFrom and specifying a path to the DLL. This is a bit messier as you need to be sure the code will always run before the DLL is needed (a SetUpFixture or equivalent might be the best option for this).
kestral
#8 Posted : Friday, August 19, 2022 3:36:13 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 12/30/2018(UTC)
Posts: 7
Location: Australia

Thanks: 1 times
Was thanked: 1 time(s) in 1 post(s)
I managed to get it going with some of the hints you had in your last message.

I ended up getting it working by creating a Resources folder under my unit test project. In that folder, I placed my DLL.
I set the DLL's build action to Content as per your suggestion
I set Copy to Output Directory to Copy Always
Where my DLL path's static string that names the dll and path, I put @"Resources\my.dll"

All seems to be working perfectly now.

Thanks very much for your assistance !
1 user thanked kestral for this useful post.
Remco on 8/20/2022(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.052 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download