I have a DLL written with x86 assembly language. All the exports refer to stdcall
functions. All exports are undecorated.
I specifically need this DLL as-is linked to a C project using an import library. LoadLibrary
or changing export names etc is not an option.
I have 2 problems
- When I create an import library (.lib) file for the DLL from a DEF file with export names, all of the names get decorated to
cdecl
! I can't find a way to specify them as undecoratedstdcall
. None of these decorated names exist in the DLL, so by run-time, nothing will work even if compilation was possible. - When I write code to call a DLL function in MSVC, whether specifying stdcall or not, the compiler modifies the names of the functions. So a call to
ExampleFunction
becomes_ExampleFunction@12
, and I need it to beExampleFunction
only.
Fortunately, what I need MUST be possible because Windows system DLLs (e.g. kernel32.dll, user32.dll etc) follow the exact same conventions. Every exported function is stdcall and is undecorated.
I've already read this: https://qualapps.blogspot.com/2007/08/how-to-create-32-bit-import-libraries.html
I encountered these issues:
- using
extern "C"
doesn't apply here as this is a C project. That specifier won't even work and despite this being a C project, names get modified anyway. - Creating a .lib file from a DEF file with just raw function names doesn't work. The names get modified.
The example-lib.def
file looks like this:
LIBRARY example-lib.dll
EXPORTS
ExampleFunction
The .lib
file is generated with this command:
lib.exe /def:example-lib.def /OUT:example-lib.lib
The entry in the lib file looks like this when using dumpbin.exe /headers example-lib.lib
Version : 0
Machine : 14C (x86)
TimeDateStamp: 5CDBA30C Wed May 15 06:26:36 2019
SizeOfData : 00000016
DLL name : example-lib.dll
Symbol name : _ExampleFunction
Type : code
Name type : no prefix
Hint : 20
Name : ExampleFunction
I'm importing by making declarations this:
extern void __stdcall ExampleFunction(int a, int b, int c);
I call the function like this:
ExampleFunction(1, 2, 3);
The lib file and directory in which it resides is specified in VC project settings.
When the code calling ExampleFunction
compiles, the linker won't complete because it can't find _ExampleFunction@12
in my DEF file. And even if that was present in the DEF file, the program won't run because _ExampleFunction@12
isn't exported in the actual DLL's export section. Only plain ExampleFunction
is exported.
So how do I achieve having the C program call ExampleFunction from the DLL exactly as is? How is this done when a C project calls a function from user32.dll? Can I use the same procedure for achieving this?