-3

My experience is with Delphi XE2 and I am not familiar with MS Visual C/C++. I am trying to compile some MS Visual C++ code into a DLL I can use from Delphi.

The compiled DLL exported functions cannot be found by Delphi.

Using a hex editor the DLL export table looks like the following snippets and does not have NULL terminated function names (a NULL ASCII char is positioned following the eol "Z"):

      ?MM_End@@YAH_N@Z
      ?MM_GetCurrentPosition@@YAHPAN0000@Z
      ?MM_GetWindow@@YAHPAN000@Z
      ?MM_MarkGetLinkFile@@YAHPAXPADH@Z
      ?MM_Start@@YAH_N@Z

The C++ header code looks like this:

    ...
    #define MMAPI_API __declspec(dllexport)
    ...
    MMAPI_API int MM_Start(bool run_mmnav);
    MMAPI_API int MM_End(bool close_mmnav);
    ...

With reference to https://msdn.microsoft.com/en-AU/library/dt232c9t%28v=vs.90%29.aspx. I have tried several variations of __stdcall and _cdecl but cannot get MS Visual C++ to compile the export table with NULL terminated strings.

halfer
  • 19,824
  • 17
  • 99
  • 186
Glen
  • 1
  • 1
  • Some people here say that downvotes should be accompanied with an explanation. I don't agree, but feedback is worthwhile in this case. David below offered a great deal of help, but you were rather rude to him. It is worth donning a Kevlar jacket when posting your tech problems on the web! Criticism will happen, and as long as it is civil, it can still be constructive. – halfer Sep 09 '15 at 22:40

1 Answers1

2

You are attempting to reverse engineer, by guesswork, a format that is well documented. The PE format is known and documented. There's no point in you trying to reverse it with a hex editor. There's no need for you to understand the format at all. Use an existing tool to list the exported functions. For instance dumpbin from the MS tool chain, or Dependency Viewer.

Once you've listed the exports you'll find that they have been exported under their C++ mangled names. Name mangling is how C++ compilers encode the function signature in the function name used for linking. Mangling was designed to allow C++ tool chains to support overloaded functions and continue using C style linker technology.

You'll see a list of exported symbols like these:

  • ?MM_Start@@YAH_N@Z
  • ?MM_End@@YAH_N@Z
  • etc.

Run these through a demangler to confirm that they are what you are expecting. The above symbols demangle as:

int __cdecl MM_Start(BOOL)
int __cdecl MM_End(BOOL)

You can import them using those names if you wish. For example:

function MM_Start(run_mmnav: BOOL): Integer; cdecl;
  external dllname name '?MM_Start@@YAH_N@Z';

function MM_End(run_mmnav: BOOL): Integer; cdecl;
  external dllname name '?MM_End@@YAH_N@Z';

Or you can choose to suppress C++ name mangling of the exported functions when you compile the DLL. Do that by wrapping the function declarations in an extern "C" block, or by exporting the functions with a .def file.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thank you David for your forthright comments. Your answer makes the same broad assumptions the documentation does and that is not helpful. By way of debug I responded to your answer in http://stackoverflow.com/questions/9576118/how-to-check-a-dll-if-a-function-exists to determine if the function names did indeed existed in the export table. By looking at Delphi acceptable local DLL the DLL I was compiling was clearly not of the same structure. My problem is how to compile the DLL with a totally unknown MS C/C++ environment. No reverse engineering involved! – Glen Aug 30 '15 at 23:12
  • You are clearly thinking about this the wrong way. My answer would be useful if you'd open your mind. – David Heffernan Aug 31 '15 at 06:03
  • With respect David you sound like the pot calling the kettle black. Your thinking and perception of the problem is closed. – Glen Aug 31 '15 at 06:27
  • I don't think so. I've given clear explanations for how you can find the names of the exported functions, and why they are mangled, and how to export them unmangled if you prefer. Do you still hope to find a way to put null terminators in the PE metadata? – David Heffernan Aug 31 '15 at 06:29
  • I have never tried to put null terminators in any file. I am not going to continue this fruitless dialog. – Glen Aug 31 '15 at 06:39
  • In the question you said "cannot get MS Visual C++ to compile the export table with NULL terminated strings". Perhaps I misunderstood that then. – David Heffernan Aug 31 '15 at 06:40
  • On the other hand, is it possible that you've never encountered C++ name mangling before? – David Heffernan Aug 31 '15 at 07:20