Search code examples
c#.netdlluwppdfium

How to add dll file into a UWP Project


I'm building a UWP Viewer for the Pdfium library(pdfium.dll). pdfium.dll is a c++ class library. In my Visual Studio Solution, I have two UWP projects. First Project(PdfiumViewer) is a UWP Class Library which imports functions from pdfium.dll and creates user controls. Eg:

[DllImport("pdfium.dll")]
public static extern void FPDF_AddRef();

The second project(PdfiumViewer.Demo) is a UWP Blank App and I referenced the first project here So that I can use its user controls in this project. I included pdfium.dll into the bin folder of my second project. But when I run the program, I get an exception saying "Unable to load pdfium.dll".

There is an existing similar Viewers developed for .Net Windows Forms and WPF apps.

I tried to add pdfium.dll as a reference by right-clicking reference -> "Add reference". In all of these cases(.Net WinForm, .Net WPF, UWP), adding it gave me an error. (ERROR : A reference to ....\pdfium.dll could not be added. please make sure that the file is accessible, and that it is a valid assembly or COM component)

I added the pdfium.dll into .Net WinForm and .Net WPF in a similar way(to the bin folder) and run them. Both types of application viewers worked fine.

My Question: Since UWP apps run in a sandbox, Is there a special location to add this dll(pdfium.dll)?

Note: I tried to add the file in different locations. (inside APPX folder, inside bin\Debug folder, inside bin\Debug\x64, etc)


Solution

  • When adding the dll to the VS UWP Project, follow the below steps.

    First, check the dll using a dependency checker and find out whether the dll is needing additional dependencies.( Such as any other dlls)

    Then, (1). Right-click the project -> "Add" -> "Add existing item" -> Select the dll -> Choose "Add as Link" Button from the arrowhead in Add button. (2). After adding the dll, Change the properties as "Content" and "Copy if newer".