4
votes

I have a managed project that uses a C-style native DLL through P/Invoke.

What is the correct way to package the native DLL so it can be added as a NuGet package to the managed project, and have the DLL be copied automatically to the output folder?

I have currently created a package using CoApp for the native DLL but i can't use it from the managed project; I get the following error when trying to add the package:

Could not install package 'foo.redist 1.0.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5.1', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Currently i only have these "pivots" in the autopkg file:

[Win32,dynamic,release] {
    bin: release\foo.dll;
}
[Win32,dynamic,debug] {
    bin: debug\foo.dll;
}

... do i need to add something else?

1
Did you ever sort this out using CoApp?Edenhill

1 Answers

1
votes

I'm in a similar situation. I opted not to use CoApp for this project, but to create a fresh nuspec/.targets file combination instead.

Inside the nuspec file I use a <files> element to list my native dlls.

In the .targets file you have access to the msbuild Condition attribute, which allows basic Configuration pivoting. In our case we always deploy 64 bit binaries, so the Platform pivot is not needed, but you could also add it if needed.

I get warnings when running nuget pack since the binaries are not inside lib, but it works fine otherwise.

Steps:

  • run nuget spec in the folder that contains your vcxproj
  • create a .build folder, in that folder create an empty mydll.targets file (match the nuspec filename)
  • manually populate the files similarly to the examples below;

Example mydll.nuspec:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
  ...your metadata here
</metadata>
<files>
  <file src="x64\Release\my.dll" target="x64\Release\my.dll" />
  <file src="x64\Debug\my.dll" target="x64\Debug\my.dll" />
</files>
</package>

Example mydll.targets:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)\..\x64\Release\my.dll" Condition="'$(Configuration)'=='Release'">
  <Link>my.dll</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\..\x64\Debug\my.dll"  Condition="'$(Configuration)'=='Debug'">
  <Link>my.dll</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>