Where to place dlls for unmanaged libraries?

前端 未结 5 550
生来不讨喜
生来不讨喜 2020-12-30 22:52

I am trying to create a Nuget package for a library that depends on ghostscript and therefore references gsdll32.dll - an unmanaged library. I can\'t just included that a st

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-30 23:07

    One problem I had was that the packages path wasn't always in the same place relative to the project file. The following worked for me:

    1. Within the NuGet package, place your unmanaged DLLs in the lib\native folder.

    2. Add the following script to the tools folder:

    install.ps1

    #This script creates or updates a PackagesPath property in the project file
    param($installPath, $toolsPath, $package, $project)
    
    $project.Save()
    
    #Load the csproj file into an xml object
    [xml] $xml = Get-Content -path $project.FullName
    
    #grab the namespace from the project element 
    $nsmgr = New-Object System.Xml.XmlNamespaceManager -ArgumentList $xml.NameTable
    $nsmgr.AddNamespace('a',$xml.Project.GetAttribute("xmlns"))
    
    #find or create the property
    $property = $xml.Project.SelectSingleNode("//a:PropertyGroup//a:PackagesPath", $nsmgr)
    if (!$property)
    {
        $property = $xml.CreateElement("PackagesPath", $xml.Project.GetAttribute("xmlns"))
        $propertyGroup = $xml.CreateElement("PropertyGroup", $xml.Project.GetAttribute("xmlns"))
        $propertyGroup.AppendChild($property)
        $xml.Project.InsertBefore($propertyGroup, $xml.Project.ItemGroup[0])
    }
    
    #find the relative path to the packages folder
    $absolutePackagesPath = (get-item $installPath).parent.FullName
    push-location (split-path $project.FullName)
    $relativePackagesPath = Resolve-Path -Relative $absolutePackagesPath
    pop-location
    
    #set the property value
    $property.InnerText = $relativePackagesPath
    
    #save the changes.
    $xml.Save($project.FullName)
    
    1. Add a targets file to the build folder. (Change "MyPackage" to the name of your package). Using a unique name for the target, like "CopyMyPackage", avoids conflicts with other packages trying to define the "AfterBuild" target. This targets file makes use of the $(PackagesPath) property defined by the above script.

    MyPackage.targets

     
     
       
         
           
         
         
         
       
    
    
    1. Finally, add a "MyPackageReadMe.txt" to the Content folder. This will enable the package to install.

    See also: http://alski.net/post/2013/05/23/Using-NuGet-25-to-deliver-unmanaged-dlls.aspx

提交回复
热议问题