Why use DllImport Attribute as apposed to adding a reference?
Asked Answered
T

4

12

I've seen a couple of examples such as this:

[DllImport("user32.dll")]
static extern bool TranslateMessage([In] ref Message lpMsg);

[DllImport("user32.dll")]
static extern IntPtr DispatchMessage([In] ref Message lpmsg);

But, what I don't understand is why someone would do that as apposed to just referencing the DLL like they do other libraries? 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?

Thickknee answered 23/7, 2010 at 20:6 Comment(0)
L
28

"But, is that saying it is not useful to reference an unmanaged dll or impossible otherwise?"

Yes, exactly so. What you're thinking of as 'referencing a DLL' is actually 'referencing a .NET assembly' - it just so happens that the most common way of packaging the kind of assemblies one tends to reference is in a DLL.

DLLImport is entirely about importing 'traditional DLLs' - i.e. ones which export all their methods using the original Windows DLL export mechanism.

Think of DLLImport as actually being called 'UnmanagedImport', and things might be clearer.

Lil answered 23/7, 2010 at 20:18 Comment(0)
M
17

Some libraries such as user32.dll are unmanaged code. Basically this means they do not have the required metadata to allow .Net to talk to them by reference (there is much more that goes into it but hopefully that gives you enough of a head start.)

Milburn answered 23/7, 2010 at 20:10 Comment(0)
S
17

In a nutshell:

  • Add Reference is used for: DLL files containing managed code

  • DllImport is used for: DLL files containing unmanaged code

Definitions:

Managed Code: code that will run only under the management of a Common Language Runtime (CLR) virtual machine, typically the .NET Framework (or Mono).

Unmanaged Code: any compiled binaries running directly on the OS; DLLs compiled using anything older than Visual Studio .NET 2002.

More details: Managed, Unmanaged, Native: What Kind of Code Is This?

Setscrew answered 28/4, 2016 at 16:26 Comment(3)
And I've upvoted again,as this explained it nicely. So rude to downvote. It should be bannedUnhand
@Unhand I agree with you - this is a nice explanation and I upvoted as well. Not sure why someone downvoted. Not sure what you mean by "it should be banned," though.Shirleneshirley
@EJoshuaS the downvoting idea in SO just does not work. It's like smearing someone over a simple mistake or a misunderstanding. This is why Facebook will never bring in a "thumbs down".Unhand
M
2

The .NET platform code compiles into Managed Code and it is stored using Assemblies, this assemblies are .DLL files BUT NOT ALL .DLL files are assemblies containing Managed Code. You only can use Managed Code with 'Add Reference' style.

Other languages and development techniques generates .DLL files with unmanaged code, actually you even can interoperate (call methods) with them but you need the DLLImport attribute

Menzies answered 23/7, 2010 at 20:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.