What's the difference of [DllImport ....] with _declspec(dllimport)

E

Ed

Hello, dear all,

I often see these two import usage in the code. Both are the interface
to use the Dll library. I think they are the same.
Normally P/Invoke means using the [DllImport ....] to import the
dll. But I have some confusion about them.

1. Is the "_declspec(dllimport)" also one of the forms of P/Invoke?
2. From performance aspect, which way is more better?
3. What's the difference of them.


Thank you in advance!
 
C

Carl Daniel [VC++ MVP]

Ed said:
Hello, dear all,

I often see these two import usage in the code. Both are the interface
to use the Dll library. I think they are the same.
Normally P/Invoke means using the [DllImport ....] to import the
dll. But I have some confusion about them.

1. Is the "_declspec(dllimport)" also one of the forms of P/Invoke?

No. __declspec(dllimport) is a compiler directive that tells the compiler to
expect that the function so decorated will be satisfied by an import library
to be linked into the module. [DllImportAttribute] is a CLR attribute used
in managed (.NET) development that provides the mapping for P/Invoke to call
an external function. They're similar, it's true, but the first
(__declspec) has nothing to do with managed programming.
2. From performance aspect, which way is more better?

They're used for different purposes. If you have a choice (e.g. you're
writing managed C++), __declspec(dllimport) will never give worse
performance.
3. What's the difference of them.

See above.

-cd
 
E

Ed

Ed said:
Hello, dear all,
I often see these two import usage in the code. Both are the interface
to use the Dll library. I think they are the same.
Normally P/Invoke means using the [DllImport ....] to import the
dll. But I have some confusion about them.
1. Is the "_declspec(dllimport)" also one of the forms of P/Invoke?

No. __declspec(dllimport) is a compiler directive that tells the compiler to
expect that the function so decorated will be satisfied by an import library
to be linked into the module. [DllImportAttribute] is a CLR attribute used
in managed (.NET) development that provides the mapping for P/Invoke to call
an external function. They're similar, it's true, but the first
(__declspec) has nothing to do with managed programming.
2. From performance aspect, which way is more better?

They're used for different purposes. If you have a choice (e.g. you're
writing managed C++), __declspec(dllimport) will never give worse
performance.
3. What's the difference of them.

See above.

-cd

Thank you ! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top