1

I have read about extern in MSDN:

http://msdn.microsoft.com/en-us/library/e59b22c5.aspx

I'm not sure what is the difference between using extern than just adding the dll as a reference to the project.

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203

2 Answers2

4

As it is stated it is typically used with DllImport. A common scenario with DllImport is using native dlls instead of managed ones.

As you cannot add native dlls to your project as a reference you need to import it during runtime.

If you have managed assemblies it is typically better to indeed reference it in your project.

See the samples of your link, they import native libraries and call functions on them.

Samuel
  • 6,126
  • 35
  • 70
0

One difference: if you want t copy or reuse of this code, DllImport work fine.

  • Please elaborate as your answer does not make sense for me. – Samuel Feb 22 '14 at 18:59
  • When you adding the dll as a reference, and add using dll namespace,and use functions of dll in a method,when code or reuse of this method, may need to add reference to other class or project. – Mohammad Shokouhi Gol Feb 22 '14 at 19:04
  • Well I suppose there is a language barrier here but I think I got what you are trying to say: When you reference an assembly you create an explicit dependency. When your assembly is used by another assembly you also get an implicit dependency on the referenced project. This is right, however DllImport does also impose a dependency and it hides it at the same time as DllImports may be scattered in user code making it hard to spot the dependency. – Samuel Feb 22 '14 at 19:09
  • Yes ,sorry for my poor English. – Mohammad Shokouhi Gol Feb 22 '14 at 19:18