Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add dll in c# project

Tags:

c#

dll

I am trying to add a [Science.dll] in my project which should be straight forward. But I am getting a problem. Can someone tell me why?

My C# project which I just copied from internet.

using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.ComponentModel; using System.Data; using Science.Mathematics.VectorCalculus;  namespace using_science_dll {     static class Program     {         /// <summary>         /// The main entry point for the application.         /// </summary>`enter code here`     } } 

C:\Csharptutorial\using_science_dll\using_science_dll\Program.cs(7,7): error CS0246: The type or namespace name 'Science' could not be found (are you missing a using directive or an assembly reference?)

Is it because I am using VS 2008?

Science.dll will run in the following frame .Net 4.0 and Visual C# 2010 Express

enter image description here

like image 758
Shahgee Avatar asked Oct 07 '11 10:10

Shahgee


People also ask

What is DLL in C?

In Windows, a dynamic-link library (DLL) is a kind of executable file that acts as a shared library of functions and resources. Dynamic linking is an operating system capability. It enables an executable to call functions or use resources stored in a separate file.

How do I import a DLL into CPP?

Using a DLL in C++ code using LoadLibraryA function Now you need to first load the library using the LoadLibrary function which is defined in libloaderapi. h header and aliased to LoadLibraryW using #define macros. It accepts only one argument: a long pointer to wide string as the file name of the library.

How do I create a DLL file using Visual Studio in C?

To create a DLL project in Visual Studio 2019At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Library. From the filtered list of project types, select Dynamic-link Library (DLL), and then choose Next.


2 Answers

The DLL must be present at all times - as the name indicates, a reference only tells VS that you're trying to use stuff from the DLL. In the project file, VS stores the actual path and file name of the referenced DLL. If you move or delete it, VS is not able to find it anymore.

I usually create a libs folder within my project's folder where I copy DLLs that are not installed to the GAC. Then, I actually add this folder to my project in VS (show hidden files in VS, then right-click and "Include in project"). I then reference the DLLs from the folder, so when checking into source control, the library is also checked in. This makes it much easier when more than one developer will have to change the project.

(Please make sure to set the build type to "none" and "don't copy to output folder" for the DLL in your project.)

PS: I use a German Visual Studio, so the captions I quoted may not exactly match the English version...

like image 191
Thorsten Dittmar Avatar answered Sep 18 '22 14:09

Thorsten Dittmar


Have you added the dll into your project references list? If not right click on the project "References" folder and selecet "Add Reference" then use browse to locate your science.dll, select it and click ok.

edit

I can't see the image of your VS instance that some people are referring to and I note that you now say that it works in Net4.0 and VS2010.

VS2008 projects support NET 3.5 by default. I expect that is the problem as your DLL may be NET 4.0 compliant but not NET 3.5.

like image 22
ChrisBD Avatar answered Sep 20 '22 14:09

ChrisBD