C++Builder Define DLL Export Function Names Using DEF File
Recently, I've been working on looking for the way to customize dll export function names in C++Builder.
There are several methods to achieve this in VC++, including using a .def
file, using __declspec
keyword, and using an /EXPORT
specification in a LINK
command.
The third way seems not to be supported by bcc32 compiler (aka Borland C++ compiler), so I've researched on the implementation of the prior two methods.
First, I use __declspec(dllexport)
keyword with specifying different calling conventions to export the functions below.
|
If we look up our code in a disassembler, we'll find that all of these functions have the same operation code like below.
|
And let me use impdef
tool to list all the export functions to a def file.
impdef TestDll.def TestDll.dll
|
The result shows that whether we use extern "C"
or not, the effect of functions using __cdecl
are the same.
The reason is that it is the default calling convention for C and C++ programs.
(@1 with @4 and @5 with @8)
And this setting can be found in Project Options
.
So if we modify our TestDll.def
like this:
|
and put it into the project then do a compile, cheer! We make it!
Let me orginize the result into tables:
void FUNCTION_NAME()
C/C++ | __cdecl | __stdcall | __fastcall |
---|---|---|---|
extern "C" | _FUNCTION_NAME | FUNCTION_NAME | @FUNCTION_NAME |
@FUNCTION_NAME$qv | @FUNCTION_NAME$qqsv | @FUNCTION_NAME$qqrv |
After all, we can now export __declspec(naked)
function in this way!
Since functions declared with the naked attribute, the compiler generates code without prolog and epilog code, we can have a pure assembly function and do less operations to hijack a dll. :p
|
|
|
Ref:
http://docwiki.embarcadero.com/RADStudio/XE6/en/Module_Definition_Files
http://aftcast.pixnet.net/blog/post/22191720-%E4%BD%BF%E7%94%A8vc%E8%88%87bcb%E9%96%8B%E7%99%BC%E7%9A%84dll
http://purefractalsolutions.com/show.php?a=utils/expdef
http://msdn.microsoft.com/en-US/library/d91k01sh.aspx
http://msdn.microsoft.com/en-US/library/7k30y2k5.aspx
http://msdn.microsoft.com/en-US/library/dabb5z75.aspx