0

Due to specific needs i need to create a DLL which exports a function that is named in a specific way, it's also mangled.

?drawGdi@stop@234@@Z

Is there anyway of accomplishing this?

2 Answers2

2

You can do that, but you have to write a DEF file.

foo.h:

extern "C" declspec(dllexport) void foo(int);

foo.def:

EXPORTS
    ?drawGdi@stop@234@@Z=_foo

(_foo is the exported name of the function).

Remember to specify the DEF file when linking the DLL, of course.

For more details see the documentation on DEF files.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • As mentioned in the URL you gave..."There are THREE methods for exporting a definition, listed in recommended order of use: The __declspec(dllexport) keyword in the source code An EXPORTS statement in a .def file An /EXPORT specification in a LINK command". DEF file are only one of the available method! And additionally, the second choice! – mox Jan 09 '13 at 15:09
  • @mox: Yes, but that's to simply export a symbol. According the document, the DEF file is the only one that allows you to specify the `internalname` different from the `externalname`. – rodrigo Jan 09 '13 at 16:31
  • I'm using Visual Studio 2012, tried to compile using `extern "C" declspec(dllexport) void foo(int);` but it doesn't want to work, so I changed it to `extern "C" void foo(int);` and my .def files looks like `EXPORTS ?drawGdi@stop@234@@Z=foo` however the export becomes `?drawGdi` leaving the rest off. – Eric Lindgren Jan 09 '13 at 23:25
  • Try enclosing it in quotes, as in `"?drawGdi@stop@234@@Z"=foo` ? – Harry Johnston Jan 10 '13 at 01:24
1

Can't you declare your function like e.g.

 class myclass;
 extern "C" void my_function(int,myclass&);

Then it should be exported as my_function (at least on Posix systems; I guess it is the same on Windows, but I don't know).

If compiling with GCC, you could use Asm Labels. Then any name acceptable by the assembler should be ok.

On Linux with ELF executables you probably could not, as David Schwartz suggested, simply edit the binary file (because that would probably break some hash-table used in ELF for symbols).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • It needs to be exactly the name I wrote, it doesn't seem to allow '@' and '?' in the name – Eric Lindgren Jan 09 '13 at 14:53
  • @EricLindgren: Windows does not allow `?` in filenames. It's not a problem of C/C++ but Windows. – Skalli Jan 09 '13 at 15:02
  • when I see `std::string` in a exported function of a DLL there's a **red flag** raised in my mind: **Danger !** cf. this question: http://stackoverflow.com/questions/3564985/returning-stdstring-stdlist-from-dll – Stephane Rolland Jan 09 '13 at 15:06
  • That `std::string&` was only an example. Propose any other C++ (but not C) type which is relevant for the discussion.... – Basile Starynkevitch Jan 09 '13 at 15:08