0

Is there a way for me to name my exported dll functions? When I use a dll export viewer the function names shown are the full declarations. I would like to use the JNA (JNI) to access functions inside the DLL, and the function names need to be a function name not a full declaration. If this is a duplicate please point it out!

  • Does this answer your question? [Java Native Access doesn't do C++, right?](https://stackoverflow.com/questions/2241685/java-native-access-doesnt-do-c-right) – Daniel Widdis Aug 29 '20 at 03:58
  • @DanielWiddis -- that is for C++ DLLs. OP is asking about C DLLs. – Andy Aug 29 '20 at 04:09
  • @Andy OP hasn't actually been clear on exactly what function names are shown. In the context of JNA, the function names are identical, and "full declarations" tend to be C++. – Daniel Widdis Aug 29 '20 at 05:53
  • @DanielWiddis -- I was going off of his tags. – Andy Aug 29 '20 at 06:01
  • 2
    @Andy fair enough, which is why I upvoted your answer. For me, this is similar to other questions I've seen regarding C++ name mangling. We both need more details to really answer the question. – Daniel Widdis Aug 29 '20 at 06:10

1 Answers1

2

It can actually be done with just the __declspec(dllexport) syntax (that is, without a .def file) if you declare the function as extern "C" (or implement it in a C file instead of C++).

extern "C"
{
  __declspec(dllexport) void __stdcall MyFunc(std::string &);
}

Generally much easier than exporting a mangled name with an alias (as you then need to track down the mangled name in order to assign an alias).

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • Doesn't that still change the export name? According to this, it would rename it to `_MyFunc`: https://stackoverflow.com/a/1467187/1204153 – Andy Aug 29 '20 at 05:03
  • That fixed it, I just surrounded all the exported functinos with "externc "C"" and it works. Could you explain what that does? – lucas.ss.05 Aug 29 '20 at 11:14
  • @lucas.ss.05 JNA uses FFI, which requires a C API. It cannot directly access C++ functions. The syntax in this answer is how you can provide a C API in a C++ DLL. – Daniel Widdis Aug 29 '20 at 15:38
  • I meant to mention that, its C im coding in – lucas.ss.05 Aug 30 '20 at 18:26