Lose the DEF file…

Typically you would use a .DEF file to specify exported functions in an EXPORT section, but you can accomplish the same thing with an embeded /EXPORT directive to the linker. I’ve found it easiest to do this directly from the function being exported.

STDAPI DllRegisterServer( ) {
  #pragma comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__ ",PRIVATE")

The __FUNCTION__ and __FUNCDNAME__ macros are builtin and resolve to the undecorated and decorated names of the function they are used in. So with this syntax an /EXPORT directive is embedded in the object file which ends up like “/EXPORT:DllRegisterServer=_DllRegisterServer@0,PRIVATE” for x86 builds and “/EXPORT:DllRegisterServer=DllRegisterServer,PRIVATE” for x64 builds. Using the decorated name ensures that the linker has enough information to map the exported name to a symbolic one. You can also pass other options supported by the /EXPORT command in the same way that the PRIVATE option is passed above.

You can use the same trick to pass /DEFAULTLIB, /INCLUDE, /MANIFESTDEPENDENCY, /MERGE and /SECTION commands to the linker as well.

This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s