22

Is it possible to use NuGet to distribute a COM DLL?

How would I setup the package?

I'm thinking that I could put the DLL in the Tools directory, then run a post-install script to register the library, but I'm not very good at PowerShell.

Are there any online examples of how to do this (if its possible)?

Jim Counts
  • 12,535
  • 9
  • 45
  • 63

1 Answers1

43

When I faced a similar problem I created a NuGet package with the following structure.

  • lib
    • MYCOMLib.dll
  • tools
    • mycom.dll
    • install.ps1

The MYCOMLib.dll is a interop DLL generated from the mycom.dll with the Type Library Importer (tlbimp.exe). This is simply done with the command :

Tlbimp mycom.dll

The install.ps1 contains the following code:

param($installPath, $toolsPath, $package, $project)

regsvr32 Join-Path $toolsPath '\mycom.dll' /s

$project.Object.References | Where-Object { $_.Name -eq "MYCOMLib" } |  ForEach-Object { $_.EmbedInteropTypes = $false }

What this script does is that it registers the COM dll and sets the EmbedInteropTypes property on the reference to false, which is necessary when using .NET 4. See Interop type cannot be embedded for more information.

Community
  • 1
  • 1
hskan
  • 683
  • 6
  • 7
  • I wish I could also accept your answer. This did help, really. – Will Marcouiller May 06 '16 at 22:17
  • Perhaps a stupid question to ask, but do I need to manually run the install.ps1 PS script in order for it to take effect, or is it automatically ran by the NuGet installation process? – Will Marcouiller May 11 '16 at 13:26
  • 1
    @WillMarcouiller nuget will run it during its install process if you place the install.ps1 in a tools folder in your nupkg. – Sean B Nov 15 '16 at 22:20
  • 1
    It appears the PowerShell install and uninstall scripts are no longer supported since NuGet 3 https://devblogs.microsoft.com/nuget/NuGet-3-What-and-Why/#powershell-install-and-uninstall-scripts – Daniel Ellis Jan 19 '23 at 16:19