DLL - Export and Import of functions without name mangling/decoration

by Daniel Marschall, 23 September 2015

When you create an API, name mangling/decoration becomes a problem. It is a
good solution to use the plain name of the function and define a uniform calling
convention like __stdcall. Only this way your DLL can be used by almost all
development environments. The Windows API is using exactly this combination,
__stdcall and undecorated names. It is very easy to export and import such
functions in Delphi, but rather complex in C++, so I have written this small
tutorial.

This tutorial will show you how you can export and import undecorated stdcall functions,
exactly the way the WinAPI provides them.

Export

testdll.def

LIBRARY TESTDLL
;DESCRIPTION "Test DLL"
EXPORTS
Subtract

testdll.cpp

// This symbol tells "testdll.h" to use the dllexport instead of dllimport statement can also be defined in
// "Project Properties -> Configuration Propertie -> C/C++ -> Preprocessor -> Preprocessor Definitions"
// But we can also define it here.
#ifndef TESTDLL_EXPORTS
#define TESTDLL_EXPORTS 1
#endif#include "stdafx.h"
#include "testdll.h"
// Now we define dummy functions. We could implement something here, but since we want to use
// Delphi DLLs, we simply leave them as dummies and replace the generated DLL with the Delphi-generated one.
// Important is the LIB file which will be created as side-product. With this LIB file we can staticly
// link our EXE to the DLL.
TESTDLL_API(int) Subtract(int a, int b) {
return 0;
}

Import (Early binding)

Attention: If you have not developed your DLL in VC++, you
MUST create a dummy DLL file (see previous section) so it creates a .LIB file as
side product. Without this .LIB file you cannot simply tell VC++ that it should
import the function "Subtract" from "testdll.dll". What a shame - in Delphi it
is just so easy.