Click here to Skip to main content
15,905,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
how mutch ways are there to use dll in C# projects?

1- we define dll and its function with :
[DllImport("dllName.dll")]
public static extern void FunctionDll();


2- we can add dll file as reference (AddReference) to project then call dll functions, directly .

other ?

what kind of dll files can we add as reference and call their functions?

i have a dll that is created by VC++ (MFC).
Which way is the best using of that dll in C# project?

thank your help,
Posted

There are two types of DLL file: Native and .NET, and they have to be handled differently.
They do not have any "external" difference, they are both files with a ".DLL" extension as far as the OS is concerned.

Native DLLs do not contain .NET code, and cannot be added as a reference - you have to use DllImport to access the methods in the file.

.NET DLLs do contain assemblies, and are used by adding a reference, (and a using line if necessary) and can then be treated as if the classes were in your project - you can create instances, call methods both static and instance, use properties, etc.

Only .NET DLL and EXE files can be added as references.
An MFC C++ DLL file is a Native DLL and can only be accessed via DllImport.
 
Share this answer
 
Comments
Zon-cpp 7-Jan-15 6:05am    
thank you very much
OriginalGriff 7-Jan-15 6:10am    
You're welcome!
TheRealSteveJudge 7-Jan-15 6:26am    
Well explained in only a few words! 5*
What wasn't mentioned is that you can also dynamically load assemblies at a runtime.
Example from SO:here[^]

As to which one you should use, it depends on your project.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900