Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Using" vs [DllImport]?

I was wondering what is the very top most declared references and why we still need to use DllImport? I'm talking C#.

like image 738
Kourosh Avatar asked Mar 11 '11 13:03

Kourosh


People also ask

Why use DllImport?

The MSDN states: "The DllImport attribute is very useful when reusing existing unmanaged code in a managed application. For instance, your managed application might need to make calls to the unmanaged WIN32 API." But, is that saying it is not useful to reference an unmanaged dll or impossible otherwise?

What is the use of DllImport in C#?

DllImport Attribute is a declarative tag used in C# to mark a class method as being defined in an external dynamic-link library (DLL) rather than in any . NET assembly.


1 Answers

From the MDSN documentation:

The DllImport attribute is very useful when reusing existing unmanaged code in a managed application. For instance, your managed application might need to make calls to the unmanaged WIN32 API.

Basically, when you're writing a .NET application, and a library does not have a managed wrapper (it's written in unmanaged code), you need to use DllImport to interoperate with it. Otherwise, you can reference managed libraries with a using statement like you normally would any base class library.

like image 192
wsanville Avatar answered Oct 11 '22 06:10

wsanville