Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

When is __declspec(dllimport) necessary

49 views
Skip to first unread message

Clark Sims

unread,
Jul 12, 1999, 3:00:00 AM7/12/99
to
System: Windows 98/NT
Compiler: Watcom C 11.0

I have several libraries where I am exporting both classes and
functions. When is it necessary to declare a class or function as
declspec dllimport?

Thanks in Advance,

Clark Sims


Bernd Luevelsmeyer

unread,
Jul 12, 1999, 3:00:00 AM7/12/99
to

It's necessary to declare objects (C objects, that is data stuff as
opposed to functions) __declspec(dllimport). Imported functions will get
a slight performance boost from it, but variables without
__declspec(dllimport) will not work at all.

The reason is the import address table that is used for DLL imports. It
is a function stub consisting of a jump to the "real" address, the real
address will be fixed at runtime when loading the DLL.

If it's a function then the called address will, without
__declspec(dllimport), be the stub's address. This doesn't hurt very
much (you get an additional jmp when calling a DLL function), but with
__declspec(dllimport) the jump can be avoided; the compiler will call
the jmp target address indirectly, bypassing the jmp instruction.

Objects are treated just like functions, however, and you don't want
your imported variable to be treated as if located at a jump instruction
in the import address table. Therefore you may not import objects
without telling the compiler they are imported. Telling the compiler
it's in fact a DLL imported variable will allow it to use a pointer to
access the variable indirectly, and the pointer is considered to be
located where the jump target for functions is fetched from, in the
import address table; so a
extern __declspec(dllimport) int foo;
is in fact treated like a
extern int *foo;
by the compiler. This works nicely.

You can see the effect in the disassembler listing. Imports without
__declspec(dllimport) are decorated normally and treated according to
their declaration, and imports with __declspec(dllimport) get a
prepended "__imp_" to their name and have an additional indirection.

Bernd Luevelsmeyer

unread,
Jul 12, 1999, 3:00:00 AM7/12/99
to
u...@40th.com wrote:
>
> Bernd Luevelsmeyer? (bernd.lue...@iplan.heitec.net?) wrote (12 Jul 1999):

> >a slight performance boost from it, but variables without
> >__declspec(dllimport) will not work at all.
>
> If you realize what you get you can make it work...just skip over the
> jmp junk and pull out the pointer, then deref that. Seen it. Don't
> use it. Would if I had to.

Of course you can make it work; but in which case would skipping over
the jmp instruction and pulling out the pointer be more comfortable than
declaring the variable __declspec(dllimport)? Perhaps a compiler that's
old enough not to support __declspec?

It reminds me of calling virtual functions in C++ classes from C source
by finding and parsing the vtable and inventing a 'this' pointer. I've
done that, but I didn't like it :-)

0 new messages