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)
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".
How to add dll file into a UWP Project
Derive from this case, you need copy your dll to the project folder and set the type as content. Then in the proper cs file, using DllImport to PInvoke the dll.
Related
I have created a Class Library project with some COM visible types. The project is installed via InstallShield.
To ensure proper registration of my assembly, I have defined the following in the build events:
"%Windir%\Microsoft.NET\Framework64\v4.0.30319\regasm" /codebase /tlb
"$(MSBuildProjectDirectory)\$(OutputPath)$(AssemblyName).dll"
I am trying to use this tlb file in vba. When I build the solution it is creating the tlb file and I am able to add that tlb file as reference and use it in VBA.
I created an installer for this project and added .dll and .tlb files in the project files in InstallShield
I installed the library in another system, and the .dll and .tlb files were present in the installation folder (C:/program files/Project/Name.dll)
But when I refer to that tlb file, and then try to create an object from it, VBA throws a 429 can't create object error.
In the classlibrary project properties - Platform target, I have tried Any CPU as well as x64
Below you'll find an excerpt from my blog that may help. I'm not 100% certain that you're facing the same issue that I was, but you may be.
Go to the Project Assistant, and then to the Application Files screen.
You’ll need to take a couple of Actions here to properly create the
installer. Obviously, you’ll need to add the Primary Output. What’s
not so obvious is that you’ll also need to manually link the *.tlb
file by selecting installation folder, then the Add File button, and
pointing to the file in your \bin\Release directory.
The second not so obvious (and most frustrating) thing is you’ll need
to set the properties on the assembly correctly. So, right click on
the Primary Output, and select Properties. In the dialog, go to the
COM & .Net Settings tab. Once you’re there, use the settings below.
Extract COM Information
Scan at Build == Dependencies and Properties
Check the COM Interop box.
You’ll need to do the same for the *.tlb file, only don’t check
the COM Interop box this time.
Another possibility is that you have a class that should have the ComVisible attribute that isn't, or you're missing the com visible interface that VBA needs to connect to the server.
As was mentioned in the comments, you may also be missing a dependency that your *.dll needs to function. I ran into that issue once. Installshield only picks up the files that are directly inside of the bin\release\ directory. If one of your dependencies are creating a sub-directory, say bin\release\someDirectory\someFile.dll, then you will need a custom post-build on your project to copy the dependency into the release folder prior to the installer build.
I have a solution with two projects:
Project A, a large private project
Project B, a small public project which needs a subset of the functions from A
I'm just getting started on Project B, but I already have a Project (A) which handles many things that will be needed in the new one. I would obviously like to reuse these functions without copying them.
If I add these as a reference in Visual Studio (2012) everything works code wise for Project B, but the generated assembly directory will also contain a functioning copy of Project A.exe.
I need to include my code from Project A without having a compiled .exe file in my output directory. Either by having it as a .dll file or inlined into the main assembly without having to refactor out the relevant parts of Project A into a new class library project. Is this possible?
Thanks for your time
I think you can try to change the Application Type of your "A Project" from Windows Application to Class Library
After that, compile your "A Project" and you will find the .dll in the output directory (instead the .exe)
Now that you have the .dll you can use it as a reference in your "B Project".
To change the Application Type you can visit this link form MSDN
I want to add include directories and libraries in my c# project like i can in my c++ project
C++ Project : There are options to include directories and linker dependencies.
C# Project
There does not appear to be an option in the project settings to add those settings. I added the path in reference path, but my debugger throws an error. Right now i've added all the dll in my project\bin\debug directory and its working, but i don't want to do it for all the projects. Where i can link these diretories ?
you can add reference of library files which you want to add into your project by right clicking the project under solution explorer and then "add reference" and then browse or select from the list of dlls..
You can't. You have to include references to code files and assemblies one by one.
The linker and compiler for .NET works different than you are used to with c++. That's why you can't just link an entire directory containing some code files / assemblies.
Add reference to your existing dll for every project which will be use it
http://msdn.microsoft.com/en-us/library/7314433t(v=vs.90).aspx
https://www.google.it/search?q=add+reference+c%23&es_sm=122&source=lnms&tbm=isch&sa=X&ei=P-2fU472Bcr00gX-tYAw&ved=0CAoQ_AUoAw&biw=1920&bih=955
I am building a project with OpenCV in C#. It requires a dll file called cvextern.dll. but, when adding this file as a reference, this message appears :-
a reference "cvextern.dll" can't be added, Please make sure that the file is accessible and that it is a valid assembly or COM component.
I get tired from searching, I spent the past 2 days in searching for a solution for that problem
the file is a native DLL which means you can't add it to a .NET project via Add Reference... you can use it via DllImport (see http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx)
In my case I had to register the .dll.
To do so, open cmd.exe (the console) with admin rights and type:
regsvr32 "foo.dll"
Make sure the required dlls are exported (or copied manually) to the bin folder when building your application.
'It' requires a dll file called cvextern.dll . 'It' can be either your own cs file or some other third party dll which you are using in your project.
To call native dlls to your own cs file, copy the dll into your project's root\lib directory and add it as an existing item. (Add -Existing item) and use Dllimport with correct location.
For third party , copy the native library to the folder where the third party library resides and add it as an existing item.
After building make sure that the required dlls are appearing in Build folder. In some cases it may not appear or get replaced in Build folder. Delete the Build folder manually and build again.
I had the same program, I hope this could help.
I your using Windows 7, open Command Prompt-> run as Administrator. register your <...>.dll.
Why run as Administrator, you can register your <...>.dll using the run at the Windows Start, but still your dll only run as user even your account is administrator.
Now you can add your <...>.dll at the Project->Add Reference->Browse
Thanks
Look here for the answer by TheMattster. I implemented it and it worked like a charm. In a nutshell, his solution suggests to add the COM dll as a resource to the project (so now it compiles into the project's dll), and upon the first run write it to a file (i.e. the dll file I wanted there in the first place).
The following is taken from his answer.
Step 1) Add the DLL as a resource (below as "Resources.DllFile"). To do this open project properties, select the resources tab, select "add existing file" and add the DLL as a resource.
Step 2) Add the name of the DLL as a string resource (below as "Resources.DllName").
Step 3) Add this code to your main form-load:
if (!File.Exists(Properties.Resources.DllName))
{
var outStream = new StreamWriter(Properties.Resources.DllName, false);
var binStream = new BinaryWriter(outStream.BaseStream);
binStream.Write(Properties.Resources.DllFile);
binStream.Close();
}
My problem was that not only I had to use the COM dll in my project, I also had to deploy it with my app using ClickOnce, and without being able to add reference to it in my project the above solution is practically the only one that worked.
In my case I also have unmanaged dll's (C++) in workspace and if you specify:
<files>
<file src="bin\*.dll" target="lib" />
</files>
nuget would try to load every dll as an assembly, even the C++ libraries! To avoid this
behaviour explicitly define your C# assemblies with references tag:
<references>
<reference file="Managed1.dll" />
<reference file="Managed2.dll" />
</references>
Remark: parent of references is metadata -> according to documentation
https://learn.microsoft.com/en-us/nuget/reference/nuspec#general-form-and-schema
Documentation: https://learn.microsoft.com/en-us/nuget/reference/nuspec
I have a solution which has 3 projects. One is a console app and other 2 are windows applications. Both windows applications uses console application so I added the reference of console application in both windows app projects. Now when I build windows projects, console application is being copied in output directory but the problem is that its config file "consoleapp.exe.config" is not being copied!
If I would have used a library (assembly) instead of console application, it would created the config file of that assembly in output folder.
How to solve this problem?
Try this:
Select consoleapp.exe.config in solution tree.
Select "Content" for Build Action option in property grid.
Select Copy always for Copy to Output Directory option.
Hope this helps you :)
Quickly creating a console project in VS2008 and adding an "App.config", it has settings:
Build Action: None
Copy to Ouput Directory: Do not copy
Custom Tool: <blank>
Custom Tool Namespace: <blank>
However I'm not sure what effect adding a reference to the console project will have, as a console project is an application rather than class library.
If you want the console application to start up with the Windows applications for debugging, then a better approach than project references would be to set the solution to start multiple applications. Right click on the solution, select properties and on the Start Project node, select multiple projects. (And remove the references to the console application.)
Your applications are using the default values for your settings from the compiled code of the referenced project.
You should copy your app.config from the other projects (or at least copy the settings that you want to be able to change) to the calling project, and then when the application compiles you will have an [YourAppName].exe.config that you can modify.
All this happens because an app domain in C# can have only one assembly level app.config file. Here's more information on MSDN.